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

Only populate a model argument for generic managers #1683

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

flaeppe
Copy link
Member

@flaeppe flaeppe commented Sep 5, 2023

I have made things!

Change the plugin so that non generic managers (like below) won't get an implicit model argument

class MyManager(models.Manager["MyModel"]):
    ...

class MyModel(models.Model):
    objects = MyManager()

Previously these(MyModel.objects) were assigned a type like: MyManager["MyModel"]

TODO:

  • Ensure compatibility between a manager and the model it is assigned to. e.g. the model argument of a manager needs to be compatible with the model the manager is instantiated on
  • Ensure compatibility between a manager and queryset e.g. the model argument of a manager needs to be compatible with the model argument of the queryset it's combined with

Related issues

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.

This is more correct! Thanks!

@@ -340,7 +343,10 @@ def run_with_model_cls(self, model_cls: Type[Model]) -> None:
incomplete_manager_defs.add(manager_name)
continue

manager_type = Instance(manager_info, [Instance(self.model_classdef.info, [])])
if manager_info.is_generic():
Copy link
Member

Choose a reason for hiding this comment

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

Maybe refactor into helper?

m_generic_from_qs_generic = ManagerGenericFromQuerySetGeneric()

qs_populated_as_manager = QuerySetPopulated.as_manager()
qs_generic_as_manager = QuerySetGeneric["MyModel"].as_manager()
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 think I found a mypy bug here, but I'm not sure. But the get_dynamic_class_hook isn't called for QuerySetGeneric["MyModel"].as_manager() due to ["MyModel"].

If I remove ["MyModel"] it is triggered. But that means the hook isn't compatible with a classmethod on a generic class.

I have no idea if it's unreasonable to expect it to be triggered, it just feels like a bug that adding type parameters affects the triggering..

Copy link
Member

Choose a reason for hiding this comment

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

Please, report it anyways :)

Copy link
Member

Choose a reason for hiding this comment

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

(and tag me, since I don't watch all issues)

Copy link
Member Author

Choose a reason for hiding this comment

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

It was already discovered, some time ago. That time from django stubs as well: python/mypy#8359

Copy link
Member

Choose a reason for hiding this comment

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

We can add a test-case with xfail for now. After [email protected] we will just remove xfail and move on.

@UnknownPlatypus
Copy link
Contributor

Hey, just chiming in to say I really appreciate all the work you all do on the plugin code to make it more reliable. Keep up the good work, would have loved to help you a bit but it's holidays season for me currently ahah.

Anyway, thanks!

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.

3 participants