Reparametrize implicit generic QuerySet subclasses#3217
Merged
sobolevn merged 1 commit intoMar 24, 2026
Merged
Conversation
2ceb4e0 to
e771a82
Compare
sobolevn
reviewed
Mar 24, 2026
sobolevn
left a comment
Member
There was a problem hiding this comment.
Please, test this with explicit disallow_any_generics = True, since your docstring mentions this.
When a custom QuerySet subclass is defined without explicit type parameters (e.g. `class MyQS(QuerySet): ...`), make it implicitly generic by copying the parent QuerySet's type variables, analogous to the existing `reparametrize_any_manager_hook` for Manager subclasses. This enables the annotate plugin hook to propagate annotation type information through custom querysets via `copy_modified(args=...)`, which requires the queryset class to have type variables.
e771a82 to
eaa2701
Compare
sobolevn
approved these changes
Mar 24, 2026
Member
|
Thanks a lot! |
Contributor
Author
|
Thank you. Was just about to comment on the new test, got sidetracked. |
Contributor
|
You are too fast ahah, I also did that in #2776 + a small refactor, I'll extract the refactor, it was rather nice. This is what I ended up with: def reparametrize_any_manager_hook(ctx: ClassDefContext) -> None:
"""
Add implicit generics to manager classes that are defined without generic.
Eg.
class MyManager(models.Manager): ...
is interpreted as:
_T = TypeVar("_T", bound=Model, covariant=True)
_QS = TypeVar("_QS", bound=QuerySet[Any], covariant=True, default=QuerySet[_T])
class MyManager(models.Manager[_T, _QS]): ...
"""
reparametrize_generic_class(ctx, fullnames.BASE_MANAGER_CLASS_FULLNAME)
def reparametrize_any_queryset_hook(ctx: ClassDefContext) -> None:
"""
Add implicit generics to queryset classes that are defined without generic.
Eg.
class MyQuerySet(models.QuerySet): ...
is interpreted as:
_T = TypeVar("_T", bound=Model, covariant=True)
class MyQuerySet(models.QuerySet[_T]): ...
"""
reparametrize_generic_class(ctx, fullnames.QUERYSET_CLASS_FULLNAME) |
This was referenced Mar 24, 2026
Contributor
Author
|
Makes sense, I decided not to prematurely abstract without a better understanding of the project, but you have more context than I do. |
Contributor
|
No worries, thanks for the contribution, this is very useful ! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When a custom QuerySet subclass is defined without explicit type parameters (e.g.
class MyQS(QuerySet): ...), make it implicitly generic by copying the parent QuerySet's type variables, analogous to the existingreparametrize_any_manager_hookfor Manager subclasses.This enables the annotate plugin hook to propagate annotation type information through custom querysets via
copy_modified(args=...), which requires the queryset class to have type variables.Disclosure: I've used Claude Opus 4.6 to assist in writing this changeset, but I have carefully iterated and manually reviewed the results.