From 831e45c6975a2d132ffbe2bde30697049a8184a8 Mon Sep 17 00:00:00 2001 From: Ivan Levkivskyi Date: Wed, 4 Dec 2019 16:50:25 +0000 Subject: [PATCH 1/3] Fix 0.750 regression: union (non-)simplification should nor affect inference --- mypy/constraints.py | 6 ++++++ test-data/unit/check-inference.test | 15 +++++++++++++++ test-data/unit/check-overloading.test | 4 ++-- test-data/unit/pythoneval.test | 9 +++++++++ 4 files changed, 32 insertions(+), 2 deletions(-) diff --git a/mypy/constraints.py b/mypy/constraints.py index 9da4f04d2277..fdb1ab0d74d4 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 bee 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..00605b453449 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: List[Future[None]] = [] + await wait(f) From ec72ac6d9ceb398edf26959f292609b23724f420 Mon Sep 17 00:00:00 2001 From: Ivan Levkivskyi Date: Wed, 4 Dec 2019 17:33:05 +0000 Subject: [PATCH 2/3] Fix Python 3.5 --- test-data/unit/pythoneval.test | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test-data/unit/pythoneval.test b/test-data/unit/pythoneval.test index 00605b453449..d6df7ece9899 100644 --- a/test-data/unit/pythoneval.test +++ b/test-data/unit/pythoneval.test @@ -1497,5 +1497,5 @@ from asyncio import Future, wait from typing import List async def foo() -> None: - f: List[Future[None]] = [] + f = [] # type: List[Future[None]] await wait(f) From 396e74f375a5af9330aa0ce3498fb3d61b726b2f Mon Sep 17 00:00:00 2001 From: Ivan Levkivskyi Date: Wed, 4 Dec 2019 19:01:06 +0000 Subject: [PATCH 3/3] Fix typo --- mypy/constraints.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mypy/constraints.py b/mypy/constraints.py index fdb1ab0d74d4..2a278995e142 100644 --- a/mypy/constraints.py +++ b/mypy/constraints.py @@ -108,7 +108,7 @@ 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 bee simplified. + # 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): 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