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

Merged
merged 3 commits into from May 31, 2017

Conversation

Projects
None yet
2 participants
@carljm
Contributor

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.

Show outdated Hide outdated mypy/semanal.py
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:

This comment has been minimized.

@ilevkivskyi

ilevkivskyi May 30, 2017

Collaborator

Why do you need this complex code? I would expect that you can access the current (active) class via self.type.

@ilevkivskyi

ilevkivskyi May 30, 2017

Collaborator

Why do you need this complex code? I would expect that you can access the current (active) class via self.type.

This comment has been minimized.

@carljm

carljm May 30, 2017

Contributor

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

carljm May 30, 2017

Contributor

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

This comment has been minimized.

Show comment
Hide comment
@carljm

carljm May 30, 2017

Contributor

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.

Contributor

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.

@ilevkivskyi

I think this is OK now, thanks!

@ilevkivskyi ilevkivskyi merged commit c38edf3 into python:master May 31, 2017

2 checks passed

continuous-integration/appveyor/pr AppVeyor build succeeded
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details

carljm added a commit to carljm/mypy that referenced this pull request May 31, 2017

Merge branch 'master' into module-alias
* master:
  Support accessing modules imported in class bodies within methods. (#3450)
  Make Type[X] compatible with metaclass of X (#3346)
  Sync typeshed (#3479)
  Handle flags in pytest passthrough (#3467)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment