Skip to content
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

New semantic analyzer: don't add submodules to symbol tables #7005

Merged
merged 16 commits into from Jun 17, 2019

Conversation

JukkaL
Copy link
Collaborator

@JukkaL JukkaL commented Jun 17, 2019

Previously we added each submodule implicitly to the the symbol table of
the parent package. This PR removes this and instead we look up names from
the modules dictionary if they aren't found in the symbol table. The change
only affects the new semantic analyzer.

This provides the foundation that should make it easier to fix #6828. It also
arguably cleans up the code.

Also refactor some related code to avoid duplication.

This isn't a pure refactor since some error messages are slightly different.

@JukkaL JukkaL requested a review from ilevkivskyi June 17, 2019 15:02
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.

Thanks for the cleanup, looks good! I have couple requests for tests/comments.

Also edit the PR description before merging otherwise this will close #6828.

else:
if self.is_incomplete_namespace(file.fullname()):
self.record_incomplete_ref()
# Handle 'module.foo'.
Copy link
Member

Choose a reason for hiding this comment

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

Doesn't this comment just duplicate the one two lines above?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Removed one of the comments.

"""Look up a symbol from a module.

Return None if no matching symbol could be bound.
"""
module = node.fullname()
names = node.names
# Rebind potential references to old version of current module in
# fine-grained incremental mode.
if module == self.cur_mod_id:
names = self.globals
Copy link
Member

Choose a reason for hiding this comment

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

Since we are here again, could you please add a test that would fail without these two lines. Currently all tests pass if I remove them.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I couldn't come up with a test that would require this. It's possible that fine-grained dependencies will deal with all the cases that this was supposed to handle. I've removed this now.

var_type = AnyType(TypeOfAny.from_unimported_type)
v = Var(name, type=var_type)
v._fullname = fullname
sym = SymbolTableNode(GDEF, v)
Copy link
Member

Choose a reason for hiding this comment

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

Maybe add a comment explaining why we create a new Var every time for technically the same missing module? (IIUC the logic is the same as explained in the docstring for create_getattr_var()).

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Added a comment.

@JukkaL JukkaL merged commit 9eb2468 into master Jun 17, 2019
@gvanrossum gvanrossum deleted the semanal-refactor-submodules branch June 17, 2019 18:33
PattenR pushed a commit to PattenR/mypy that referenced this pull request Jun 23, 2019
…7005)

Previously we added each submodule implicitly to the the symbol table of
the parent package. This PR removes this and instead we look up names from
the modules dictionary if they aren't found in the symbol table. The change
only affects the new semantic analyzer.

This provides a foundation that should make python#6828 much easier to address. It 
also arguably cleans up the code.

Also refactor some related code to avoid duplication.

This isn't a pure refactor since some error messages are slightly different.
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.

Uncertainty between submodule and local definition in new semantic analyzer
2 participants