Resolve objects through the metaclass and ban manager access on instances - #3593
Open
UnknownPlatypus wants to merge 6 commits into
Open
Resolve objects through the metaclass and ban manager access on instances#3593UnknownPlatypus wants to merge 6 commits into
UnknownPlatypus wants to merge 6 commits into
Conversation
…tances Declaring `objects` on `Model` overrode class-level inference, so `objects = BookManager()` resolved as `Manager[Book]` outside mypy. A `ModelBase.__getattr__` fallback leaves the assignment alone; mypy ignores its `Literal` restriction, so the plugin drops it. Reverse accessors annotated as `x_set: Manager[Book]` must become `RelatedManager[Book]`. ^ Conflicts: ^ django-stubs/db/models/base.pyi ^ django-stubs/db/models/manager.pyi
This comment has been minimized.
This comment has been minimized.
UnknownPlatypus
commented
Aug 13, 2026
Contributor
|
Diff from mypy_primer, showing the effect of this PR on type check results on a corpus of open source code: zulip (https://github.com/zulip/zulip)
+ zerver/models/users.py:696: error: Need type annotation for "objects" [var-annotated]
- zerver/models/users.py:1165: error: Argument 1 to "list" has incompatible type "QuerySet[UserProfile, dict[str, Any]]"; expected "Iterable[RawUserDict]" [arg-type]
+ zerver/models/users.py:1165: error: Argument 1 to "list" has incompatible type "QuerySet[Any, dict[str, Any]]"; expected "Iterable[RawUserDict]" [arg-type]
- zerver/models/users.py:1184: error: Argument 1 to "list" has incompatible type "QuerySet[UserProfile, dict[str, Any]]"; expected "Iterable[RawUserDict]" [arg-type]
+ zerver/models/users.py:1184: error: Argument 1 to "list" has incompatible type "QuerySet[Any, dict[str, Any]]"; expected "Iterable[RawUserDict]" [arg-type]
- zerver/models/users.py:1194: error: Argument 1 to "list" has incompatible type "QuerySet[UserProfile, dict[str, Any]]"; expected "Iterable[RawUserDict]" [arg-type]
+ zerver/models/users.py:1194: error: Argument 1 to "list" has incompatible type "QuerySet[Any, dict[str, Any]]"; expected "Iterable[RawUserDict]" [arg-type]
- zerver/lib/display_recipient.py:67: error: Argument 1 to "list" has incompatible type "QuerySet[UserProfile, dict[str, Any]]"; expected "Iterable[UserDisplayRecipient]" [arg-type]
+ zerver/lib/display_recipient.py:67: error: Argument 1 to "list" has incompatible type "QuerySet[Any, dict[str, Any]]"; expected "Iterable[UserDisplayRecipient]" [arg-type]
- zerver/lib/display_recipient.py:83: error: Argument 1 to "list" has incompatible type "QuerySet[UserProfile, dict[str, Any]]"; expected "Iterable[UserDisplayRecipient]" [arg-type]
+ zerver/lib/display_recipient.py:83: error: Argument 1 to "list" has incompatible type "QuerySet[Any, dict[str, Any]]"; expected "Iterable[UserDisplayRecipient]" [arg-type]
- zerver/actions/users.py:918: error: Incompatible types in assignment (expression has type "QuerySet[UserProfile, dict[str, Any]]", variable has type "list[dict[str, Any]]") [assignment]
+ zerver/actions/users.py:918: error: Incompatible types in assignment (expression has type "QuerySet[Any, dict[str, Any]]", variable has type "list[dict[str, Any]]") [assignment]
- zerver/tests/test_users.py:1846: error: "UserProfile" has no attribute "realm_id"; maybe "realm"? [attr-defined]
- zerver/tests/test_bots.py:639: error: "UserProfile" has no attribute "bot_owner_id" [attr-defined]
- zerver/tests/test_bots.py:2212: error: "UserProfile" has no attribute "bot_owner_id" [attr-defined]
|
UnknownPlatypus
commented
Aug 13, 2026
sobolevn
approved these changes
Aug 13, 2026
sobolevn
left a comment
Member
There was a problem hiding this comment.
Looks solid! Thanks a lot, this is much better now!
Contributor
Author
|
Should we apply a similar pattern to our defined manager ? For ex on the User model we still use classvar |
Member
|
We should unify all type-checker results as much as possible :) |
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.
I have made things!
This fixes two issues around
Model.objects:MyModel().objectswas allowed while it's a runtime error -> I've used explicit__get__(self, instance: None)to solve thatobjects = BookManager()are not overriding our stub-defined type for pyright/pyrefly soobjectswas aManager[Book]and incorrect. -> I think I found a way to have ourobjectsannotation to act as a true fallback using__getattr__on theModelBasemetaclass. By nature getattr is always checked last so now it's correct for every type checker!I had to workaround two mypy issues that made this a bit more difficult but in the end it seem to work:
Related issues
From #3559
Fixes #174
Fixes #1414
Also improves #579
AI Policy