Skip to content

Support for import inside class #2077

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
merged 4 commits into from
Sep 3, 2016
Merged

Support for import inside class #2077

merged 4 commits into from
Sep 3, 2016

Conversation

gvanrossum
Copy link
Member

Tentatively fix #1839.

@@ -2128,6 +2128,17 @@ def visit_member_expr(self, expr: MemberExpr) -> None:
if full_name in obsolete_name_mapping:
self.fail("Module has no attribute %r (it's now called %r)" % (
expr.name, obsolete_name_mapping[full_name]), expr)
elif isinstance(base, RefExpr) and isinstance(base.node, TypeInfo):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I can understand, the if statement above seems to be looking for module members also. What's the difference?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The previous block looks for foo.bar where foo is a module (and bar is anything defined in foo.py). The new block here looks for C.bar where C is some class (C could also be a more complex expression, like foo.C), but only of bar is a module object. For other types of things in C (e.g. methods or class variables) other existing code paths do the right thing, but those other paths (in particular in checkmember.py) don't get it right if bar happens to be a module. Without my small change there, the test code would crash (see original issue, #1839). But the code here (semanal.py) is required to create the right symbol table entry so that looking up things in C.bar will actually work.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool, that makes sense. Would you mind adding short comments to the two branches saying this?

@ddfisher
Copy link
Collaborator

ddfisher commented Sep 2, 2016

Aside for minor comment changes this LGTM!

@ddfisher ddfisher merged commit 245108c into master Sep 3, 2016
@ddfisher ddfisher deleted the fix1839 branch September 3, 2016 00:09
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.

Modules imported to top level of class cause error.
2 participants