Skip to content

Commit de732b9

Browse files
committed
Make type of *args argument Sequence instead of list
Closes #261.
1 parent 067e564 commit de732b9

File tree

5 files changed

+21
-7
lines changed

5 files changed

+21
-7
lines changed

mypy/checker.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,8 @@ def check_func_def(self, defn: FuncItem, typ: CallableType, name: str) -> None:
517517
for i in range(len(typ.arg_types)):
518518
arg_type = typ.arg_types[i]
519519
if typ.arg_kinds[i] == nodes.ARG_STAR:
520-
arg_type = self.named_generic_type('builtins.list',
520+
# TODO: This should actually be Tuple[t, ...] once it's supported.
521+
arg_type = self.named_generic_type('typing.Sequence',
521522
[arg_type])
522523
elif typ.arg_kinds[i] == nodes.ARG_STAR2:
523524
arg_type = self.named_generic_type('builtins.dict',

mypy/test/data/check-varargs.test

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66

77

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

mypy/test/data/lib-stub/typing.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,7 @@ def __iter__(self) -> 'Iterator[T]': pass
3232
class Iterator(Iterable[T], Generic[T]):
3333
@abstractmethod
3434
def __next__(self) -> T: pass
35+
36+
class Sequence(Generic[T]):
37+
@abstractmethod
38+
def __getitem__(self, n: Any) -> T: pass

mypy/test/data/pythoneval.test

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -993,3 +993,12 @@ def f() -> Optional[int]: pass
993993
x = f()
994994
y = 1
995995
y = x
996+
997+
[case testAppendToStarArg]
998+
import typing
999+
def f(*x: int) -> None:
1000+
x.append(1)
1001+
f(1)
1002+
[out]
1003+
_program.py: In function "f":
1004+
_program.py, line 3: Sequence[int] has no attribute "append"

mypy/test/testtypes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ def callable(self, vars, *a) -> CallableType:
237237
for v in vars:
238238
tv.append(TypeVarDef(v, n, None, self.fx.o))
239239
n -= 1
240-
return CallableType(a[:-1],
240+
return CallableType(list(a[:-1]),
241241
[ARG_POS] * (len(a) - 1),
242242
[None] * (len(a) - 1),
243243
a[-1],

0 commit comments

Comments
 (0)