Skip to content
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

Allow __getattr__ during semantic analysis #5295

merged 4 commits into from Jun 29, 2018
Changes from 1 commit
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.
+30 −0
Diff settings


Just for now

Copy path View file
@@ -3083,6 +3083,18 @@ 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:
getattr_defn = names['__getattr__']
if isinstance(getattr_defn.node, (FuncDef, Var)):
if isinstance(getattr_defn.node.type, CallableType):
typ = getattr_defn.node.type.ret_type
typ = AnyType(TypeOfAny.from_error)
name = parts[i]
v = Var(name, type=typ)
v._fullname = name
n = SymbolTableNode(GDEF, v)
names[name] = n

This comment has been minimized.

Copy link

msullivan Jun 29, 2018


It might be worth pulling this out into another method, since it is a big chunk of code for an obscure case.

This comment has been minimized.

Copy link

ilevkivskyi Jun 29, 2018

Author Collaborator

Makes sense. I there is also similar code above in visit_import_from, it would make sense to combine the two into a helper method. I will do this tomorrow.

# TODO: What if node is Var or FuncDef?
if not n:
if not suppress_errors:
@@ -4451,3 +4451,21 @@ class C(B):
def meth(cls, x: int) -> int: ...
[builtins fixtures/classmethod.pyi]

[case testGetAttrImportAnnotation]
import a
x: a.A
[file a.pyi]
from typing import Any
def __getattr__(attr: str) -> Any: ...
[builtins fixtures/module.pyi]

[case testGetAttrImportBaseClass]
import a
class B(a.A): ...
[file a.pyi]
from typing import Any
def __getattr__(attr: str) -> Any: ...
[builtins fixtures/module.pyi]
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.