Resolve queryset annotate types for expressions with static ClassVar#3221
Open
federicobond wants to merge 2 commits intotypeddjango:masterfrom
Open
Resolve queryset annotate types for expressions with static ClassVar#3221federicobond wants to merge 2 commits intotypeddjango:masterfrom
federicobond wants to merge 2 commits intotypeddjango:masterfrom
Conversation
35848bd to
d87fa61
Compare
sobolevn
approved these changes
Mar 25, 2026
Member
sobolevn
left a comment
There was a problem hiding this comment.
This looks super cool! cc @UnknownPlatypus
Contributor
|
I'll check this this week, it looks promising ! |
|
|
||
| # For now, we don't try to resolve the output_field of the field would be, but use Any. | ||
| # Try to resolve the output_field type for each expression. For expressions | ||
| # with a static ClassVar output_field (e.g. Count → IntegerField → int), |
Contributor
There was a problem hiding this comment.
Could we use the return type of the property/cached_property when applicable ?
Contributor
Author
There was a problem hiding this comment.
Not sure what you mean, which property?
Contributor
Author
There was a problem hiding this comment.
I've added resolution via property return type in a separate commit.
Instead of always typing annotated fields as Any, resolve the concrete Python type when the expression has a static ClassVar output_field (e.g. Count → int, Exists → bool, Length → int, Now → datetime).
d87fa61 to
247f89b
Compare
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.
Instead of always typing annotated fields as Any, resolve the concrete Python type when the expression has a static ClassVar output_field (e.g. Count → int, Exists → bool, Length → int, Now → datetime).
The current implementation introduces some unsoundness, since functions like
Lengthcan returnNonefor nullable fields. This is not the case forCountorExists, for example. Settingis_nullable=Truehere is theoretically safer but would make the types wrong in the other direction, and is more annoying in practice.The real solution is to query the Django runtime for the type of the requested field argument, but it introduces quite a bit of complexity, and I'm not sure if it's worth pursuing before agreeing this is a desirable feature.
For now, I'm happy to discuss and gather more feedback.