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

Problem with manager created using .from_queryset() and imported from other module #1032

Closed
ngnpope opened this issue Jun 28, 2022 · 4 comments · Fixed by #1033
Closed

Problem with manager created using .from_queryset() and imported from other module #1032

ngnpope opened this issue Jun 28, 2022 · 4 comments · Fixed by #1033
Labels
bug Something isn't working

Comments

@ngnpope
Copy link
Contributor

ngnpope commented Jun 28, 2022

Bug report

When a manager is created using .from_queryset() and imported from another module, it's type is Any, but only when the QuerySet subclass has its generic specified as ["MyModel"].

What's wrong

I took this test case and adapted it to move the NewManager = BaseManager.from_queryset(ModelQuerySet) line into myapp/querysets.py and imported NewManager into myapp/models.py instead of ModelQuerySet.

See this diff: from_queryset-extra-tests.txt

Applying this to the current master results in the following two tests failing:

  • from_queryset_using_basemanager_with_manager_imported_from_other_module
  • from_queryset_using_manager_with_manager_imported_from_other_module

If I change the following in myapp/querysets.py for these tests then they pass:

-                class ModelQuerySet(models.QuerySet["MyModel"]):
+                class ModelQuerySet(models.QuerySet):

Weird.

As far as I could work out get_dynamic_class_hook is being called for myapp.models.NewManager, but then get_function_hook/get_method_hook doesn't seem to get called for methods on the manager in the failure case.

How is that should be

When NewManager is created using .from_queryset() and is imported from another module it shouldn't resolve to Any when Queryset["MyModel"] is used as the subclass, but should be the same as when only QuerySet is used as the subclass or NewManager is created in myapp/models.py.

System information

  • OS:
  • python version: 3.9.5
  • django version: 3.2.13
  • mypy version: 0.961
  • django-stubs version: 1.12.0
  • django-stubs-ext version: 0.5.0
@ngnpope ngnpope added the bug Something isn't working label Jun 28, 2022
@flaeppe
Copy link
Member

flaeppe commented Jun 29, 2022

Interestingly enough, the hook is called if I do class ModelQuerySet(models.QuerySet[models.Model]) as well.

@ngnpope
Copy link
Contributor Author

ngnpope commented Jun 29, 2022

Hmm. Yes. And it also works if I do class ModelQuerySet(models.QuerySet["models.Model"]), so it's not specifically to do with string annotations. Most likely something to do with the circular dependency and the order things are resolved - maybe we need to defer for an extra pass somewhere?

@flaeppe
Copy link
Member

flaeppe commented Jun 29, 2022

I'm suspecting it has to do with this change: #999 (comment)

Or at least that it takes that path to add the manager. I have a change in #1025 that might resolve it. I could try if that's a correct assumption

@flaeppe
Copy link
Member

flaeppe commented Jun 29, 2022

Yep, seems that it worked. See #1033

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Development

Successfully merging a pull request may close this issue.

2 participants