Skip to content

Use newer syntax and enable more mypy configuration options#3203

Open
ngnpope wants to merge 13 commits intotypeddjango:masterfrom
ngnpope:ngnpope/more-typing-improvements
Open

Use newer syntax and enable more mypy configuration options#3203
ngnpope wants to merge 13 commits intotypeddjango:masterfrom
ngnpope:ngnpope/more-typing-improvements

Conversation

@ngnpope
Copy link
Member

@ngnpope ngnpope commented Mar 18, 2026

I have made things!

  • Remove use of Union[X, Y] in favour of X | Y
    • We're targeting Python 3.10+, so the old syntax is no longer necessary
  • Use from __future__ import annotations everywhere
    • Aligns closer to the behaviour for Python 3.14 onwards
    • Allows not needing to quote annotations in some cases
    • Allows moving non-runtime imports into type checking block
  • Enable strict_equality_for_none
  • Enable allow_redefinition_new (planned to be default after mypy v2.0)
  • Enable fixed_format_cache (to become default in mypy v1.20)
  • Remove obsolete reportShadowedImports from pyright configuration

ngnpope added 11 commits March 18, 2026 18:24
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.
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
Copy link
Member Author

Choose a reason for hiding this comment

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

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]
Copy link
Member Author

Choose a reason for hiding this comment

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

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]
Copy link
Member Author

Choose a reason for hiding this comment

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

This is another case of the Options.pk one.

else:
related_model_cls = field.field.model

if related_model_cls is None:
Copy link
Contributor

Choose a reason for hiding this comment

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

This case was added in PR #1956 to fix issue #1953 - shouldn't it be kept with # type: ignore[comparison-overlap] instead of removed?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah. Probably. This likely indicates that the stubs are wrong somewhere.

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

Labels

None yet

Development

Successfully merging this pull request may close these issues.

3 participants