Track classes with explicit __module__ attributes #16698
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Having access to this information can be used for more accurate diagnostics and tests
At runtime, classes which declare their __module__ attribute explicitly have that reflected in
their repr. This MR stores this information on TypeInfo nodes as the module_override attribute.
The original motivation for that is this typeshed discussion python/typeshed#11141
This is a relatively minimal change to enable the desired behavior. In trying to understand how this part of mypy works, I considered whether it was possible to store the module name and qualname separately and only generate the fullname on demand, like runtime does. I quickly discovered how much state is indexed by the fullname, so that seems like it'd be a massive project.
I was originally looking at modules which set their __name__ attribute explicitly. That's similarly easy to capture, but the number of places that would need to propagate to was a bit intimidating.
Strictly speaking, we don't need the qualname property, but it seemed reasonable to make that available since I had to generate it anyway. Changing TypeInfo.dump() to use the new realname property was done solely for the sake of being able to write a test.
I don't love
realname
as the name for this property, but I couldn't think of anything better. Real in the sense that that's what runtime would say it's name is.