Skip to content

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

Merged

Conversation

carljm
Copy link
Member

@carljm carljm commented May 25, 2017

Observed the problem while working on #3435. Fixing this is a prerequisite for adding support for module alias assignment in a class body.

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:
Copy link
Member

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.

Copy link
Member Author

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_.

@carljm
Copy link
Member Author

carljm commented May 30, 2017

Thanks for review! My reply to your comment is now hidden by code updates. Using self.type definitely simplifies the inner-most logic to get the class type info. The outer checks are still needed in order to ensure we don't apply this too broadly to all variables, as shown by my latest additions to the tests. There may be a better way to do this verification as well, looking forward to your review.

Copy link
Member

@ilevkivskyi ilevkivskyi left a 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!

@ilevkivskyi ilevkivskyi merged commit c38edf3 into python:master May 31, 2017
carljm added a commit to carljm/mypy that referenced this pull request May 31, 2017
* 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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants