-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
mypy fails to report missing "private" __x attribute #8267
Comments
Yeah, mypy should understand private attributes like |
#523 is related to this. It was closed by PR #6790, which adds code to avoid reporting false positives when the same private name is used in a subclass and superclass. However, it doesn't implement name mangling nor does it add any checks for private names being accessed from outside the class. As a result, false negatives can occur, as in the test case above. Here is another false negative test case: class C:
__private = 'foo'
print(C.__private) mypy will not report any errors, but the code breaks at runtime. There are good arguments for not implementing name mangling:
On the other hand, mypy is a type checker and not a style checker, so I don't know whether unclean code that does execute correctly should be rejected. The name mangling is documented in the official language reference, so it's not just a CPython detail. In any case, to solve the false negatives, either name mangling should be implemented or additional checks should be done when a name is looked up, to report private name lookups from outside the class. |
Mypy should clearly follow runtime Python semantics and implement name mangling. If the error messages would become confusing, we may have to add some extra logic or notes to error messages. We'd be happy to review a PR if anybody would like to contribute one! |
I personally feel like name mangling should not be supported and mangled names should stay private and filtered as such. People accessing symbols such as _Foo__bar should not be able to do that in Mypy. I also feel like the AttributeError |
The first example in this issue is of code that fails with an AttributeError at runtime but not reported by mypy. It seems clear to me that we should fix that. |
I completely agree. I was probably too tunnel-visioned on the potential fix in #16715. Sorry for that. I just feel like fully implementing name mangling on the parser level is not a good idea. |
Why shouldn't mypy fully support mangling? It's a perfectly well documented and valid part of the language, not deprecated or anything. There's no warnings at all about regular private attribute access, and plenty of things in typeshed include private/undocumented members. Mangled names aren't supposed to be any more private, there's perfectly valid reasons to want to access them from outside the class. There is the import system exporting rules, but those have ways to specifically allow private names. Even if you do use them, mypy gives a specific error, but still sets the types appropriately so subsequent code is correct. If name mangling wasn't implemented, uses would just be produce |
I agree in general that mypy should model the runtime correctly. It doesn't have to be mypy's job to warn users about access to private attributes, as that's easy enough to do in a general-purpose linter. I don't think we can afford to deprecate double underscores for positional-only arguments yet, though; there must be a lot of existing code that relies on those. If we add better support for name mangling, we need to be careful not to break those. |
Mypy doesn't seem to grok that
self.__x
in a subclass is different fromself.__x
in a superclass. It also doesn't seem to understand order of initialization.bug
mypy should report:
tytest.py:8: error: "B" has no attribute "__x
mypy 0.700
yes (
mypy-0.770+dev.01ca4e062e38a9db8f462b9464fbbd5dd945a8cf
)The text was updated successfully, but these errors were encountered: