mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-04 10:48:32 +00:00
[ruff
] Implement unnecessary-nested-literal
(RUF041
) (#14323)
Co-authored-by: Micha Reiser <micha@reiser.io> Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
This commit is contained in:
parent
187974eff4
commit
11a2929ed7
10 changed files with 770 additions and 1 deletions
31
crates/ruff_linter/resources/test/fixtures/ruff/RUF041.py
vendored
Normal file
31
crates/ruff_linter/resources/test/fixtures/ruff/RUF041.py
vendored
Normal file
|
@ -0,0 +1,31 @@
|
|||
from typing import Literal
|
||||
import typing as t
|
||||
import typing_extensions
|
||||
|
||||
|
||||
y: Literal[1, print("hello"), 3, Literal[4, 1]]
|
||||
Literal[1, Literal[1]]
|
||||
Literal[1, 2, Literal[1, 2]]
|
||||
Literal[1, Literal[1], Literal[1]]
|
||||
Literal[1, Literal[2], Literal[2]]
|
||||
t.Literal[1, t.Literal[2, t.Literal[1]]]
|
||||
Literal[
|
||||
1, # comment 1
|
||||
Literal[ # another comment
|
||||
1 # yet annother comment
|
||||
]
|
||||
] # once
|
||||
|
||||
# Ensure issue is only raised once, even on nested literals
|
||||
MyType = Literal["foo", Literal[True, False, True], "bar"]
|
||||
|
||||
# nested literals, all equivalent to `Literal[1]`
|
||||
Literal[Literal[1]]
|
||||
Literal[Literal[Literal[1], Literal[1]]]
|
||||
Literal[Literal[1], Literal[Literal[Literal[1]]]]
|
||||
|
||||
# OK
|
||||
x: Literal[True, False, True, False]
|
||||
z: Literal[{1, 3, 5}, "foobar", {1,3,5}]
|
||||
typing_extensions.Literal[1, 1, 1]
|
||||
n: Literal["No", "duplicates", "here", 1, "1"]
|
31
crates/ruff_linter/resources/test/fixtures/ruff/RUF041.pyi
vendored
Normal file
31
crates/ruff_linter/resources/test/fixtures/ruff/RUF041.pyi
vendored
Normal file
|
@ -0,0 +1,31 @@
|
|||
from typing import Literal
|
||||
import typing as t
|
||||
import typing_extensions
|
||||
|
||||
|
||||
y: Literal[1, print("hello"), 3, Literal[4, 1]]
|
||||
Literal[1, Literal[1]]
|
||||
Literal[1, 2, Literal[1, 2]]
|
||||
Literal[1, Literal[1], Literal[1]]
|
||||
Literal[1, Literal[2], Literal[2]]
|
||||
t.Literal[1, t.Literal[2, t.Literal[1]]]
|
||||
Literal[
|
||||
1, # comment 1
|
||||
Literal[ # another comment
|
||||
1 # yet annother comment
|
||||
]
|
||||
] # once
|
||||
|
||||
# Ensure issue is only raised once, even on nested literals
|
||||
MyType = Literal["foo", Literal[True, False, True], "bar"]
|
||||
|
||||
# nested literals, all equivalent to `Literal[1]`
|
||||
Literal[Literal[1]]
|
||||
Literal[Literal[Literal[1], Literal[1]]]
|
||||
Literal[Literal[1], Literal[Literal[Literal[1]]]]
|
||||
|
||||
# OK
|
||||
x: Literal[True, False, True, False]
|
||||
z: Literal[{1, 3, 5}, "foobar", {1,3,5}]
|
||||
typing_extensions.Literal[1, 1, 1]
|
||||
n: Literal["No", "duplicates", "here", 1, "1"]
|
Loading…
Add table
Add a link
Reference in a new issue