Skip to content

PYI041 fixes None | int | None | float to None | None | float #18298

Open
@dscorbett

Description

@dscorbett

Summary

The fix for redundant-numeric-union (PYI041) can produce None | None, which can raise an exception.

$ cat >pyi041.py <<'# EOF'
def foo(x: None | int | None | float) -> None: ...
# EOF

$ ruff --isolated check pyi041.py --select PYI041 --fix 
Found 1 error (1 fixed, 0 remaining).

$ python pyi041.py 
Traceback (most recent call last):
  File "pyi041.py", line 1, in <module>
    def foo(x: None | None | float) -> None: ...
               ~~~~~^~~~~~
TypeError: unsupported operand type(s) for |: 'NoneType' and 'NoneType'

One possible solution is to move the supertype to the position of the subtype if the subtype preceded it; for example, None | int | None | float would become None | float | None. None | float | None | int would still become None | float | None as it already does in Ruff 0.11.11.

Version

ruff 0.11.11 (0397682 2025-05-22)

Metadata

Metadata

Assignees

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