What it does #
The rule finds type hints written with the old generic types of the typing module:
from typing import Dict, List, Optional
def load(ids: Optional[List[str]]) -> Dict[str, int]: ...
Modern Python has a shorter syntax for the same thing:
def load(ids: list[str] | None) -> dict[str, int]: ...
Nothing changes in how the code runs. The code becomes shorter and easier to read, and the issues disappear from your checks and scans.
When you’d use it #
Use it when the same rule keeps coming back across your code. Your weekly email suggests it when this rule caused at least a quarter of the issues found in the week.
How to use it #
- Create a branch for this change only, so the review stays easy.
-
Give your assistant this instruction:
Modernise the type hints in this repository, in every .py file: - List[X] → list[X], Dict[K, V] → dict[K, V], Tuple[...] → tuple[...], Set[X] → set[X], FrozenSet[X] → frozenset[X]. - Optional[X] → X | None, and Union[A, B] → A | B. - Remove the imports from typing that are no longer used. Do not change anything else: no renames, no logic changes, no formatting of untouched lines. When you have finished, run Norma's live_check on every changed file, then send a task report with register_applied_actions. - Review the pull request. It touches many files, but every change follows the same pattern.
If your project uses Ruff, it can make the same change in one command. Then ask your assistant to run live_check on the changed files and send a task report:
ruff check --select UP006,UP007,UP035,UP045 --fix .
On older Ruff versions that do not know UP045, leave it out: UP007 covers Optional there.
Limits & notes #
list[str]needs Python 3.9 or newer, andX | Noneneeds Python 3.10 or newer. On older versions, addfrom __future__ import annotationsas the first import of each file, or keep the old syntax.- The next weekly email shows Last week’s advice worked under the recommendation when this rule’s share of the issues falls.
Frequently asked questions #
Is the change safe?
Yes, on a supported Python version. The new syntax means exactly the same thing to Python and to type checkers.
Why fix them all at once?
Fixing them one at a time costs your team a little time on every task. One change on its own branch costs one review.
Related #
- Ask your assistant to send a task report — record the fixes.
- Live Check — confirm the changed files are clean.