From 92e0f78ce1c5742e7e5ab388bd7f13ecea999ff6 Mon Sep 17 00:00:00 2001 From: Ivan Levkivskyi Date: Sun, 8 Jul 2018 15:53:59 +0100 Subject: [PATCH] Only use __getattr__ when node is not found --- mypy/semanal.py | 2 +- test-data/unit/check-modules.test | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/mypy/semanal.py b/mypy/semanal.py index d7847ca30118..bf3b9c284051 100644 --- a/mypy/semanal.py +++ b/mypy/semanal.py @@ -3078,7 +3078,7 @@ def lookup_qualified(self, name: str, ctx: Context, n = names.get(parts[i], None) if n and isinstance(n.node, ImportedName): n = self.dereference_module_cross_ref(n) - elif '__getattr__' in names: + elif not n and '__getattr__' in names: gvar = self.create_getattr_var(names['__getattr__'], parts[i], parts[i]) if gvar: diff --git a/test-data/unit/check-modules.test b/test-data/unit/check-modules.test index 845cc8d92ba5..7567fa8707a7 100644 --- a/test-data/unit/check-modules.test +++ b/test-data/unit/check-modules.test @@ -2484,3 +2484,14 @@ from typing import Any def __getattr__(attr: str) -> Any: ... [builtins fixtures/module.pyi] [out] + +[case testNoGetattrInterference] +import testmod as t +def f(x: t.Cls) -> None: + reveal_type(x) # E: Revealed type is 'testmod.Cls' +[file testmod.pyi] +from typing import Any +def __getattr__(attr: str) -> Any: ... +class Cls: ... +[builtins fixtures/module.pyi] +[out]