-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Avoid applying PEP 646 rewrites in invalid contexts (#14234)
## Summary Closes #14231.
- Loading branch information
1 parent
555a5c9
commit 94dee2a
Showing
4 changed files
with
181 additions
and
98 deletions.
There are no files selected for viewing
51 changes: 41 additions & 10 deletions
51
crates/ruff_linter/resources/test/fixtures/pyupgrade/UP044.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,58 @@ | ||
from typing import Generic, TypeVarTuple, Unpack | ||
|
||
Shape = TypeVarTuple('Shape') | ||
Shape = TypeVarTuple("Shape") | ||
|
||
|
||
class C(Generic[Unpack[Shape]]): | ||
pass | ||
|
||
class D(Generic[Unpack [Shape]]): | ||
|
||
class D(Generic[Unpack[Shape]]): | ||
pass | ||
|
||
def f(*args: Unpack[tuple[int, ...]]): pass | ||
|
||
def f(*args: Unpack[other.Type]): pass | ||
def f(*args: Unpack[tuple[int, ...]]): | ||
pass | ||
|
||
|
||
# Not valid unpackings but they are valid syntax | ||
def foo(*args: Unpack[int | str]) -> None: pass | ||
def foo(*args: Unpack[int and str]) -> None: pass | ||
def foo(*args: Unpack[int > str]) -> None: pass | ||
def f(*args: Unpack[other.Type]): | ||
pass | ||
|
||
|
||
def f(*args: Generic[int, Unpack[int]]): | ||
pass | ||
|
||
|
||
# Valid syntax, but can't be unpacked. | ||
def f(*args: Unpack[int | str]) -> None: | ||
pass | ||
|
||
|
||
def f(*args: Unpack[int and str]) -> None: | ||
pass | ||
|
||
|
||
def f(*args: Unpack[int > str]) -> None: | ||
pass | ||
|
||
|
||
# We do not use the shorthand unpacking syntax in the following cases | ||
from typing import TypedDict | ||
|
||
|
||
class KwargsDict(TypedDict): | ||
x: int | ||
y: int | ||
|
||
def foo(name: str, /, **kwargs: Unpack[KwargsDict]) -> None: pass | ||
|
||
# OK | ||
def f(name: str, /, **kwargs: Unpack[KwargsDict]) -> None: | ||
pass | ||
|
||
|
||
# OK | ||
def f() -> object: | ||
return Unpack[tuple[int, ...]] | ||
|
||
|
||
# OK | ||
def f(x: Unpack[int]) -> object: ... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
127 changes: 68 additions & 59 deletions
127
...rules/pyupgrade/snapshots/ruff_linter__rules__pyupgrade__tests__unpack_pep_646_py311.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,83 +1,92 @@ | ||
--- | ||
source: crates/ruff_linter/src/rules/pyupgrade/mod.rs | ||
snapshot_kind: text | ||
--- | ||
UP044.py:5:17: UP044 [*] Use `*` for unpacking | ||
UP044.py:6:17: UP044 [*] Use `*` for unpacking | ||
| | ||
3 | Shape = TypeVarTuple('Shape') | ||
4 | | ||
5 | class C(Generic[Unpack[Shape]]): | ||
6 | class C(Generic[Unpack[Shape]]): | ||
| ^^^^^^^^^^^^^ UP044 | ||
6 | pass | ||
7 | pass | ||
| | ||
= help: Convert to `*` for unpacking | ||
|
||
ℹ Safe fix | ||
2 2 | | ||
3 3 | Shape = TypeVarTuple('Shape') | ||
ℹ Unsafe fix | ||
3 3 | Shape = TypeVarTuple("Shape") | ||
4 4 | | ||
5 |-class C(Generic[Unpack[Shape]]): | ||
5 |+class C(Generic[*Shape]): | ||
6 6 | pass | ||
7 7 | | ||
8 8 | class D(Generic[Unpack [Shape]]): | ||
5 5 | | ||
6 |-class C(Generic[Unpack[Shape]]): | ||
6 |+class C(Generic[*Shape]): | ||
7 7 | pass | ||
8 8 | | ||
9 9 | | ||
|
||
UP044.py:8:17: UP044 [*] Use `*` for unpacking | ||
| | ||
6 | pass | ||
7 | | ||
8 | class D(Generic[Unpack [Shape]]): | ||
| ^^^^^^^^^^^^^^^ UP044 | ||
9 | pass | ||
| | ||
= help: Convert to `*` for unpacking | ||
UP044.py:10:17: UP044 [*] Use `*` for unpacking | ||
| | ||
10 | class D(Generic[Unpack[Shape]]): | ||
| ^^^^^^^^^^^^^ UP044 | ||
11 | pass | ||
| | ||
= help: Convert to `*` for unpacking | ||
|
||
ℹ Safe fix | ||
5 5 | class C(Generic[Unpack[Shape]]): | ||
6 6 | pass | ||
7 7 | | ||
8 |-class D(Generic[Unpack [Shape]]): | ||
8 |+class D(Generic[*Shape]): | ||
9 9 | pass | ||
10 10 | | ||
11 11 | def f(*args: Unpack[tuple[int, ...]]): pass | ||
ℹ Unsafe fix | ||
7 7 | pass | ||
8 8 | | ||
9 9 | | ||
10 |-class D(Generic[Unpack[Shape]]): | ||
10 |+class D(Generic[*Shape]): | ||
11 11 | pass | ||
12 12 | | ||
13 13 | | ||
|
||
UP044.py:11:14: UP044 [*] Use `*` for unpacking | ||
UP044.py:14:14: UP044 [*] Use `*` for unpacking | ||
| | ||
9 | pass | ||
10 | | ||
11 | def f(*args: Unpack[tuple[int, ...]]): pass | ||
14 | def f(*args: Unpack[tuple[int, ...]]): | ||
| ^^^^^^^^^^^^^^^^^^^^^^^ UP044 | ||
12 | | ||
13 | def f(*args: Unpack[other.Type]): pass | ||
15 | pass | ||
| | ||
= help: Convert to `*` for unpacking | ||
|
||
ℹ Safe fix | ||
8 8 | class D(Generic[Unpack [Shape]]): | ||
9 9 | pass | ||
10 10 | | ||
11 |-def f(*args: Unpack[tuple[int, ...]]): pass | ||
11 |+def f(*args: *tuple[int, ...]): pass | ||
ℹ Unsafe fix | ||
11 11 | pass | ||
12 12 | | ||
13 13 | def f(*args: Unpack[other.Type]): pass | ||
14 14 | | ||
13 13 | | ||
14 |-def f(*args: Unpack[tuple[int, ...]]): | ||
14 |+def f(*args: *tuple[int, ...]): | ||
15 15 | pass | ||
16 16 | | ||
17 17 | | ||
|
||
UP044.py:13:14: UP044 [*] Use `*` for unpacking | ||
UP044.py:18:14: UP044 [*] Use `*` for unpacking | ||
| | ||
11 | def f(*args: Unpack[tuple[int, ...]]): pass | ||
12 | | ||
13 | def f(*args: Unpack[other.Type]): pass | ||
18 | def f(*args: Unpack[other.Type]): | ||
| ^^^^^^^^^^^^^^^^^^ UP044 | ||
19 | pass | ||
| | ||
= help: Convert to `*` for unpacking | ||
|
||
ℹ Safe fix | ||
10 10 | | ||
11 11 | def f(*args: Unpack[tuple[int, ...]]): pass | ||
12 12 | | ||
13 |-def f(*args: Unpack[other.Type]): pass | ||
13 |+def f(*args: *other.Type): pass | ||
14 14 | | ||
15 15 | | ||
16 16 | # Not valid unpackings but they are valid syntax | ||
ℹ Unsafe fix | ||
15 15 | pass | ||
16 16 | | ||
17 17 | | ||
18 |-def f(*args: Unpack[other.Type]): | ||
18 |+def f(*args: *other.Type): | ||
19 19 | pass | ||
20 20 | | ||
21 21 | | ||
|
||
UP044.py:22:27: UP044 [*] Use `*` for unpacking | ||
| | ||
22 | def f(*args: Generic[int, Unpack[int]]): | ||
| ^^^^^^^^^^^ UP044 | ||
23 | pass | ||
| | ||
= help: Convert to `*` for unpacking | ||
|
||
ℹ Unsafe fix | ||
19 19 | pass | ||
20 20 | | ||
21 21 | | ||
22 |-def f(*args: Generic[int, Unpack[int]]): | ||
22 |+def f(*args: Generic[int, *int]): | ||
23 23 | pass | ||
24 24 | | ||
25 25 | |