-
-
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
move _default_manager to ModelBase #1150
move _default_manager to ModelBase #1150
Conversation
django-stubs/db/models/base.pyi
Outdated
class ModelBase(type): ... | ||
class ModelBase(type): | ||
@property | ||
def _default_manager(cls: Type[_Self]) -> BaseManager[_Self]: ... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that mypy might not like cls
to be bound to Type[Model]
🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, mypy's right, updated.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have no idea how to solve this, maybe a type: ignore
should be introduced.
Indeed, there's no guarantee that ModelBase
would receive cls
that bounded to Model
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can fallback to the first solution + type: ignore
django-stubs/db/models/base.pyi
Outdated
@@ -18,16 +17,17 @@ class ModelState: | |||
|
|||
class ModelBase(type): | |||
@property | |||
def _default_manager(cls: Type[_Model]) -> BaseManager[_Model]: ... | |||
def objects(cls: Type[_Self]) -> BaseManager[_Self]: ... # type: ignore |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please, add a code for these ignores. --show-error-codes
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks a lot!
Tests:
reveal_type(base_instance.model_cls._default_manager) # N: Revealed type is "django.db.models.manager.Manager[myapp.models.MyModel]" reveal_type(base_instance.model_cls._base_manager) # N: Revealed type is "django.db.models.manager.BaseManager[myapp.models.MyModel]"
Please, make sure to fix |
@sobolevn I believe all checks have passed, what's blocking? |
I have made things!
Move _default_manager from Model (as a
@classproperty
) to ModelBase (as a@property
)To match what exactly happens in django
https://github.com/django/django/blob/e14d08cd894e9d91cb5d9f44ba7532c1a223f458/django/db/models/base.py#L429
(and also makes pyright/pylance happy)
Related issues
see also #817.