Skip to content

Fixed length tuple concatenation #7409

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Sep 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions mypy/checkexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -1886,6 +1886,11 @@ def infer_literal_expr_type(self, value: LiteralValue, fallback_name: str) -> Ty
column=typ.column,
))

def concat_tuples(self, left: TupleType, right: TupleType) -> TupleType:
"""Concatenate two fixed length tuples."""
return TupleType(items=left.items + right.items,
fallback=self.named_type('builtins.tuple'))

def visit_int_expr(self, e: IntExpr) -> Type:
"""Type check an integer literal (trivial)."""
return self.infer_literal_expr_type(e.value, 'builtins.int')
Expand Down Expand Up @@ -1945,6 +1950,16 @@ def visit_op_expr(self, e: OpExpr) -> Type:
return self.strfrm_checker.check_str_interpolation(e.left, e.right)
left_type = self.accept(e.left)

proper_left_type = get_proper_type(left_type)
if isinstance(proper_left_type, TupleType) and e.op == '+':
left_add_method = proper_left_type.partial_fallback.type.get('__add__')
if left_add_method and left_add_method.fullname == 'builtins.tuple.__add__':
proper_right_type = get_proper_type(self.accept(e.right))
if isinstance(proper_right_type, TupleType):
right_radd_method = proper_right_type.partial_fallback.type.get('__radd__')
if right_radd_method is None:
return self.concat_tuples(proper_left_type, proper_right_type)

if e.op in nodes.op_methods:
method = self.get_operator_method(e.op)
result, method_type = self.check_op(method, left_type, e.right, e,
Expand Down
10 changes: 9 additions & 1 deletion test-data/unit/check-tuples.test
Original file line number Diff line number Diff line change
Expand Up @@ -738,7 +738,7 @@ class C: pass

a = None # type: A

(a, a) + a # E: Unsupported left operand type for + ("Tuple[A, A]")
(a, a) + a # E: Unsupported operand types for + ("Tuple[A, A]" and "A")
a + (a, a) # E: Unsupported operand types for + ("A" and "Tuple[A, A]")
f((a, a)) # E: Argument 1 to "f" has incompatible type "Tuple[A, A]"; expected "A"
(a, a).foo # E: "Tuple[A, A]" has no attribute "foo"
Expand Down Expand Up @@ -1233,3 +1233,11 @@ reveal_type(tup[2]) # N: Revealed type is 'Union[Any, builtins.int*]' \
reveal_type(tup[:]) # N: Revealed type is 'Union[Tuple[builtins.int, builtins.str], builtins.list[builtins.int*]]'

[builtins fixtures/tuple.pyi]

[case testFixedLengthTupleConcatenation]
a = (1, "foo", 3)
b = ("bar", 7)

reveal_type(a + b) # N: Revealed type is 'Tuple[builtins.int, builtins.str, builtins.int, builtins.str, builtins.int]'

[builtins fixtures/tuple.pyi]
3 changes: 2 additions & 1 deletion test-data/unit/fixtures/tuple.pyi
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Builtins stub used in tuple-related test cases.

from typing import Iterable, Iterator, TypeVar, Generic, Sequence, Any, overload
from typing import Iterable, Iterator, TypeVar, Generic, Sequence, Any, overload, Tuple

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

Expand All @@ -15,6 +15,7 @@ class tuple(Sequence[Tco], Generic[Tco]):
def __contains__(self, item: object) -> bool: pass
def __getitem__(self, x: int) -> Tco: pass
def __rmul__(self, n: int) -> tuple: pass
def __add__(self, x: Tuple[Tco, ...]) -> Tuple[Tco, ...]: pass
def count(self, obj: Any) -> int: pass
class function: pass
class ellipsis: pass
Expand Down