Skip to content

Commit 3b260bf

Browse files
Fix false positives for Y090 in relation to PEP-646 (#473)
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
1 parent 8e3995d commit 3b260bf

File tree

4 files changed

+18
-1
lines changed

4 files changed

+18
-1
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Change Log
22

3+
Bugfixes:
4+
* Y090: Fix false positive for `tuple[Unpack[Ts]]`.
5+
36
## 24.3.0
47

58
New error codes:

pyi.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,7 @@ def _is_object(node: ast.AST | None, name: str, *, from_: Container[str]) -> boo
349349
_is_object, name="AsyncGenerator", from_=_TYPING_OR_COLLECTIONS_ABC
350350
)
351351
_is_Generic = partial(_is_object, name="Generic", from_=_TYPING_MODULES)
352+
_is_Unpack = partial(_is_object, name="Unpack", from_=_TYPING_MODULES)
352353

353354

354355
def _is_object_or_Unused(node: ast.expr | None) -> bool:
@@ -1537,7 +1538,9 @@ def visit_Subscript(self, node: ast.Subscript) -> None:
15371538
self._visit_slice_tuple(node.slice, subscripted_object_name)
15381539
else:
15391540
self.visit(node.slice)
1540-
if subscripted_object_name in {"tuple", "Tuple"}:
1541+
if subscripted_object_name in {"tuple", "Tuple"} and not (
1542+
isinstance(node.slice, ast.Subscript) and _is_Unpack(node.slice.value)
1543+
):
15411544
self._Y090_error(node)
15421545

15431546
def _visit_typing_Literal(self, node: ast.Subscript) -> None:

tests/pep646_py311.pyi

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# flags: --extend-select=Y090
2+
import typing
3+
4+
_Ts = typing.TypeVarTuple("_Ts")
5+
6+
e: tuple[*_Ts]
7+
f: typing.Tuple[*_Ts] # Y022 Use "tuple[Foo, Bar]" instead of "typing.Tuple[Foo, Bar]" (PEP 585 syntax)

tests/single_element_tuples.pyi

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22
import builtins
33
import typing
44

5+
_Ts = typing.TypeVarTuple("_Ts")
6+
57
a: tuple[int] # Y090 "tuple[int]" means "a tuple of length 1, in which the sole element is of type 'int'". Perhaps you meant "tuple[int, ...]"?
68
b: typing.Tuple[builtins.str] # Y022 Use "tuple[Foo, Bar]" instead of "typing.Tuple[Foo, Bar]" (PEP 585 syntax) # Y090 "typing.Tuple[builtins.str]" means "a tuple of length 1, in which the sole element is of type 'builtins.str'". Perhaps you meant "typing.Tuple[builtins.str, ...]"?
79
c: tuple[int, ...]
810
d: typing.Tuple[builtins.str, builtins.complex] # Y022 Use "tuple[Foo, Bar]" instead of "typing.Tuple[Foo, Bar]" (PEP 585 syntax)
11+
e: tuple[typing.Unpack[_Ts]]
12+
f: typing.Tuple[typing.Unpack[_Ts]] # Y022 Use "tuple[Foo, Bar]" instead of "typing.Tuple[Foo, Bar]" (PEP 585 syntax)

0 commit comments

Comments
 (0)