Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Track classes with explicit __module__ attributes #16698

Closed
wants to merge 1 commit into from

Conversation

tungol
Copy link
Contributor

@tungol tungol commented Dec 21, 2023

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.

@tungol tungol marked this pull request as draft December 21, 2023 23:49

This comment has been minimized.

Having access to this information can be used for more accurate
diagnostics and tests
Copy link
Contributor

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant