Skip to content

Flatten union before contracting literals when checking subtyping #18898

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
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
5 changes: 4 additions & 1 deletion mypy/subtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
UnionType,
UnpackType,
find_unpack_in_list,
flatten_nested_unions,
get_proper_type,
is_named_instance,
split_with_prefix_and_suffix,
Expand Down Expand Up @@ -327,7 +328,9 @@ def _is_subtype(
and isinstance(left, Instance)
and (left.type.is_enum or left.type.fullname == "builtins.bool")
):
right = UnionType(mypy.typeops.try_contracting_literals_in_union(right.items))
right = UnionType(
mypy.typeops.try_contracting_literals_in_union(flatten_nested_unions(right.items))
)
if proper_subtype:
is_subtype_of_item = any(
is_proper_subtype(orig_left, item, subtype_context=subtype_context)
Expand Down
2 changes: 2 additions & 0 deletions mypy/typeops.py
Original file line number Diff line number Diff line change
Expand Up @@ -1069,6 +1069,8 @@ class Status(Enum):
def try_contracting_literals_in_union(types: Sequence[Type]) -> list[ProperType]:
"""Contracts any literal types back into a sum type if possible.

Requires a flattened union and does not descend into children.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe it's better to add an assert or transparently handle this correctly? Both usages of this in mypy seem fine but I think preventing misuse is good.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I'd rather not - this function is called twice in the whole codebase, one of them in a rather hot function (subtyping check). While failure to pass a flattened union may result in some false positives, it isn't dangerous and can't cause any errors down the road.


Will replace the first instance of the literal with the sum type and
remove all others.

Expand Down
22 changes: 22 additions & 0 deletions test-data/unit/check-literal.test
Original file line number Diff line number Diff line change
Expand Up @@ -2765,6 +2765,28 @@ reveal_type(x) # N: Revealed type is "Literal[__main__.Foo.A]"
reveal_type(y) # N: Revealed type is "Literal[__main__.Foo.A]"
[builtins fixtures/tuple.pyi]

[case testLiteralUnionEnumAliasAssignable]
from enum import Enum
from typing import Literal, Union

class E(Enum):
A = 'a'
B = 'b'
C = 'c'

A = Literal[E.A]
B = Literal[E.B, E.C]

def f(x: Union[A, B]) -> None: ...
def f2(x: Union[A, Literal[E.B, E.C]]) -> None: ...
def f3(x: Union[Literal[E.A], B]) -> None: ...

def main(x: E) -> None:
f(x)
f2(x)
f3(x)
[builtins fixtures/tuple.pyi]

[case testStrictEqualityLiteralTrueVsFalse]
# mypy: strict-equality

Expand Down