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

Use (simplified) unions instead of joins for tuple fallbacks #17408

Merged
merged 7 commits into from
Jun 22, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 3 additions & 0 deletions mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
SUGGESTED_TEST_FIXTURES,
MessageBuilder,
append_invariance_notes,
append_union_note,
format_type,
format_type_bare,
format_type_distinctly,
Expand Down Expand Up @@ -6814,6 +6815,8 @@ def check_subtype(
)
if isinstance(subtype, Instance) and isinstance(supertype, Instance):
notes = append_invariance_notes(notes, subtype, supertype)
if isinstance(subtype, UnionType) and isinstance(supertype, UnionType):
notes = append_union_note(notes, subtype, supertype, self.options)
if extra_info:
msg = msg.with_additional_msg(" (" + ", ".join(extra_info) + ")")

Expand Down
18 changes: 18 additions & 0 deletions mypy/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
UninhabitedType,
UnionType,
UnpackType,
flatten_nested_unions,
get_proper_type,
get_proper_types,
)
Expand Down Expand Up @@ -3198,6 +3199,23 @@ def append_invariance_notes(
return notes


def append_union_note(
notes: list[str], arg_type: UnionType, expected_type: UnionType, options: Options
) -> list[str]:
"""Point to specific union item(s) that may cause failure in subtype check."""
non_matching = []
items = flatten_nested_unions(arg_type.items)
if len(items) < MAX_UNION_ITEMS:
return notes
for item in items:
if not is_subtype(item, expected_type):
non_matching.append(item)
if non_matching:
types = ", ".join([format_type(typ, options) for typ in non_matching])
notes.append(f"Subtype item{plural_s(types)} that may cause the mismatch: {types}")
return notes


def append_numbers_notes(
notes: list[str], arg_type: Instance, expected_type: Instance
) -> list[str]:
Expand Down
6 changes: 4 additions & 2 deletions test-data/unit/check-unions.test
Original file line number Diff line number Diff line change
Expand Up @@ -1324,7 +1324,8 @@ class C10: ...

x: Union[C1, C2, C3, C4, C5, C6, C7, C8, C9, C10, int]
y: Union[C1, C2, C3, C4, C5, C6, C7, C8, C9, C10, str]
x = y # E: Incompatible types in assignment (expression has type "Union[C1, C2, C3, C4, C5, C6, C7, C8, C9, C10, str]", variable has type "Union[C1, C2, C3, C4, C5, C6, C7, C8, C9, C10, int]")
x = y # E: Incompatible types in assignment (expression has type "Union[C1, C2, C3, C4, C5, C6, C7, C8, C9, C10, str]", variable has type "Union[C1, C2, C3, C4, C5, C6, C7, C8, C9, C10, int]") \
# N: Subtype items that may cause the mismatch: "str"
Copy link
Member

Choose a reason for hiding this comment

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

This is great, and allays my concern. Thanks! Though ideally I think it would be best if we could say something like "items in the first union not in the second" rather than "subtype items", which might not be familiar terminology to all of our users.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, I was thinking about this, but then I thought that it is not technically correct, e.g. Sub| int is still a subtype of Super | int, but now it seems to me I am overthinking it. I will change the message.


[case testLargeUnionsNoneShown]
from typing import Union
Expand All @@ -1343,4 +1344,5 @@ class C11: ...

x: Union[C1, C2, C3, C4, C5, C6, C7, C8, C9, C10, C11]
y: Union[C1, C2, C3, C4, C5, C6, C7, C8, C9, C10, C11, None]
x = y # E: Incompatible types in assignment (expression has type "Union[C1, C2, C3, C4, C5, <6 more items>, None]", variable has type "Union[C1, C2, C3, C4, C5, <6 more items>]")
x = y # E: Incompatible types in assignment (expression has type "Union[C1, C2, C3, C4, C5, <6 more items>, None]", variable has type "Union[C1, C2, C3, C4, C5, <6 more items>]") \
# N: Subtype items that may cause the mismatch: "None"
Loading