Modernize / enforce modern type annotations

Author: The-CompilerCreated Apr 3, 2022Updated Sep 2, 2026
Labelscomponent: style / refactoring

After #6905, we can modernize our type annotations in various ways by doing the following:

  • Use from __future__ import annotations (PEP 563, mypy docs) - initial support for our introspection landed in #5769 already
  • Configure flake8-future-import to enforce it
  • Switch to PEP 585 annotations, i.e. use builtin tuple, dict, list, set etc.
  • Adopt PEP 604, i.e. use | instead of Union and | None instead of Optional

Looks like pyupgrade and isort/reorder_python_imports have some tooling for this: Python Type Hints - How to Upgrade Syntax with pyupgrade - Adam Johnson.

Might also want to adopt typing_extensions at least for Python 3.7, so we can use Protocol, Literal, Final, TypedDict, etc. Maybe over time also for newer Python versions, if we need mroe recent typing features - see included items.