diff --git a/mypy/constraints.py b/mypy/constraints.py index 9da4f04d2277..2a278995e142 100644 --- a/mypy/constraints.py +++ b/mypy/constraints.py @@ -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 diff --git a/test-data/unit/check-inference.test b/test-data/unit/check-inference.test index b2050a127ba6..5366de858018 100644 --- a/test-data/unit/check-inference.test +++ b/test-data/unit/check-inference.test @@ -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*' diff --git a/test-data/unit/check-overloading.test b/test-data/unit/check-overloading.test index 6574cd9e730c..0351111c8fa0 100644 --- a/test-data/unit/check-overloading.test +++ b/test-data/unit/check-overloading.test @@ -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' b: int if int(): @@ -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(): diff --git a/test-data/unit/pythoneval.test b/test-data/unit/pythoneval.test index fad202d50d1e..d6df7ece9899 100644 --- a/test-data/unit/pythoneval.test +++ b/test-data/unit/pythoneval.test @@ -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)
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: