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 4 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-classes.test
Original file line number Diff line number Diff line change
Expand Up @@ -1665,6 +1665,83 @@ a.f = a.f # E: Property "f" defined in "A" is read-only
a.f.x # E: "int" has no attribute "x"
[builtins fixtures/property.pyi]

[case testPropertyWithTupleUnionAndNonUnionMismatchReturnValueType]
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]:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a testcase with the opposite direction (annotated with tuple of unions, returns tuple of props)

Copy link
Contributor Author

@Luunynliny Luunynliny Apr 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sterliakov Just to make sure I understand, you would like me to test this configuration ?
Or something else ?

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: str

    @property
    def x(self) -> Tuple[Union[str, int], Union[str, float], Union[str, None], 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,
        )

Copy link
Collaborator

@sterliakov sterliakov Apr 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd love to see a test where the expected return type is wider than the actual return type, but the changed method is still triggered. In your example all items are assignable, so that method won't fire. Something like

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: str

    @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,
        )

I adjusted the third union item to not be assignable, so that the comparator should be triggered (please check that it actually is). And why is x a property, won't a simpler test without class do (and same question about the test you already added)?

a: str
b: str
c: str
d: str
e: str
f: str
g: str
h: str
i: str
j: str
k: str
l: str

x: Tuple[
    Union[str, int], Union[str, float], int, Union[str, None], Union[str, None],
    Union[str, None], str, str, str, str, 
    str, str,
] = (
    a, b, c, d, e,
    f, g, h, i, j,
    k, l,
)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And a nit: please move this to check-tuples.test as the long tuple is the problem here, not the surrounding class.

Copy link
Contributor Author

@Luunynliny Luunynliny Apr 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the clarification and the provided example.

I adjusted the third union item to not be assignable, so that the comparator should be triggered (please check that it actually is).

It indeed triggers to relevant error. I also a Union to modified code works as expected.

And why is x a property, won't a simpler test without class do (and same question about the test you already added)?

Beside beging the configuration where I first encountered the error message issue, your suggestion triggers an Incompatible types in assignment (which works correctly) instead of a Incompatible return value type.

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 testPropertyWithTupleUnionAndNonUnionMismatchReturnValueType_wider_expeced_return]
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]

-- Descriptors
-- -----------

Expand Down