-
-
Notifications
You must be signed in to change notification settings - Fork 454
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
type[Model]
no longer allows access to .objects
in django-stubs
4.2.4
#1684
Comments
Yes,
from django.contrib.auth.models import User
from django.db.models import Model
user_type: type[Model] = User
users = user_type._default_manager.all() |
Thanks for the tip @flaeppe! |
I think that we might need to put this in the docs! |
But when I switch to |
Yeah, PyCharm also warns about accessing a private member (in this case, it's "only" a yellow squiggle, and doesn't show up in CI). Ruff doesn't warn about this, which I suppose is nice, depending on your point of view. Is this fundamentally a Django issue that should be raised with them? It seems like in general it shouldn't be prescribed that end users use private methods and attributes? |
We have our own Django expert to ask :) |
I don't think that this follows. The quoted text only says that we should use I sympathise that it's a very frustrating choice on Django's part to only sometimes create the
I wholeheartedly agree. Is there some other possible work-around? Should all models explicitly define
I think that it might well be. Perhaps the solution here is that we should try to get this behaviour changed in Django. |
A quick follow-up to my comment above: Adding a We're using that instead of replacing |
Nice workaround 👍🏾 doesn't work for generic code though 🙁 |
I'm working on the same codebase, It turns out that solution causes a bunch of other problems and we weren't able to proceed with it. And ultimately the problem is working with abstract models instead of concrete models is a headache. Made worse when what concrete models are available at runtime depends on django settings in a way that means we can't explicitly refer to them in many places |
I've made this discussion to talk about how we dealt with this problem in our codebase, #2452 |
Bug report
What's wrong
Prior to
django-stubs
4.2.4 /mypy
1.5.1, the you could type hint a variable/class attribute astype[Model]
and access.objects
on it:Note that is the real usage, the variable is a class variable that is set by derived classes.
But as of the latest
django-stubs
andmypy
, this generates a type check error:How is that should be
No type check errors.
System information
python
version: 3.10.11django
version: 4.2.5mypy
version: 1.5.1django-stubs
version: 4.2.4django-stubs-ext
version: 4.2.2Mypy configuration:
The text was updated successfully, but these errors were encountered: