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

redundant-none-literal (PYI061): Unsafe or different autofix for Python 3.9 #15795

Open
Avasam opened this issue Jan 28, 2025 · 0 comments
Open
Assignees
Labels
bug Something isn't working fixes Related to suggested fixes for violations

Comments

@Avasam
Copy link
Contributor

Avasam commented Jan 28, 2025

Description

In #14872, more autofixes were added for redundant-none-literal (PYI061), but it only applies to Python 3.10+ as the | may not be usable in annotations in <=3.9 in all cases, even with from __future__ import annotations.

I am requesting to either allow the fix on <=3.9 as an "unsafe" autofix.
OR to autofix using typing.Union. Leaving the Union --> | transformation logic to other (UP?) rules.

I also think this fix should always be available using | in type stubs (.pyi files)

Current vs expected

The following code:

def get_rotation(rotation: float | Literal[None, "horizontal", "vertical"]) -> float: ...

With the following command:
ruff check --fix --unsafe-fixes --preview --select=PYI061 --isolated --target-version=py39

In Ruff 0.9.3:

stubs\matplotlib\text.pyi:16:44: PYI061 `Literal[None, ...]` can be replaced with `Literal[...] | None`
   |
14 | from .transforms import Bbox, Transform
15 |
16 | def get_rotation(rotation: float | Literal[None, "horizontal", "vertical"]) -> float: ...
   |                                            ^^^^ PYI061
17 |
18 | class Text(Artist):
   |
   = help: Replace with `Literal[...] | None`

Found 1 error.

I expect:

Found 1 error (1 fixed, 0 remaining).

Unsafely autofixed to:

def get_rotation(rotation: float | Literal["horizontal", "vertical"] | None) -> float: ...

Or safely autofixed to:

def get_rotation(rotation: float | Union[Literal["horizontal", "vertical"], None]) -> float: ...
@AlexWaygood AlexWaygood added bug Something isn't working fixes Related to suggested fixes for violations labels Jan 28, 2025
@dylwil3 dylwil3 self-assigned this Feb 3, 2025
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
Projects
None yet
Development

No branches or pull requests

3 participants