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

__getattr__ support for metaclass #3840

Closed
yuyangguo42 opened this issue Aug 18, 2017 · 5 comments
Closed

__getattr__ support for metaclass #3840

yuyangguo42 opened this issue Aug 18, 2017 · 5 comments

Comments

@yuyangguo42
Copy link

yuyangguo42 commented Aug 18, 2017

from six import add_metaclass

class MetaFoo(type):
    SOME_DICT = {
        'asdf': 'bbbb',
    }
    def __getattr__(cls, attr):
        # type: (str) -> str
        return MetaFoo.SOME_DICT[attr]

@add_metaclass(MetaFoo)
class Foo(object):
    pass

print(Foo.asdf)  # error: Type[Foo] has no attribute "asdf"

So not sure if this is supposed to be supported or the usage pattern should be dis-encouraged anyways ><

@ilevkivskyi
Copy link
Member

This is not very high priority, but on the other hand this should be easy to implement. If you want to make a PR, then please take a look at checkmember.analyze_class_attribute_access (you can see how to make special treatment of __getattr__ etc. in checkmember.analyze_member_var_access)

@elazarg
Copy link
Contributor

elazarg commented Aug 18, 2017

It's unrelated to member access in metaclasses. mypy simply does not understand add_metaclass. It works fine with standard metaclasses, and on the other hand

x = Foo  # type: MetaFoo

fails too.

@elazarg
Copy link
Contributor

elazarg commented Aug 18, 2017

So this is duplicate of #3365.

@ilevkivskyi
Copy link
Member

OK, then I am closing this as a duplicate.

@elazarg
Copy link
Contributor

elazarg commented Aug 18, 2017

@yuyangguo42 you may want to try #3842.

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

No branches or pull requests

3 participants