Use newer syntax and enable more mypy configuration options#3203
Open
ngnpope wants to merge 13 commits intotypeddjango:masterfrom
Open
Use newer syntax and enable more mypy configuration options#3203ngnpope wants to merge 13 commits intotypeddjango:masterfrom
ngnpope wants to merge 13 commits intotypeddjango:masterfrom
Conversation
As Python 3.10 is the minimum supported version, we can use the PEP 604 syntax instead of `typing.Union`.
As Python 3.10 is the minimum supported version, we can use the PEP 604 syntax instead of `typing.Union`.
This allows us to postpone evaulation of annotations so that we don't need to manually stringify some annotations. It also makes it possible to avoid importing things at runtime that are only used for type checking. As a bonus, it's closer to the Python 3.14 behavour so, when 3.14 becomes the minimum version, we'll be able to simply remove the future import without any other changes.
This is to become the default at some point beyond mypy v2.0 It will replace the exising `allow_redefinition` in v2.0 See python/mypy#20726
This is to become the default in mypy v1.20.
ngnpope
commented
Mar 18, 2026
Comment on lines
+421
to
424
| if (lookup == "pk" or lookup.startswith("pk__")) and query.get_meta().pk is None: # type: ignore[comparison-overlap] | ||
| # Primary key lookup when no primary key field is found, model is presumably | ||
| # abstract and we can't say anything about 'pk'. | ||
| return None |
Member
Author
There was a problem hiding this comment.
This one is a little tricky because of the change in e3f5b3d. I decided to punt on removing this block for now - we can follow up later.
Comment on lines
+384
to
388
| if queryset_info.fullname is None: # type: ignore[comparison-overlap] | ||
| # In some cases, due to the way the semantic analyzer works, only | ||
| # passed_queryset.name is available. But it should be analyzed again, | ||
| # so this isn't a problem. | ||
| return None # type: ignore[unreachable] |
Member
Author
There was a problem hiding this comment.
Again, not entirely sure here, but likely this block can go too - the existing suppressed unreachable is an extra hint. Looking in mypy/nodes.py, the only fullname that is str | None is on SymbolTableNode.
| # We also need to override existing `pk` definition from `stubs`: | ||
| auto_field = model_cls._meta.pk | ||
| if auto_field is not None: | ||
| if auto_field is not None: # type: ignore[comparison-overlap] |
Member
Author
There was a problem hiding this comment.
This is another case of the Options.pk one.
sobolevn
reviewed
Mar 19, 2026
| else: | ||
| related_model_cls = field.field.model | ||
|
|
||
| if related_model_cls is None: |
Contributor
Member
Author
There was a problem hiding this comment.
Yeah. Probably. This likely indicates that the stubs are wrong somewhere.
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!
Union[X, Y]in favour ofX | Yfrom __future__ import annotationseverywherestrict_equality_for_noneallow_redefinition_new(planned to be default after mypy v2.0)fixed_format_cache(to become default in mypy v1.20)reportShadowedImportsfrom pyright configuration