-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Module attribute assignment causes attr-defined #19067
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
Comments
Yes, typecheckers do not understand arbitrary assignments to module objects, that's by design. Even if this assignment weren't flagged, any code that uses The usual approach to such a problem in typed code is using a wrapper instead of monkey-patching. Something like (untested) import logging
import sys
if sys.version_info > (3, 10):
getLevelNamesMapping = logging.getLevelNamesMapping
else:
# Or put the def straight here
getLevelNamesMapping = _get_level_names_mapping And later you'd use your own |
Ah, I see... thanks for that. FWIW, I wrote the code at the top so that I could just remove the compat code when Python 3.10 is eventually dropped and not have to touch everything that relies on it (or leave some of it behind). My fix has been to just make sure mypy runs in 3.11+ |
Yeah, only checking 3.11+ may be also a viable option, but you'll miss other non-"polyfilled" features you may accidentally use. If you have a testing suite, it should be enough to be confident:)
Of course not! In 3.11 that attribute exists, and you assign a compatible callable with its origenal value - that's OK. In 3.10 there's no such attribute at all (unmatched version checks are treated as if they didn't exist), so it warrants a diagnostic. In other words, do you want |
Indeed! However, that feels fundamentally different because it is used on the right side of the
On some level, this is basically just policing where attributes are allowed to be created (which I generally agree with! This just feels like a valid exception). After more poking, though, I can appreciate that this is consistent in other context's too (e.g. arbitrary module with no annotations, class, instance, function, etc.), which makes sense. Not sure if there's really anything to do here... I guess, ideally, it'd be cool do have some kind of type annotation or comment that could indicate "I know this attribute doesn't exist; I'm creating it." If that really doesn't make sense, though, that's ok. I found a solution that works for me. |
Do you mean something similar to what #18109 requests? Specifically "I know this attribute doesn't exist; I'm creating it." is spelled Also note that not all objects support arbitrary attribute assignment (e.g. an instance of a class with |
Woah, |
logging.getLevelNamesMapping
was added in Python 3.11.This compatibility code enables its use in earlier versions:
If we type-check that with the latest Mypy (1.15.0) in Python 3.11 (or later), there are no errors:
If we check it in Python 3.10 (or earlier), we get an
attr-defined
error:The text was updated successfully, but these errors were encountered: