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 instances of a class to access inner class definitions on that class. #3636

Merged
merged 2 commits into from Jun 30, 2017
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions mypy/checkmember.py
Expand Up @@ -227,6 +227,13 @@ def analyze_member_var_access(name: str, itype: Instance, info: TypeInfo,
# The associated Var node of a decorator contains the type.
v = vv.var

if isinstance(vv, TypeInfo):
# If the associated variable is a TypeInfo synthesize a Var node for
# the purposes of type checking. This enables us to type check things
# like accessing class attributes on an inner class.
v = Var(name, type=type_object_type(vv, builtin_type))
v.info = info

if isinstance(v, Var):
return analyze_var(name, v, itype, info, node, is_lvalue, msg,
original_type, not_ready_callback, chk=chk)
Expand Down
15 changes: 15 additions & 0 deletions test-data/unit/check-classes.test
Expand Up @@ -681,6 +681,21 @@ def produce() -> Bar:
reveal_type(Bar().make_int) # E: Revealed type is 'def () -> builtins.int'
return Bar()

[case testInnerClassPropertyAccess]
class Foo:
class Meta:
name = 'Bar'
meta = Meta

reveal_type(Foo.Meta) # E: Revealed type is 'def () -> __main__.Foo.Meta'
reveal_type(Foo.meta) # E: Revealed type is 'def () -> __main__.Foo.Meta'
reveal_type(Foo.Meta.name) # E: Revealed type is 'builtins.str'
reveal_type(Foo.meta.name) # E: Revealed type is 'builtins.str'
reveal_type(Foo().Meta) # E: Revealed type is 'def () -> __main__.Foo.Meta'
reveal_type(Foo().meta) # E: Revealed type is 'def () -> __main__.Foo.Meta'
reveal_type(Foo().meta.name) # E: Revealed type is 'builtins.str'
reveal_type(Foo().Meta.name) # E: Revealed type is 'builtins.str'

-- Declaring attribute type in method
-- ----------------------------------

Expand Down