Skip to content

Commit e9fa89b

Browse files
authored
Fix error message when returning long tuple with type mismatch (#18881)
1 parent 67b70ce commit e9fa89b

File tree

2 files changed

+78
-1
lines changed

2 files changed

+78
-1
lines changed

mypy/messages.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2439,7 +2439,7 @@ def generate_incompatible_tuple_error(
24392439
error_cnt = 0
24402440
notes: list[str] = []
24412441
for i, (lhs_t, rhs_t) in enumerate(zip(lhs_types, rhs_types)):
2442-
if not is_subtype(lhs_t, rhs_t):
2442+
if not is_subtype(rhs_t, lhs_t):
24432443
if error_cnt < 3:
24442444
notes.append(
24452445
"Expression tuple item {} has type {}; {} expected; ".format(

test-data/unit/check-tuples.test

+77
Original file line numberDiff line numberDiff line change
@@ -1607,6 +1607,83 @@ t6: Tuple[int, int, int, int, int, int, int, int, int, int, int, int] = (1, 2, 3
16071607

16081608
[builtins fixtures/tuple.pyi]
16091609

1610+
[case testPropertyLongTupleReturnTypeMismatchUnion]
1611+
from typing import Tuple, Union
1612+
class A:
1613+
a: str
1614+
b: str
1615+
c: str
1616+
d: str
1617+
e: str
1618+
f: str
1619+
g: Union[str, int]
1620+
h: Union[str, float]
1621+
i: Union[str, None]
1622+
j: Union[str, None]
1623+
k: Union[str, None]
1624+
l: Union[str, None]
1625+
1626+
@property
1627+
def x(self) -> Tuple[str, str, str, str, str, str, str, str, str, str, str, str]:
1628+
return (
1629+
self.a,
1630+
self.b,
1631+
self.c,
1632+
self.d,
1633+
self.e,
1634+
self.f,
1635+
self.g,
1636+
self.h,
1637+
self.i,
1638+
self.j,
1639+
self.k,
1640+
self.l,
1641+
)
1642+
[out]
1643+
main:18: error: Incompatible return value type (6 tuple items are incompatible; 3 items are omitted)
1644+
main:18: note: Expression tuple item 6 has type "Union[str, int]"; "str" expected;
1645+
main:18: note: Expression tuple item 7 has type "Union[str, float]"; "str" expected;
1646+
main:18: note: Expression tuple item 8 has type "Optional[str]"; "str" expected;
1647+
[builtins fixtures/property.pyi]
1648+
1649+
[case testPropertyLongTupleReturnTypeMismatchUnionWiderExpected]
1650+
from typing import Tuple, Union
1651+
class A:
1652+
a: str
1653+
b: str
1654+
c: str
1655+
d: str
1656+
e: str
1657+
f: str
1658+
g: str
1659+
h: str
1660+
i: str
1661+
j: str
1662+
k: str
1663+
l: Union[float, int]
1664+
1665+
@property
1666+
def x(self) -> Tuple[Union[str, int], Union[str, float], int, Union[str, None], Union[str, None], Union[str, None], str, str, str, str, str, str]:
1667+
return (
1668+
self.a,
1669+
self.b,
1670+
self.c,
1671+
self.d,
1672+
self.e,
1673+
self.f,
1674+
self.g,
1675+
self.h,
1676+
self.i,
1677+
self.j,
1678+
self.k,
1679+
self.l,
1680+
)
1681+
[out]
1682+
main:18: error: Incompatible return value type (2 tuple items are incompatible)
1683+
main:18: note: Expression tuple item 2 has type "str"; "int" expected;
1684+
main:18: note: Expression tuple item 11 has type "Union[float, int]"; "str" expected;
1685+
[builtins fixtures/property.pyi]
1686+
16101687
[case testTupleWithStarExpr]
16111688
from typing import Tuple, List
16121689
points = (1, "test") # type: Tuple[int, str]

0 commit comments

Comments
 (0)