Skip to content
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

Merged
merged 4 commits into from
Nov 5, 2022

Conversation

clouds56
Copy link
Contributor

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.

class ModelBase(type): ...
class ModelBase(type):
@property
def _default_manager(cls: Type[_Self]) -> BaseManager[_Self]: ...
Copy link
Member

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] 🤔

Copy link
Contributor Author

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.

Copy link
Contributor Author

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.

Copy link
Member

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

@@ -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
Copy link
Member

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.

Copy link
Member

@sobolevn sobolevn left a 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:

@sobolevn
Copy link
Member

Please, make sure to fix django-stubs/db/models/base.pyi with black

@clouds56
Copy link
Contributor Author

clouds56 commented Nov 4, 2022

@sobolevn I believe all checks have passed, what's blocking?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants