-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Support accessing modules imported in class bodies within methods. #3450
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
Support accessing modules imported in class bodies within methods. #3450
Conversation
mypy/semanal.py
Outdated
if isinstance(base.node, TypeInfo): | ||
# C.bar where C is a class | ||
type_info = base.node | ||
elif isinstance(base.node, Var) and self.function_stack: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do you need this complex code? I would expect that you can access the current (active) class via self.type
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Inexperience in the mypy codebase! I missed the existence of self.type
, thanks! Pushing update.
This complex code is performing two tasks, though, and self.type
addresses only one of them. The other task is verifying that base.node
is in fact a reference to the class (cls
) or an instance of it (self
). If we fully simplify, we fail this added test, because we will consider any variable as a class reference: https://gist.github.com/carljm/174bff5aeb78d9a3b04ed11a536cfb9f
If you have a suggestion of a better way to do this verification, I'd be happy for that! Of course it is possible to just check that the variable name is cls
or self
, but this is not really correct; there is no such naming requirement in the language, and in some nested-class cases I've seen people use e.g. self_
.
Thanks for review! My reply to your comment is now hidden by code updates. Using |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is OK now, thanks!
* master: Support accessing modules imported in class bodies within methods. (python#3450) Make Type[X] compatible with metaclass of X (python#3346) Sync typeshed (python#3479) Handle flags in pytest passthrough (python#3467)
Observed the problem while working on #3435. Fixing this is a prerequisite for adding support for module alias assignment in a class body.