Skip to content

Always set correct kind for module __getattr__ #5893

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

Merged
merged 4 commits into from
Nov 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -3017,7 +3017,7 @@ def visit_member_expr(self, expr: MemberExpr) -> None:
typ = AnyType(TypeOfAny.from_error)
else:
typ = AnyType(TypeOfAny.from_error)
expr.kind = MDEF
expr.kind = GDEF
expr.fullname = '{}.{}'.format(file.fullname(), expr.name)
expr.node = Var(expr.name, type=typ)
else:
Expand Down
50 changes: 50 additions & 0 deletions test-data/unit/check-modules.test
Original file line number Diff line number Diff line change
Expand Up @@ -2627,3 +2627,53 @@ import foo.bar.baz
reveal_type(foo.bar.baz.x) # E: Revealed type is 'builtins.int'
[file foo/bar/baz.py]
x = 0

[case testModuleGetAttrAssignUnannotated]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about testing a package with a missing explicit submodule (__getattr__ in __init__) and then assigning something to a submodule attribute? Or is this still unsupported?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, I added two tests for this.

import roles
# this should not crash
roles.role = 1

[file roles.pyi]
def __getattr__(name): ...

[case testModuleGetAttrAssignUnannotatedDouble]
import roles
# this also should not crash
roles.role.attr = 1

[file roles.pyi]
def __getattr__(name): ...

[case testModuleGetAttrAssignAny]
import roles
roles.role = 1

[file roles.pyi]
from typing import Any
def __getattr__(name: str) -> Any: ...

[case testModuleGetAttrAssignError]
import roles

roles.role = 1 # E: Incompatible types in assignment (expression has type "int", variable has type "str")

[file roles.pyi]
def __getattr__(name: str) -> str: ...

[case testModuleGetAttrAssignSubmodule]
import roles
roles.role = 1
roles.missing.attr = 1

[file roles/__init__.pyi]
from typing import Any
def __getattr__(name: str) -> Any: ...

[case testModuleGetAttrAssignSubmoduleStrict]
import roles
roles.role = 1 # E: Incompatible types in assignment (expression has type "int", variable has type Module)

[file roles/__init__.pyi]
from types import ModuleType
def __getattr__(name: str) -> ModuleType: ...
[builtins fixtures/module.pyi]