Skip to content

Commit

Permalink
Support multiplying tuple with an integer
Browse files Browse the repository at this point in the history
Work towards python#863.
  • Loading branch information
JukkaL authored and JamesGuthrie committed Sep 15, 2015
1 parent ba76080 commit f01a1e6
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 0 deletions.
2 changes: 2 additions & 0 deletions mypy/checkexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -1265,6 +1265,8 @@ def has_member(self, typ: Type, member: str) -> bool:
elif isinstance(typ, UnionType):
result = all(self.has_member(x, member) for x in typ.items)
return result
elif isinstance(typ, TupleType):
return self.has_member(typ.fallback, member)
else:
return False

Expand Down
14 changes: 14 additions & 0 deletions mypy/test/data/pythoneval.test
Original file line number Diff line number Diff line change
Expand Up @@ -1048,3 +1048,17 @@ _program.py: note: In function "p":
_program.py:6: error: "str" not callable
_program.py: note: At top level:
_program.py:8: error: Argument 1 to "startswith" of "str" has incompatible type "Tuple[str, bytes]"; expected "Union[str, Tuple[str, ...]]"

[case testMultiplyTupleByInteger]
n = 4
t = ('',) * n
t + 1
[out]
_program.py:3: error: Unsupported operand types for + (Tuple[str, ...] and "int")

[case testMultiplyTupleByIntegerReverse]
n = 4
t = n * ('',)
t + 1
[out]
_program.py:3: error: Unsupported operand types for + (Tuple[str, ...] and "int")
2 changes: 2 additions & 0 deletions stubs/2.7/builtins.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,8 @@ class tuple(Sequence[_T_co], Generic[_T_co]):
def __gt__(self, x: Tuple[_T_co, ...]) -> bool: ...
def __ge__(self, x: Tuple[_T_co, ...]) -> bool: ...
def __add__(self, x: Tuple[_T_co, ...]) -> Tuple[_T_co, ...]: ...
def __mul__(self, n: int) -> Tuple[_T_co, ...]: ...
def __rmul__(self, n: int) -> Tuple[_T_co, ...]: ...
def count(self, x: Any) -> int: ...
def index(self, x: Any) -> int: ...

Expand Down
2 changes: 2 additions & 0 deletions stubs/3.2/builtins.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,8 @@ class tuple(Sequence[_T_co], Generic[_T_co]):
def __gt__(self, x: Tuple[_T_co, ...]) -> bool: ...
def __ge__(self, x: Tuple[_T_co, ...]) -> bool: ...
def __add__(self, x: Tuple[_T_co, ...]) -> Tuple[_T_co, ...]: ...
def __mul__(self, n: int) -> Tuple[_T_co, ...]: ...
def __rmul__(self, n: int) -> Tuple[_T_co, ...]: ...
def count(self, x: Any) -> int: ...
def index(self, x: Any) -> int: ...

Expand Down

0 comments on commit f01a1e6

Please sign in to comment.