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

Support inheriting manager classes created via .from_queryset #1699

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

flaeppe
Copy link
Member

@flaeppe flaeppe commented Sep 10, 2023

I have made things!

The changes here allows creation of managers looking like below

class MyQuerySet(models.QuerySet["MyModel"]):
    ...

MyManager = models.Manager.from_queryset(MyQuerySet)

class MyOtherManager(MyManager):
    ...

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:

MyManager = models.Manager.from_queryset(MyQuerySet)

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)

This is generally too dynamic for mypy to understand. It would probably be quite difficult to make it work in mypy except in simple cases where all the arguments are static.

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.

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it correct?

Copy link
Member Author

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)

Copy link
Member Author

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.

Copy link
Member Author

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]"
Copy link
Member

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

Copy link
Member Author

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.

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