Skip to content

Commit

Permalink
[flake8-pyi, ruff] Fix traversal of nested literals and unions (`…
Browse files Browse the repository at this point in the history
…PYI016`, `PYI051`, `PYI055`, `PYI062`, `RUF041`) (#14641)
  • Loading branch information
sbrugman authored Nov 28, 2024
1 parent d9cbf2f commit dc29f52
Show file tree
Hide file tree
Showing 16 changed files with 319 additions and 170 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,6 @@ def func2() -> str | str: # PYI016: Duplicate union member `str`

# Test case for mixed union type fix
field33: typing.Union[typing.Union[int | int] | typing.Union[int | int]] # Error

# Test case for mixed union type
field34: typing.Union[list[int], str] | typing.Union[bytes, list[int]] # Error
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,7 @@ field31: typing.Union[float, typing.Union[int | int]] # Error
field32: typing.Union[float, typing.Union[int | int | int]] # Error

# Test case for mixed union type fix
field33: typing.Union[typing.Union[int | int] | typing.Union[int | int]] # Error
field33: typing.Union[typing.Union[int | int] | typing.Union[int | int]] # Error

# Test case for mixed union type
field34: typing.Union[list[int], str] | typing.Union[bytes, list[int]] # Error
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,16 @@ PYI016.py:89:41: PYI016 Duplicate union member `int`
|
= help: Remove duplicate union member `int`

PYI016.py:92:54: PYI016 Duplicate union member `int`
|
91 | # Should emit in cases with nested `typing.Union`
92 | field27: typing.Union[typing.Union[typing.Union[int, int]]] # PYI016: Duplicate union member `int`
| ^^^ PYI016
93 |
94 | # Should emit in cases with mixed `typing.Union` and `|`
|
= help: Remove duplicate union member `int`

PYI016.py:95:29: PYI016 Duplicate union member `int`
|
94 | # Should emit in cases with mixed `typing.Union` and `|`
Expand All @@ -340,6 +350,16 @@ PYI016.py:95:29: PYI016 Duplicate union member `int`
|
= help: Remove duplicate union member `int`

PYI016.py:98:54: PYI016 Duplicate union member `int`
|
97 | # Should emit twice in cases with multiple nested `typing.Union`
98 | field29: typing.Union[int, typing.Union[typing.Union[int, int]]] # Error
| ^^^ PYI016
99 |
100 | # Should emit once in cases with multiple nested `typing.Union`
|
= help: Remove duplicate union member `int`

PYI016.py:98:59: PYI016 Duplicate union member `int`
|
97 | # Should emit twice in cases with multiple nested `typing.Union`
Expand All @@ -350,6 +370,16 @@ PYI016.py:98:59: PYI016 Duplicate union member `int`
|
= help: Remove duplicate union member `int`

PYI016.py:101:54: PYI016 Duplicate union member `int`
|
100 | # Should emit once in cases with multiple nested `typing.Union`
101 | field30: typing.Union[int, typing.Union[typing.Union[int, str]]] # Error
| ^^^ PYI016
102 |
103 | # Should emit once, and fix to `typing.Union[float, int]`
|
= help: Remove duplicate union member `int`

PYI016.py:104:49: PYI016 Duplicate union member `int`
|
103 | # Should emit once, and fix to `typing.Union[float, int]`
Expand Down Expand Up @@ -385,21 +415,35 @@ PYI016.py:110:42: PYI016 Duplicate union member `int`
109 | # Test case for mixed union type fix
110 | field33: typing.Union[typing.Union[int | int] | typing.Union[int | int]] # Error
| ^^^ PYI016
111 |
112 | # Test case for mixed union type
|
= help: Remove duplicate union member `int`

PYI016.py:110:49: PYI016 Duplicate union member `typing.Union[int | int]`
PYI016.py:110:62: PYI016 Duplicate union member `int`
|
109 | # Test case for mixed union type fix
110 | field33: typing.Union[typing.Union[int | int] | typing.Union[int | int]] # Error
| ^^^^^^^^^^^^^^^^^^^^^^^ PYI016
| ^^^ PYI016
111 |
112 | # Test case for mixed union type
|
= help: Remove duplicate union member `typing.Union[int | int]`
= help: Remove duplicate union member `int`

PYI016.py:110:68: PYI016 Duplicate union member `int`
|
109 | # Test case for mixed union type fix
110 | field33: typing.Union[typing.Union[int | int] | typing.Union[int | int]] # Error
| ^^^ PYI016
111 |
112 | # Test case for mixed union type
|
= help: Remove duplicate union member `int`

PYI016.py:113:61: PYI016 Duplicate union member `list[int]`
|
112 | # Test case for mixed union type
113 | field34: typing.Union[list[int], str] | typing.Union[bytes, list[int]] # Error
| ^^^^^^^^^ PYI016
|
= help: Remove duplicate union member `list[int]`
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,16 @@ PYI016.pyi:89:41: PYI016 Duplicate union member `int`
|
= help: Remove duplicate union member `int`

PYI016.pyi:92:54: PYI016 Duplicate union member `int`
|
91 | # Should emit in cases with nested `typing.Union`
92 | field27: typing.Union[typing.Union[typing.Union[int, int]]] # PYI016: Duplicate union member `int`
| ^^^ PYI016
93 |
94 | # Should emit in cases with mixed `typing.Union` and `|`
|
= help: Remove duplicate union member `int`

PYI016.pyi:95:29: PYI016 Duplicate union member `int`
|
94 | # Should emit in cases with mixed `typing.Union` and `|`
Expand All @@ -340,6 +350,16 @@ PYI016.pyi:95:29: PYI016 Duplicate union member `int`
|
= help: Remove duplicate union member `int`

PYI016.pyi:98:54: PYI016 Duplicate union member `int`
|
97 | # Should emit twice in cases with multiple nested `typing.Union`
98 | field29: typing.Union[int, typing.Union[typing.Union[int, int]]] # Error
| ^^^ PYI016
99 |
100 | # Should emit once in cases with multiple nested `typing.Union`
|
= help: Remove duplicate union member `int`

PYI016.pyi:98:59: PYI016 Duplicate union member `int`
|
97 | # Should emit twice in cases with multiple nested `typing.Union`
Expand All @@ -350,6 +370,16 @@ PYI016.pyi:98:59: PYI016 Duplicate union member `int`
|
= help: Remove duplicate union member `int`

PYI016.pyi:101:54: PYI016 Duplicate union member `int`
|
100 | # Should emit once in cases with multiple nested `typing.Union`
101 | field30: typing.Union[int, typing.Union[typing.Union[int, str]]] # Error
| ^^^ PYI016
102 |
103 | # Should emit once, and fix to `typing.Union[float, int]`
|
= help: Remove duplicate union member `int`

PYI016.pyi:104:49: PYI016 Duplicate union member `int`
|
103 | # Should emit once, and fix to `typing.Union[float, int]`
Expand Down Expand Up @@ -385,21 +415,35 @@ PYI016.pyi:110:42: PYI016 Duplicate union member `int`
109 | # Test case for mixed union type fix
110 | field33: typing.Union[typing.Union[int | int] | typing.Union[int | int]] # Error
| ^^^ PYI016
111 |
112 | # Test case for mixed union type
|
= help: Remove duplicate union member `int`

PYI016.pyi:110:49: PYI016 Duplicate union member `typing.Union[int | int]`
PYI016.pyi:110:62: PYI016 Duplicate union member `int`
|
109 | # Test case for mixed union type fix
110 | field33: typing.Union[typing.Union[int | int] | typing.Union[int | int]] # Error
| ^^^^^^^^^^^^^^^^^^^^^^^ PYI016
| ^^^ PYI016
111 |
112 | # Test case for mixed union type
|
= help: Remove duplicate union member `typing.Union[int | int]`
= help: Remove duplicate union member `int`

PYI016.pyi:110:68: PYI016 Duplicate union member `int`
|
109 | # Test case for mixed union type fix
110 | field33: typing.Union[typing.Union[int | int] | typing.Union[int | int]] # Error
| ^^^ PYI016
111 |
112 | # Test case for mixed union type
|
= help: Remove duplicate union member `int`

PYI016.pyi:113:61: PYI016 Duplicate union member `list[int]`
|
112 | # Test case for mixed union type
113 | field34: typing.Union[list[int], str] | typing.Union[bytes, list[int]] # Error
| ^^^^^^^^^ PYI016
|
= help: Remove duplicate union member `list[int]`
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ PYI041.py:30:28: PYI041 Use `float` instead of `int | float`
|
= help: Remove redundant type

PYI041.py:34:26: PYI041 Use `float` instead of `int | float`
|
34 | def f3(arg1: int, *args: Union[int | int | float]) -> None:
| ^^^^^^^^^^^^^^^^^^^^^^^^ PYI041
35 | ...
|
= help: Remove redundant type

PYI041.py:38:24: PYI041 Use `float` instead of `int | float`
|
38 | async def f4(**kwargs: int | int | float) -> None:
Expand All @@ -41,6 +49,30 @@ PYI041.py:42:26: PYI041 Use `float` instead of `int | float`
|
= help: Remove redundant type

PYI041.py:46:26: PYI041 Use `float` instead of `int | float`
|
46 | def f6(arg1: int, *args: Union[Union[int, int, float]]) -> None:
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI041
47 | ...
|
= help: Remove redundant type

PYI041.py:50:26: PYI041 Use `float` instead of `int | float`
|
50 | def f7(arg1: int, *args: Union[Union[Union[int, int, float]]]) -> None:
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI041
51 | ...
|
= help: Remove redundant type

PYI041.py:54:26: PYI041 Use `float` instead of `int | float`
|
54 | def f8(arg1: int, *args: Union[Union[Union[int | int | float]]]) -> None:
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI041
55 | ...
|
= help: Remove redundant type

PYI041.py:59:10: PYI041 Use `complex` instead of `int | float | complex`
|
58 | def f9(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ PYI041.pyi:27:28: PYI041 Use `float` instead of `int | float`
|
= help: Remove redundant type

PYI041.pyi:30:26: PYI041 Use `float` instead of `int | float`
|
30 | def f3(arg1: int, *args: Union[int | int | float]) -> None: ... # PYI041
| ^^^^^^^^^^^^^^^^^^^^^^^^ PYI041
|
= help: Remove redundant type

PYI041.pyi:33:24: PYI041 Use `float` instead of `int | float`
|
33 | async def f4(**kwargs: int | int | float) -> None: ... # PYI041
Expand Down Expand Up @@ -66,6 +73,27 @@ PYI041.pyi:49:26: PYI041 Use `float` instead of `int | float`
|
= help: Remove redundant type

PYI041.pyi:52:26: PYI041 Use `float` instead of `int | float`
|
52 | def f6(arg1: int, *args: Union[Union[int, int, float]]) -> None: ... # PYI041
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI041
|
= help: Remove redundant type

PYI041.pyi:55:26: PYI041 Use `float` instead of `int | float`
|
55 | def f7(arg1: int, *args: Union[Union[Union[int, int, float]]]) -> None: ... # PYI041
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI041
|
= help: Remove redundant type

PYI041.pyi:58:26: PYI041 Use `float` instead of `int | float`
|
58 | def f8(arg1: int, *args: Union[Union[Union[int | int | float]]]) -> None: ... # PYI041
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ PYI041
|
= help: Remove redundant type

PYI041.pyi:64:24: PYI041 Use `complex` instead of `int | float | complex`
|
62 | def good(self, arg: int) -> None: ...
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,35 @@ PYI051.py:7:51: PYI051 `Literal[42]` is redundant in a union with `int`
9 | F: TypeAlias = typing.Union[str, typing.Union[typing.Union[typing.Union[Literal["foo"], int]]]]
|

PYI051.py:8:76: PYI051 `Literal["foo"]` is redundant in a union with `str`
|
6 | C: TypeAlias = typing.Union[Literal[5], int, typing.Union[Literal["foo"], str]]
7 | D: TypeAlias = typing.Union[Literal[b"str_bytes", 42], bytes, int]
8 | E: TypeAlias = typing.Union[typing.Union[typing.Union[typing.Union[Literal["foo"], str]]]]
| ^^^^^ PYI051
9 | F: TypeAlias = typing.Union[str, typing.Union[typing.Union[typing.Union[Literal["foo"], int]]]]
10 | G: typing.Union[str, typing.Union[typing.Union[typing.Union[Literal["foo"], int]]]]
|

PYI051.py:9:81: PYI051 `Literal["foo"]` is redundant in a union with `str`
|
7 | D: TypeAlias = typing.Union[Literal[b"str_bytes", 42], bytes, int]
8 | E: TypeAlias = typing.Union[typing.Union[typing.Union[typing.Union[Literal["foo"], str]]]]
9 | F: TypeAlias = typing.Union[str, typing.Union[typing.Union[typing.Union[Literal["foo"], int]]]]
| ^^^^^ PYI051
10 | G: typing.Union[str, typing.Union[typing.Union[typing.Union[Literal["foo"], int]]]]
|

PYI051.py:10:69: PYI051 `Literal["foo"]` is redundant in a union with `str`
|
8 | E: TypeAlias = typing.Union[typing.Union[typing.Union[typing.Union[Literal["foo"], str]]]]
9 | F: TypeAlias = typing.Union[str, typing.Union[typing.Union[typing.Union[Literal["foo"], int]]]]
10 | G: typing.Union[str, typing.Union[typing.Union[typing.Union[Literal["foo"], int]]]]
| ^^^^^ PYI051
11 |
12 | def func(x: complex | Literal[1J], y: Union[Literal[3.14], float]): ...
|

PYI051.py:12:31: PYI051 `Literal[1J]` is redundant in a union with `complex`
|
10 | G: typing.Union[str, typing.Union[typing.Union[typing.Union[Literal["foo"], int]]]]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,35 @@ PYI051.pyi:7:51: PYI051 `Literal[42]` is redundant in a union with `int`
9 | F: TypeAlias = typing.Union[str, typing.Union[typing.Union[typing.Union[Literal["foo"], int]]]]
|

PYI051.pyi:8:76: PYI051 `Literal["foo"]` is redundant in a union with `str`
|
6 | C: TypeAlias = typing.Union[Literal[5], int, typing.Union[Literal["foo"], str]]
7 | D: TypeAlias = typing.Union[Literal[b"str_bytes", 42], bytes, int]
8 | E: TypeAlias = typing.Union[typing.Union[typing.Union[typing.Union[Literal["foo"], str]]]]
| ^^^^^ PYI051
9 | F: TypeAlias = typing.Union[str, typing.Union[typing.Union[typing.Union[Literal["foo"], int]]]]
10 | G: typing.Union[str, typing.Union[typing.Union[typing.Union[Literal["foo"], int]]]]
|

PYI051.pyi:9:81: PYI051 `Literal["foo"]` is redundant in a union with `str`
|
7 | D: TypeAlias = typing.Union[Literal[b"str_bytes", 42], bytes, int]
8 | E: TypeAlias = typing.Union[typing.Union[typing.Union[typing.Union[Literal["foo"], str]]]]
9 | F: TypeAlias = typing.Union[str, typing.Union[typing.Union[typing.Union[Literal["foo"], int]]]]
| ^^^^^ PYI051
10 | G: typing.Union[str, typing.Union[typing.Union[typing.Union[Literal["foo"], int]]]]
|

PYI051.pyi:10:69: PYI051 `Literal["foo"]` is redundant in a union with `str`
|
8 | E: TypeAlias = typing.Union[typing.Union[typing.Union[typing.Union[Literal["foo"], str]]]]
9 | F: TypeAlias = typing.Union[str, typing.Union[typing.Union[typing.Union[Literal["foo"], int]]]]
10 | G: typing.Union[str, typing.Union[typing.Union[typing.Union[Literal["foo"], int]]]]
| ^^^^^ PYI051
11 |
12 | def func(x: complex | Literal[1J], y: Union[Literal[3.14], float]): ...
|

PYI051.pyi:12:31: PYI051 `Literal[1J]` is redundant in a union with `complex`
|
10 | G: typing.Union[str, typing.Union[typing.Union[typing.Union[Literal["foo"], int]]]]
Expand Down
Loading

0 comments on commit dc29f52

Please sign in to comment.