Skip to content

Fix joining of fixed-length tuples with mismatching lengths #8333

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 1 commit into from
Feb 3, 2020
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
13 changes: 9 additions & 4 deletions mypy/join.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,20 +265,25 @@ def visit_tuple_type(self, t: TupleType) -> ProperType:
# When given two fixed-length tuples:
# * If they have the same length, join their subtypes item-wise:
# Tuple[int, bool] + Tuple[bool, bool] becomes Tuple[int, bool]
# * If lengths do not match, return a variadic tuple:
# Tuple[bool, int] + Tuple[bool] becomes Tuple[int, ...]
#
# Otherwise, `t` is a fixed-length tuple but `self.s` is NOT:
# * Joining with a variadic tuple returns variadic tuple:
# Tuple[int, bool] + Tuple[bool, ...] becomes Tuple[int, ...]
# * Joining with any Sequence also returns a Sequence:
# Tuple[int, bool] + List[bool] becomes Sequence[int]
if isinstance(self.s, TupleType) and self.s.length() == t.length():
items = [] # type: List[Type]
for i in range(t.length()):
items.append(self.join(t.items[i], self.s.items[i]))
fallback = join_instances(mypy.typeops.tuple_fallback(self.s),
mypy.typeops.tuple_fallback(t))
assert isinstance(fallback, Instance)
return TupleType(items, fallback)
if self.s.length() == t.length():
items = [] # type: List[Type]
for i in range(t.length()):
items.append(self.join(t.items[i], self.s.items[i]))
return TupleType(items, fallback)
else:
return fallback
else:
return join_types(self.s, mypy.typeops.tuple_fallback(t))

Expand Down
6 changes: 6 additions & 0 deletions mypy/test/testtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,12 @@ def test_tuples(self) -> None:
self.assert_join(self.tuple(self.fx.a),
self.tuple(self.fx.a, self.fx.a),
self.var_tuple(self.fx.a))
self.assert_join(self.tuple(self.fx.b),
self.tuple(self.fx.a, self.fx.c),
self.var_tuple(self.fx.a))
self.assert_join(self.tuple(),
self.tuple(self.fx.a),
self.var_tuple(self.fx.a))

def test_var_tuples(self) -> None:
self.assert_join(self.tuple(self.fx.a),
Expand Down
46 changes: 46 additions & 0 deletions test-data/unit/check-tuples.test
Original file line number Diff line number Diff line change
Expand Up @@ -1158,6 +1158,52 @@ reveal_type(subtup if int() else vartup) # N: Revealed type is 'builtins.tuple[
[builtins fixtures/tuple.pyi]
[out]

[case testTupleJoinIrregular]
from typing import Tuple

tup1 = None # type: Tuple[bool, int]
tup2 = None # type: Tuple[bool]

reveal_type(tup1 if int() else tup2) # N: Revealed type is 'builtins.tuple[builtins.int]'
reveal_type(tup2 if int() else tup1) # N: Revealed type is 'builtins.tuple[builtins.int]'

reveal_type(tup1 if int() else ()) # N: Revealed type is 'builtins.tuple[builtins.int]'
reveal_type(() if int() else tup1) # N: Revealed type is 'builtins.tuple[builtins.int]'

reveal_type(tup2 if int() else ()) # N: Revealed type is 'builtins.tuple[builtins.bool]'
reveal_type(() if int() else tup2) # N: Revealed type is 'builtins.tuple[builtins.bool]'

[builtins fixtures/tuple.pyi]
[out]

[case testTupleSubclassJoinIrregular]
from typing import Tuple, NamedTuple

class NTup1(NamedTuple):
a: bool

class NTup2(NamedTuple):
a: bool
b: bool

class SubTuple(Tuple[bool, int, int]): ...

tup1 = None # type: NTup1
tup2 = None # type: NTup2
subtup = None # type: SubTuple

reveal_type(tup1 if int() else tup2) # N: Revealed type is 'builtins.tuple[builtins.bool]'
reveal_type(tup2 if int() else tup1) # N: Revealed type is 'builtins.tuple[builtins.bool]'

reveal_type(tup1 if int() else subtup) # N: Revealed type is 'builtins.tuple[builtins.int]'
reveal_type(subtup if int() else tup1) # N: Revealed type is 'builtins.tuple[builtins.int]'

reveal_type(tup2 if int() else subtup) # N: Revealed type is 'builtins.tuple[builtins.int]'
reveal_type(subtup if int() else tup2) # N: Revealed type is 'builtins.tuple[builtins.int]'

[builtins fixtures/tuple.pyi]
[out]

[case testTupleWithUndersizedContext]
a = ([1], 'x')
if int():
Expand Down