-
-
Notifications
You must be signed in to change notification settings - Fork 464
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Custom manager causes phantom errors and sometimes crash #709
Comments
I'm running into this too. I realized probably all of our models that define custom managers using At first sight it looked like moving the manager definition outside of the model class body fixed the issue, but after doing that a whole sea of odd errors appear in the manager module. Things like this:
|
@antonagestam I think I've found the cause of these kind of errors. A closer look in
Where it seems like a new "manager node" is created and from what I can tell, that new manager node ends up where the django-stubs/mypy_django_plugin/transformers/managers.py Lines 52 to 59 in fafb200
I'm quite convinced that the error you're encountering appears when:
An additional note regarding
It seems that the reason for |
@flaeppe I think your findings could explain why I had a hard time creating a minimal reproduction, I think I missed trying with manager and model in different modules. While I was completely dumbfounded by these errors, it sounds like you've figured it out 👍 |
I just hit
Even though all models / managers / querysets were defined in the same function. Using newest django-stubs. |
@aleksanb That's interesting, can you produce a repro? As far as I can tell the test that I found, below, should cover the case you describe: django-stubs/tests/typecheck/managers/querysets/test_from_queryset.yml Lines 284 to 311 in 8d8b8cd
i.e. Custom Also, be aware of that if you use |
I'll try making a minimal reproduce, but won't have time till next week. |
Cool, think you should open a new issue if you figure out a way to reproduce the error. |
I think I might have a lead. Pass: class FooManager(models.Manager['Foo']):
pass
FooModelManager = FooManager.from_queryset(FooQuerySet)
class Foo(models.Model):
objects = FooModelManager() Fail: FooModelManager = models.Manager.from_queryset(FooQuerySet)
class Foo(models.Model):
objects = FooModelManager() I've opened #1017 to try to run this failing test in CI. |
@aleksanb I have been getting a similar error too! |
Seems like CI doesn't run automatically anymore? |
I'm seeing the same issue and have traced it back to #991, will try to see if I can reproduce it in a test |
This test reproduces the issue locally for me, but not on CI for some reason (see #1021): - case: from_queryset_custom_auth_user_model
main: |
from users import User
custom_settings: |
AUTH_USER_MODEL = "users.User"
INSTALLED_APPS = ("django.contrib.auth", "django.contrib.contenttypes", "users")
files:
- path: users/__init__.py
- path: users/models.py
content: |
from django.contrib.auth.models import AbstractBaseUser
from django.db import models
from .querysets import UserQuerySet
UserManager = models.Manager.from_queryset(UserQuerySet)
class User(AbstractBaseUser):
email = models.EmailField(unique=True)
objects = UserManager()
USERNAME_FIELD = "email"
- path: users/querysets.py
content: |
from django.db import models
from typing import Optional, TYPE_CHECKING
if TYPE_CHECKING:
from .models import User
class UserQuerySet(models.QuerySet["User"]):
pass |
Bug report
What's wrong
Still working on a minimal repro, but here's my stack trace:
I have a
FooManager
:It's often used on models like this:
For some reason, three completely unrelated files are getting these errors:
I tried adding whitespace to the top of
some_file.py
but the line numbers didn't change. Odd. I looked through my project for files where lines 7 and 25 referencedmodels
and found myFooManager
. I added 1 line to the top ofFooManager
and the mypy errors incremented by 1. I added 100 lines to the top ofFooManager
and got this sweet stack trace.How is that should be
There should be no errors.
System information
python
version: 3.9.6django
version: 3.1mypy
version: 0.910django-stubs
version: 1.8.0django-stubs-ext
version: 0.3.1The text was updated successfully, but these errors were encountered: