Skip to content

UP045 fixes Optional[None] to None | None #18508

@dscorbett

Description

@dscorbett

Summary

The fix for non-pep604-annotation-optional (UP045) can produce None | None, the persistently inconvenient exception to PEP 604.

$ cat >up045.py <<'# EOF'
from typing import Optional
foo: Optional[None] = None
# EOF

$ ruff --isolated check up045.py --select UP045 --preview --target-version py310 --fix 
Found 1 error (1 fixed, 0 remaining).

$ cat up045.py
from typing import Optional
foo: None | None = None

$ python up045.py 2>&1 | tail -n 1
TypeError: unsupported operand type(s) for |: 'NoneType' and 'NoneType'

Version

ruff 0.11.13 (5faf72a 2025-06-05)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingfixesRelated to suggested fixes for violationshelp wantedContributions especially welcome

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions