Skip to content

Improve the support for promotions inside unions. #19245

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
16 changes: 12 additions & 4 deletions mypy/meet.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

from itertools import product
from typing import Callable

from mypy import join
Expand Down Expand Up @@ -130,16 +131,23 @@ def narrow_declared_type(declared: Type, narrowed: Type) -> Type:
if isinstance(declared, UnionType):
return make_simplified_union(
[
narrow_declared_type(x, narrowed)
for x in declared.relevant_items()
narrow_declared_type(d, n)
for d, n in product(
Copy link
Collaborator

Choose a reason for hiding this comment

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

This looks like the correct way of doing that, despite higher comp complexity. What's more mypyc-friendly - itertools.product or a plain listcomp with two for clauses?

declared.relevant_items(),
narrowed.relevant_items() if isinstance(narrowed, UnionType) else (narrowed,),
)
# This (ugly) special-casing is needed to support checking
# branches like this:
# x: Union[float, complex]
# if isinstance(x, int):
# ...
# And assignments like this:
# x: float | None
# y: int | None
# x = y
if (
is_overlapping_types(x, narrowed, ignore_promotions=True)
or is_subtype(narrowed, x, ignore_promotions=False)
is_overlapping_types(d, n, ignore_promotions=True)
or is_subtype(n, d, ignore_promotions=False)
)
]
)
Expand Down
33 changes: 33 additions & 0 deletions test-data/unit/check-narrowing.test
Original file line number Diff line number Diff line change
Expand Up @@ -2453,6 +2453,39 @@ while x is not None and b():

[builtins fixtures/primitives.pyi]

[case testNarrowPromotionsInsideUnions1]

from typing import Union

x: Union[str, float, None]
y: Union[int, str]
x = y
reveal_type(x) # N: Revealed type is "Union[builtins.str, builtins.int]"
z: Union[complex, str]
z = x
reveal_type(z) # N: Revealed type is "Union[builtins.int, builtins.str]"

[builtins fixtures/primitives.pyi]

[case testNarrowPromotionsInsideUnions2]
# flags: --warn-unreachable

from typing import Optional

def b() -> bool: ...
def i() -> int: ...
x: Optional[float]

while b():
x = None
while b():
reveal_type(x) # N: Revealed type is "Union[builtins.int, None]"
if x is None or b():
x = i()
reveal_type(x) # N: Revealed type is "builtins.int"

[builtins fixtures/bool.pyi]

[case testNarrowingTypeVarMultiple]
from typing import TypeVar

Expand Down