From 0ec5262cbf3b6b84872874b7569d2213fdc7daea Mon Sep 17 00:00:00 2001 From: Ivan Levkivskyi Date: Sun, 5 Feb 2017 21:27:08 +0100 Subject: [PATCH 1/2] Fix meta level on member access --- mypy/checkexpr.py | 7 +++++++ test-data/unit/check-incremental.test | 19 +++++++++++++++++++ test-data/unit/lib-stub/typing.pyi | 6 +++++- 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/mypy/checkexpr.py b/mypy/checkexpr.py index 79454d029ee2b..67c89515a46e1 100644 --- a/mypy/checkexpr.py +++ b/mypy/checkexpr.py @@ -990,6 +990,13 @@ def analyze_ordinary_member_access(self, e: MemberExpr, e.name, original_type, e, is_lvalue, False, False, self.named_type, self.not_ready_callback, self.msg, original_type=original_type, chk=self.chk) + if isinstance(member_type, CallableType): + for v in member_type.variables: + v.id.meta_level = 0 + if isinstance(member_type, Overloaded): + for it in member_type.items(): + for v in it.variables: + v.id.meta_level = 0 if is_lvalue: return member_type else: diff --git a/test-data/unit/check-incremental.test b/test-data/unit/check-incremental.test index 439d536b96f02..1950f5b8e578c 100644 --- a/test-data/unit/check-incremental.test +++ b/test-data/unit/check-incremental.test @@ -1767,6 +1767,25 @@ main:2: error: Revealed type is 'TypedDict(x=builtins.int, _fallback=typing.Mapp main:3: error: Revealed type is 'TypedDict(x=builtins.int, _fallback=ntcrash.C.A@4)' main:4: error: Revealed type is 'def () -> ntcrash.C.A@4' +[case testGenericMethodRestoreMetaLevel] +from typing import Dict + +d = {} # type: Dict[str, int] +g = d.get # This should not crash: see https://github.com/python/mypy/issues/2804 +[builtins fixtures/dict.pyi] + +[case testGenericMethodRestoreMetaLevel2] +from typing import TypeVar + +T = TypeVar('T') + +class D: + def m(self, x: T) -> T: + return x + +g = D().m # This should not crash: see https://github.com/python/mypy/issues/2804 +[builtins fixtures/dict.pyi] + [case testIncrementalPerFileFlags] # flags: --config-file tmp/mypy.ini import a diff --git a/test-data/unit/lib-stub/typing.pyi b/test-data/unit/lib-stub/typing.pyi index 77a7b349e4cdf..30cfa3d1f6258 100644 --- a/test-data/unit/lib-stub/typing.pyi +++ b/test-data/unit/lib-stub/typing.pyi @@ -78,7 +78,11 @@ class Sequence(Iterable[T], Generic[T]): @abstractmethod def __getitem__(self, n: Any) -> T: pass -class Mapping(Generic[T, U]): pass +class Mapping(Generic[T, U]): + @overload + def get(self, k: T) -> Optional[U]: pass + @overload + def get(self, k: T, default: Union[U, S]) -> Union[U, S]: pass class MutableMapping(Generic[T, U]): pass From de2e21fa3343a5b31258c00937cfe3fe412731d9 Mon Sep 17 00:00:00 2001 From: Ivan Levkivskyi Date: Tue, 7 Feb 2017 00:38:36 +0100 Subject: [PATCH 2/2] Move 'get' method to dict.pyi --- test-data/unit/fixtures/dict.pyi | 6 +++++- test-data/unit/lib-stub/typing.pyi | 6 +----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/test-data/unit/fixtures/dict.pyi b/test-data/unit/fixtures/dict.pyi index 18f3ebe6eccff..5a4458c09000e 100644 --- a/test-data/unit/fixtures/dict.pyi +++ b/test-data/unit/fixtures/dict.pyi @@ -1,6 +1,6 @@ # Builtins stub used in dictionary-related test cases. -from typing import TypeVar, Generic, Iterable, Iterator, Mapping, Tuple, overload +from typing import TypeVar, Generic, Iterable, Iterator, Mapping, Tuple, overload, Optional, Union T = TypeVar('T') KT = TypeVar('KT') @@ -19,6 +19,10 @@ class dict(Iterable[KT], Mapping[KT, VT], Generic[KT, VT]): def __setitem__(self, k: KT, v: VT) -> None: pass def __iter__(self) -> Iterator[KT]: pass def update(self, a: Mapping[KT, VT]) -> None: pass + @overload + def get(self, k: KT) -> Optional[VT]: pass + @overload + def get(self, k: KT, default: Union[KT, T]) -> Union[VT, T]: pass class int: # for convenience def __add__(self, x: int) -> int: pass diff --git a/test-data/unit/lib-stub/typing.pyi b/test-data/unit/lib-stub/typing.pyi index 30cfa3d1f6258..77a7b349e4cdf 100644 --- a/test-data/unit/lib-stub/typing.pyi +++ b/test-data/unit/lib-stub/typing.pyi @@ -78,11 +78,7 @@ class Sequence(Iterable[T], Generic[T]): @abstractmethod def __getitem__(self, n: Any) -> T: pass -class Mapping(Generic[T, U]): - @overload - def get(self, k: T) -> Optional[U]: pass - @overload - def get(self, k: T, default: Union[U, S]) -> Union[U, S]: pass +class Mapping(Generic[T, U]): pass class MutableMapping(Generic[T, U]): pass