Skip to content
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

Fix Incompatible return value type error message for long tuple with Union and non-Union type mismatch #18881

Merged
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
2 changes: 1 addition & 1 deletion mypy/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -2439,7 +2439,7 @@ def generate_incompatible_tuple_error(
error_cnt = 0
notes: list[str] = []
for i, (lhs_t, rhs_t) in enumerate(zip(lhs_types, rhs_types)):
if not is_subtype(lhs_t, rhs_t):
if not is_subtype(rhs_t, lhs_t):
if error_cnt < 3:
notes.append(
"Expression tuple item {} has type {}; {} expected; ".format(
Expand Down
77 changes: 77 additions & 0 deletions test-data/unit/check-tuples.test
Original file line number Diff line number Diff line change
Expand Up @@ -1607,6 +1607,83 @@ t6: Tuple[int, int, int, int, int, int, int, int, int, int, int, int] = (1, 2, 3

[builtins fixtures/tuple.pyi]

[case testPropertyLongTupleReturnTypeMismatchUnion]
from typing import Tuple, Union
class A:
a: str
b: str
c: str
d: str
e: str
f: str
g: Union[str, int]
h: Union[str, float]
i: Union[str, None]
j: Union[str, None]
k: Union[str, None]
l: Union[str, None]

@property
def x(self) -> Tuple[str, str, str, str, str, str, str, str, str, str, str, str]:
return (
self.a,
self.b,
self.c,
self.d,
self.e,
self.f,
self.g,
self.h,
self.i,
self.j,
self.k,
self.l,
)
[out]
main:18: error: Incompatible return value type (6 tuple items are incompatible; 3 items are omitted)
main:18: note: Expression tuple item 6 has type "Union[str, int]"; "str" expected;
main:18: note: Expression tuple item 7 has type "Union[str, float]"; "str" expected;
main:18: note: Expression tuple item 8 has type "Optional[str]"; "str" expected;
[builtins fixtures/property.pyi]

[case testPropertyLongTupleReturnTypeMismatchUnionWiderExpected]
from typing import Tuple, Union
class A:
a: str
b: str
c: str
d: str
e: str
f: str
g: str
h: str
i: str
j: str
k: str
l: Union[float, int]

@property
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]:
return (
self.a,
self.b,
self.c,
self.d,
self.e,
self.f,
self.g,
self.h,
self.i,
self.j,
self.k,
self.l,
)
[out]
main:18: error: Incompatible return value type (2 tuple items are incompatible)
main:18: note: Expression tuple item 2 has type "str"; "int" expected;
main:18: note: Expression tuple item 11 has type "Union[float, int]"; "str" expected;
[builtins fixtures/property.pyi]

[case testTupleWithStarExpr]
from typing import Tuple, List
points = (1, "test") # type: Tuple[int, str]
Expand Down