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

Double underscore name mangling in classes to allow independent types #523

Closed
JukkaL opened this issue Dec 8, 2014 · 10 comments · Fixed by #6790
Closed

Double underscore name mangling in classes to allow independent types #523

JukkaL opened this issue Dec 8, 2014 · 10 comments · Fixed by #6790

Comments

@JukkaL
Copy link
Collaborator

JukkaL commented Dec 8, 2014

Mypy should perform name mangling in classes.

@marmistrz
Copy link

A simple example, exposing the behavior:

class foo:
    def __bar(self) -> None:
        print("xD")

foo()._foo__bar()
/tmp/test.py:5: error: "foo" has no attribute "_foo__bar"

@gvanrossum
Copy link
Member

That looks intentional. You shouldn't be accessing private attributes like that.

@marmistrz
Copy link

@gvanrossum in production code not. But in tests you sometimes need to access the internal state of the object to validate what happened.

@JukkaL
Copy link
Collaborator Author

JukkaL commented Sep 19, 2017

You can use getattr(foo(), '_foo__bar')() in tests. The main issue is allowing multiple classes in a class hierarchy use the same __foo attribute with independent types.

@marmistrz
Copy link

@JukkaL, yes, but it's less readable

@kwvanderlinde
Copy link

The fact of the matter is that foo does have the attribute _foo__bar and we all know it. The error message is a blatant lie and a source of confusion.

@gvanrossum
Copy link
Member

Thank you for your encouragement. You should just use # type: ignore and move on. If you do this a lot, clearly something is wrong in your application or API design and mypy should not endorse that.

@ilevkivskyi
Copy link
Member

ilevkivskyi commented Sep 23, 2017

@gvanrossum

You should just use # type: ignore and move on. If you do this a lot, clearly something is wrong in your application or API design and mypy should not endorse that.

As @JukkaL mentioned the original issue is not about allowing Foo()._Foo__foo, this issue is about allowing things like:

class B:
    __foo: int
class C(B):
    __foo: str

which is type safe but currently prohibited.

@ilevkivskyi ilevkivskyi reopened this Sep 23, 2017
@ilevkivskyi ilevkivskyi changed the title Double underscore name mangling in classes Double underscore name mangling in classes to allow independent types Sep 23, 2017
@gvanrossum
Copy link
Member

Ouch, sorry, I read too quickly.

@rafaelcaricio
Copy link
Contributor

rafaelcaricio commented May 6, 2019

I'm looking at this one.

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

Successfully merging a pull request may close this issue.

6 participants