-
-
Notifications
You must be signed in to change notification settings - Fork 450
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
Support inheriting manager classes created via .from_queryset
#1699
base: master
Are you sure you want to change the base?
Support inheriting manager classes created via .from_queryset
#1699
Conversation
The changes here allows creation of managers looking like below ```python class MyQuerySet(models.QuerySet["MyModel"]): ... MyManager = models.Manager.from_queryset(MyQuerySet) class MyOtherManager(MyManager): ... ```
@@ -642,8 +642,8 @@ | |||
reveal_type(user.article_set) # N: Revealed type is "myapp.models.Article_RelatedManager" | |||
reveal_type(user.book_set.add) # N: Revealed type is "def (*objs: Union[myapp.models.Book, builtins.int], *, bulk: builtins.bool =)" | |||
reveal_type(user.article_set.add) # N: Revealed type is "def (*objs: Union[myapp.models.Article, builtins.int], *, bulk: builtins.bool =)" | |||
reveal_type(user.book_set.filter) # N: Revealed type is "def (*args: Any, **kwargs: Any) -> myapp.models.LibraryEntityQuerySet[myapp.models.Book]" | |||
reveal_type(user.article_set.filter) # N: Revealed type is "def (*args: Any, **kwargs: Any) -> myapp.models.LibraryEntityQuerySet[myapp.models.Article]" | |||
reveal_type(user.book_set.filter) # N: Revealed type is "def (*args: Any, **kwargs: Any) -> myapp.models.LibraryEntityQuerySet" |
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.
Is it correct?
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.
Yeah, I think this is a chain reaction of a bug that was revealed. Might be better to push it as a separate PR though.
The below change is the reason, in mypy_django_plugin/transformers/managers.py
- manager_type_info = manager_instance.type
+ manager_type_info = manager_instance.type.get_containing_type_info(method_name)
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.
Right, think I figured it out. The change there takes a more correct path. There's nothing generic on the related managers, their model arguments are already populated on parent class(es). It becomes more obvious when using .get
and seeing what model is returned. So I'll add that.
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've opened #1701
@@ -310,7 +310,7 @@ | |||
- case: handles_type_collision_with_from_queryset | |||
main: | | |||
from myapp.models import MyModel, FromQuerySet | |||
reveal_type(FromQuerySet) # N: Revealed type is "def [_T <: django.db.models.base.Model] () -> myapp.models.ManagerFromModelQuerySet[_T`1]" | |||
reveal_type(FromQuerySet) # N: Revealed type is "def () -> myapp.models.ManagerFromModelQuerySet[_T`1]" |
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.
T
will be unbound here
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.
Hm, yeah, gotta look in to that. But I'm not so sure there should be any arguments here. It might be, if we implicitly copy the manager argument to the new subclass created. It's all a bit blurry, since it needs to align with the model argument of the queryset etc.
I have made things!
The changes here allows creation of managers looking like below
I'll be honest here and will be saying that this is a bit controversial, because we kind of enforce mypy to interpret this kind of line:
As a
TypeAlias
. The only thing I have to back up the changes here, apart from that runtime wise it's actually a type alias that is created, is here: python/mypy#8897 (comment)In our case it's not too dynamic as our plugin can resolve the
.from_queryset
call. But I haven't found any other tool that we can work with to make the generated type inheritable.