Skip to content

Fix 0.750 regression: union (non-)simplification should not affect inference #8077

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 3 commits into from
Dec 4, 2019
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
6 changes: 6 additions & 0 deletions mypy/constraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,12 @@ def _infer_constraints(template: Type, actual: Type,
template = get_proper_type(template)
actual = get_proper_type(actual)

# Type inference shouldn't be affected by whether union types have been simplified.
if isinstance(template, UnionType):
template = mypy.typeops.make_simplified_union(template.items)
if isinstance(actual, UnionType):
actual = mypy.typeops.make_simplified_union(actual.items)

# Ignore Any types from the type suggestion engine to avoid them
# causing us to infer Any in situations where a better job could
# be done otherwise. (This can produce false positives but that
Expand Down
15 changes: 15 additions & 0 deletions test-data/unit/check-inference.test
Original file line number Diff line number Diff line change
Expand Up @@ -2924,3 +2924,18 @@ reveal_type(q2(z)) # N: Revealed type is '__main__.B*'
reveal_type(q1(Z(b))) # N: Revealed type is '__main__.B*'
reveal_type(q2(Z(b))) # N: Revealed type is '__main__.B*'
[builtins fixtures/isinstancelist.pyi]

[case testUnionInvariantSubClassAndCovariantBase]
from typing import Union, Generic, TypeVar

T = TypeVar('T')
T_co = TypeVar('T_co', covariant=True)

class Cov(Generic[T_co]): ...
class Inv(Cov[T]): ...

X = Union[Cov[T], Inv[T]]

def f(x: X[T]) -> T: ...
x: Inv[int]
reveal_type(f(x)) # N: Revealed type is 'builtins.int*'
4 changes: 2 additions & 2 deletions test-data/unit/check-overloading.test
Original file line number Diff line number Diff line change
Expand Up @@ -3646,7 +3646,7 @@ def test_narrow_none() -> None:
a: Optional[int]
if int():
a = narrow_none(a)
reveal_type(a) # N: Revealed type is 'Union[builtins.int, None]'
reveal_type(a) # N: Revealed type is 'builtins.int'
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 a nice improvement too.


b: int
if int():
Expand Down Expand Up @@ -3710,7 +3710,7 @@ def test_narrow_none_v2() -> None:
a: Optional[int]
if int():
a = narrow_none_v2(a)
reveal_type(a) # N: Revealed type is 'Union[builtins.int, None]'
reveal_type(a) # N: Revealed type is 'builtins.int'

b: int
if int():
Expand Down
9 changes: 9 additions & 0 deletions test-data/unit/pythoneval.test
Original file line number Diff line number Diff line change
Expand Up @@ -1490,3 +1490,12 @@ reveal_type(A().b)
[out]
_testNamedTupleAtRunTime.py:5: error: NamedTuple type as an attribute is not supported
_testNamedTupleAtRunTime.py:7: note: Revealed type is 'Any'

[case testAsyncioFutureWait]
# mypy: strict-optional
from asyncio import Future, wait
from typing import List

async def foo() -> None:
f = [] # type: List[Future[None]]
await wait(f)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy