Skip to content

optional-annotation

Pythonstyleno fixdefault: offpython/ruff

Reports a typing.Optional[...] subscript when the target_version has X | Y (3.10 and later).

Optional[X] is a name for X | None, which the newer syntax writes without an import and in the same form as other unions. The name also suggests a parameter that may be left out, which it is not.

To fix, write X | None.

Reported
...
from typing import Optional
x: Optional[int]
...
python:
lint:
rules:
optional-annotation: warn # off (default), warn, error

Enabled by the presets: python/ruff.

Suppress one line with sudsy-disable-next-line optional-annotation in a comment of the file’s language.

The same check in other tools; Sudsy reports what each upstream rule reports.

Tool Rule Match
ruff UP045 full