mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-04 18:58:04 +00:00
[flake8-pyi
] Improve autofix safety for redundant-none-literal
(PYI061) (#14583)
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
This commit is contained in:
parent
e8fce20736
commit
c606bf014e
5 changed files with 505 additions and 137 deletions
|
@ -1,4 +1,4 @@
|
|||
from typing import Literal
|
||||
from typing import Literal, Union
|
||||
|
||||
|
||||
def func1(arg1: Literal[None]):
|
||||
|
@ -17,7 +17,7 @@ def func4(arg1: Literal[int, None, float]):
|
|||
...
|
||||
|
||||
|
||||
def func5(arg1: Literal[None, None]):
|
||||
def func5(arg1: Literal[None, None]):
|
||||
...
|
||||
|
||||
|
||||
|
@ -25,13 +25,21 @@ def func6(arg1: Literal[
|
|||
"hello",
|
||||
None # Comment 1
|
||||
, "world"
|
||||
]):
|
||||
]):
|
||||
...
|
||||
|
||||
|
||||
def func7(arg1: Literal[
|
||||
None # Comment 1
|
||||
]):
|
||||
]):
|
||||
...
|
||||
|
||||
|
||||
def func8(arg1: Literal[None] | None):
|
||||
...
|
||||
|
||||
|
||||
def func9(arg1: Union[Literal[None], None]):
|
||||
...
|
||||
|
||||
|
||||
|
@ -58,3 +66,16 @@ Literal[1, None, "foo", None] # Y061 None inside "Literal[]" expression. Replac
|
|||
# and there are no None members in the Literal[] slice,
|
||||
# only emit Y062:
|
||||
Literal[None, True, None, True] # Y062 Duplicate "Literal[]" member "True"
|
||||
|
||||
|
||||
# Regression tests for https://github.com/astral-sh/ruff/issues/14567
|
||||
x: Literal[None] | None
|
||||
y: None | Literal[None]
|
||||
z: Union[Literal[None], None]
|
||||
|
||||
a: int | Literal[None] | None
|
||||
b: None | Literal[None] | None
|
||||
c: (None | Literal[None]) | None
|
||||
d: None | (Literal[None] | None)
|
||||
e: None | ((None | Literal[None]) | None) | None
|
||||
f: Literal[None] | Literal[None]
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
from typing import Literal
|
||||
from typing import Literal, Union
|
||||
|
||||
|
||||
def func1(arg1: Literal[None]): ...
|
||||
|
@ -28,6 +28,12 @@ def func7(arg1: Literal[
|
|||
]): ...
|
||||
|
||||
|
||||
def func8(arg1: Literal[None] | None):...
|
||||
|
||||
|
||||
def func9(arg1: Union[Literal[None], None]): ...
|
||||
|
||||
|
||||
# OK
|
||||
def good_func(arg1: Literal[int] | None): ...
|
||||
|
||||
|
@ -35,3 +41,16 @@ def good_func(arg1: Literal[int] | None): ...
|
|||
# From flake8-pyi
|
||||
Literal[None] # PYI061 None inside "Literal[]" expression. Replace with "None"
|
||||
Literal[True, None] # PYI061 None inside "Literal[]" expression. Replace with "Literal[True] | None"
|
||||
|
||||
|
||||
# Regression tests for https://github.com/astral-sh/ruff/issues/14567
|
||||
x: Literal[None] | None
|
||||
y: None | Literal[None]
|
||||
z: Union[Literal[None], None]
|
||||
|
||||
a: int | Literal[None] | None
|
||||
b: None | Literal[None] | None
|
||||
c: (None | Literal[None]) | None
|
||||
d: None | (Literal[None] | None)
|
||||
e: None | ((None | Literal[None]) | None) | None
|
||||
f: Literal[None] | Literal[None]
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue