Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Fixes for PYI061 and RUF020 can generate None | None #14567

Description

@dscorbett

The fixes for redundant-none-literal (PYI061) and never-union (RUF020) in Ruff 0.8.0 can generate None | None, which raises an error at runtime.

PYI061 example:

$ cat pyi061.py
from typing import Literal
x: Literal[None] | None

$ ruff check --isolated --preview --select PYI061 pyi061.py --fix
Found 1 error (1 fixed, 0 remaining).

$ cat pyi061.py
from typing import Literal
x: None | None

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

RUF020 example:

$ cat ruf020.py
from typing import Never
x: None | Never | None

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

$ cat ruf020.py
from typing import Never
x: None | None

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

Metadata

Metadata

Assignees

No one assigned

    Labels

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

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions