Skip to content

Missing |None annotation inspection

PyCharm 2026.1 and newer also reports parameters whose default value is None but whose type annotation does not admit None. BetterPy keeps its own inspection for the same code shape so the "BetterPy: Add | None to annotation" quick-fix is available as an inspection quick-fix in the highlighted-code actions.

How to invoke

Place the caret on the highlighted None default or initializer, then use ⌥⏎ / Alt+Enter -> "BetterPy: Add | None to annotation".

Example

def fetch_user(user_id: str, timeout: int = None):
    ...

becomes:

def fetch_user(user_id: str, timeout: int | None = None):
    ...

It also handles annotated assignments with None initializers:

current_user: User = None

becomes:

current_user: User | None = None

What it supports

  • Parameters with None defaults.
  • Annotated assignments initialized with None.
  • Existing union syntax such as Union[T, None] and T | None.
  • Quick-fixing only the annotation while preserving the default expression.

What it deliberately avoids

  • Values that are not actually initialized with None.
  • Unannotated parameters or assignments.
  • Annotations that already allow None.
  • Library and stub files outside your project.

Notes

  • PyCharm 2026.1 and newer has its own diagnostic for this pattern. BetterPy's inspection intentionally overlaps with it so the BetterPy quick-fix appears with the highlighted inspection actions.
  • The quick-fix is gated by the Missing |None annotation inspection setting under Settings | BetterPy | Typing & Type Hints | Code Inspections.