Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
dscorbett opened this issue Nov 24, 2024 · 5 comments · Fixed by #14583 or #14589
Closed

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

dscorbett opened this issue Nov 24, 2024 · 5 comments · Fixed by #14583 or #14589
Labels
bug Something isn't working fixes Related to suggested fixes for violations help wanted Contributions especially welcome

Comments

@dscorbett
Copy link

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'
@AlexWaygood
Copy link
Member

Hah, great catch (as ever). For reference, the CPython issue where it's debated whether we should change this behaviour at runtime in Python is python/cpython#107271.

Regardless of whether we change it in a future version of CPython, however, this is clearly a bug in ruff, since the code does not raise an exception prior to the autofix but does afterwards.

@AlexWaygood AlexWaygood added bug Something isn't working fixes Related to suggested fixes for violations help wanted Contributions especially welcome labels Nov 24, 2024
@MichaReiser
Copy link
Member

CC: @sbrugman (there's no expectation that you fix the issue, I just thought that you might be interested knowing about it)

@sbrugman
Copy link
Contributor

Nice catch. I'll look into it.

@AlexWaygood
Copy link
Member

AlexWaygood commented Nov 25, 2024

The frustrating thing here is that if you have PYI016 also enabled, then this isn't an issue. But of course we can't mandate that users select rules in tandem like that.

@sbrugman
Copy link
Contributor

I'm exploring to fix this in a single go: if the parent union already contains the None literal, then we can change the edit to range_deletion instead of range_replacement.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working fixes Related to suggested fixes for violations help wanted Contributions especially welcome
Projects
None yet
4 participants