Skip to content

Error when accessing attribute of inner class #3635

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

Closed
rowillia opened this issue Jun 29, 2017 · 3 comments · Fixed by #3636
Closed

Error when accessing attribute of inner class #3635

rowillia opened this issue Jun 29, 2017 · 3 comments · Fixed by #3636

Comments

@rowillia
Copy link
Contributor

rowillia commented Jun 29, 2017

test_inner_class_attribute.py:

class Foo:
    class Meta:
        name = 'Roy'

print(Foo().Meta.name)

Python executes the code just fine:

$ python3.6 test_inner_class_attribute.py
Roy

But mypy fails to typecheck

$ mypy test_inner_class_attribute.py
test_inner_class_attribute.py:5: error: Member "Meta" is not assignable

This pattern is super common in the Django Rest Framework, Marshmallow, and PynamoDB projects (Probably more that I don't know about as well 😄 )

@emmatyping
Copy link
Member

Interesting. This will pass typechecking:

from typing import Any
class A:
    class B:
        pass
    b = B()
reveal_type(A().b)
reveal_type(A.B)
main.py:6: error: Revealed type is 'main.A.B'
main.py:7: error: Revealed type is 'def () -> main.A.B'

So the issue seems to be that B is being inferred as a function.

@rowillia
Copy link
Contributor Author

@ethanhs yeah debugging that now. The issue is when analyzing the member access the type is TypeInfo which then falls through here -
https://github.com/python/mypy/blob/master/mypy/checkmember.py#L269

@emmatyping
Copy link
Member

Yep, that makes sense. Shouldn't be too bad to fix if you add handling of member classes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants