Skip to content

Commit d7178c0

Browse files
o11cJukkaL
authored andcommitted
Interpret *args as Tuple[T, ...] instead of Sequence[T]
This breaks some buggy code.
1 parent 1a5d982 commit d7178c0

File tree

5 files changed

+24
-13
lines changed

5 files changed

+24
-13
lines changed

lib-python/3.2/test/test_base64.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,8 +229,8 @@ def test_ErrorHeritage(self) -> None:
229229

230230

231231
class TestMain(unittest.TestCase):
232-
def get_output(self, *args: str, **options: Any) -> Any:
233-
args = [sys.executable, '-m', 'base64'] + list(args)
232+
def get_output(self, *args_tuple: str, **options: Any) -> Any:
233+
args = [sys.executable, '-m', 'base64'] + list(args_tuple)
234234
return subprocess.check_output(args, **options)
235235

236236
def test_encode_decode(self) -> None:

mypy/checker.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -511,8 +511,8 @@ def check_func_def(self, defn: FuncItem, typ: CallableType, name: str) -> None:
511511
for i in range(len(typ.arg_types)):
512512
arg_type = typ.arg_types[i]
513513
if typ.arg_kinds[i] == nodes.ARG_STAR:
514-
# TODO: This should actually be Tuple[t, ...] once it's supported.
515-
arg_type = self.named_generic_type('typing.Sequence',
514+
# builtins.tuple[T] is typing.Tuple[T, ...]
515+
arg_type = self.named_generic_type('builtins.tuple',
516516
[arg_type])
517517
elif typ.arg_kinds[i] == nodes.ARG_STAR2:
518518
arg_type = self.named_generic_type('builtins.dict',

mypy/test/data/check-varargs.test

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,33 @@
66

77

88
[case testVarArgsWithinFunction]
9-
from typing import Sequence
9+
from typing import Tuple
1010
def f( *b: 'B') -> None:
11-
ab = None # type: Sequence[B]
12-
ac = None # type: Sequence[C]
13-
b = ac # E: Incompatible types in assignment (expression has type Sequence[C], variable has type Sequence[B])
14-
ac = b # E: Incompatible types in assignment (expression has type Sequence[B], variable has type Sequence[C])
11+
ab = None # type: Tuple[B, ...]
12+
ac = None # type: Tuple[C, ...]
13+
b = ac # E: Incompatible types in assignment (expression has type Tuple[C, ...], variable has type Tuple[B, ...])
14+
ac = b # E: Incompatible types in assignment (expression has type Tuple[B, ...], variable has type Tuple[C, ...])
1515
b = ab
1616
ab = b
1717

1818
class B: pass
1919
class C: pass
20-
[builtins fixtures/list.py]
20+
[builtins fixtures/tuple.py]
2121
[out]
2222
main: note: In function "f":
2323

2424

25+
[case testVarArgsAreTuple]
26+
from typing import Tuple, Sequence
27+
def want_tuple(types: Tuple[type, ...]): pass
28+
def want_sequence(types: Sequence[type]): pass
29+
def test(*t: type) -> None:
30+
want_tuple(t)
31+
want_sequence(t)
32+
[builtins fixtures/tuple.py]
33+
[out]
34+
35+
2536
-- Calling varargs function
2637
-- ------------------------
2738

mypy/test/data/fixtures/tuple.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
# Builtins stub used in tuple-related test cases.
22

3-
from typing import Iterable, TypeVar, Generic
3+
from typing import Iterable, TypeVar, Generic, Sequence
44

55
Tco = TypeVar('Tco', covariant=True)
66

77
class object:
88
def __init__(self): pass
99

1010
class type: pass
11-
class tuple(Generic[Tco]):
11+
class tuple(Sequence[Tco], Generic[Tco]):
1212
def __getitem__(self, x: int) -> Tco: pass
1313
class function: pass
1414

mypy/test/data/pythoneval.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -972,7 +972,7 @@ def f(*x: int) -> None:
972972
f(1)
973973
[out]
974974
_program.py: note: In function "f":
975-
_program.py:3: error: Sequence[int] has no attribute "append"
975+
_program.py:3: error: Tuple[int, ...] has no attribute "append"
976976

977977
[case testExit]
978978
print('a')

0 commit comments

Comments
 (0)