Skip to content

Manager with generic QuerySet using TypeVar defaults - #2776

Draft
UnknownPlatypus wants to merge 15 commits into
typeddjango:masterfrom
UnknownPlatypus:managers-with-generic-querysets-3
Draft

UnknownPlatypus wants to merge 15 commits into
typeddjango:masterfrom
UnknownPlatypus:managers-with-generic-querysets-3

Conversation

@UnknownPlatypus

@UnknownPlatypus UnknownPlatypus commented Aug 21, 2025 •

Copy link
Copy Markdown
Contributor

I have made things!

This is redo for #1270 using TypeVar defaults to implement the change in a non-breaking way.

This is not completely ready, I opening it to get some feedback and because there are still a few issues to address.

The PR currently passes tests but I've tried it on my work codebase and some related issues have resurfaced (not caused by this PR, but by the fact that a lot of places previously ignored are now checked)

Most notably:

  1. ✅ .annotate() in custom queryset method looses type
  2. ✅ I had to do the same re-parametrization trick we do for Manager (cf Reparametrize managers without explicit type parameters #1169) which causes exactly the same kind of issue with overrides of models.Manager.get_queryset which are rather common because documented -> was done in Reparametrize implicit generic QuerySet subclasses #3217
  3. ✅ Also Prefetch's to_attr raises "Model" has no attribute "prefetched_field" #795 was more present -- Fixed in Initial support for to_attr inference in Prefetch calls #2779)

I think this is step in the right direction but I'm a bit afraid it might cause some churn if we don't address the related issues first.

Todos

Related issues

Also fixes a bunch of related issues, I've added regression tests for them:

Comment thread django-stubs/db/models/fields/related_descriptors.pyi
use_in_migrations: bool
name: str
model: type[_T]
_queryset_class: type[_QS]

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I needed to expose this symbol because I use it in the plugin.
If we don't want that, I can also add a type-ignore in the plugin code but I think it's fine to have it because it's a core component of a manager and very unlikely to disappear

Comment thread mypy_django_plugin/transformers/models.py
Comment thread mypy_django_plugin/transformers/managers.py
Comment thread django-stubs/db/models/manager.pyi
@UnknownPlatypus UnknownPlatypus changed the title Managers with generic querysets 3 Manager with generic QuerySet using TypeVar defaults Aug 21, 2025
@UnknownPlatypus
UnknownPlatypus marked this pull request as draft August 27, 2025 07:26
@rtpg

rtpg commented Feb 12, 2026

Copy link
Copy Markdown

@UnknownPlatypus do you want eyes on this? I had issues with annotations that ended up with me writing this test, maybe this branch could end up resolving those too

@UnknownPlatypus

Copy link
Copy Markdown
Contributor Author

@rtpg I plan on rebasing this pr latter this week now that most of the blocking issue mentioned in the description are resolved.

If you have some time, it would be great to resolve the annotations in custom queryset issue. Without that, the current state of this PR will cause a lot of issues for such cases that were previously ignored

I'll integrate the test you proposed in this MR, I think it will work

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants