Skip to content

New semantic analyzer: allow replacing submodule with a variable - #6630

Merged
ilevkivskyi merged 2 commits into
python:masterfrom
ilevkivskyi:fix-newan-tricky-import
Apr 4, 2019
Merged

ilevkivskyi merged 2 commits into
python:masterfrom
ilevkivskyi:fix-newan-tricky-import

Conversation

@ilevkivskyi

Copy link
Copy Markdown
Member

Although this looks like an arbitrary exception from general rules, this:

  • is an actual pattern used by some libraries
  • matches old analyzer in most cases
  • matches runtime semantics

@ilevkivskyi
ilevkivskyi requested a review from JukkaL April 4, 2019 16:53

@JukkaL JukkaL left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Even this is a bit ad hoc, it's useful to be able to support this use case. I have a few questions about test cases, otherwise looks good (assuming tests pass).

import lib

reveal_type(lib.config.x) # E: Revealed type is 'builtins.int'
reveal_type(config.x) # E: Revealed type is 'builtins.int'

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

What about trying to import lib.config as a module? Will it work? What about import lib.config as c?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Hm, OK, I will try adding these tests.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

OK so these two have different behavior in mypy:

  • import lib.config actually imports lib, so when one writes lib.config later, the variable takes precedence.
  • import lib.config as tmp finds module lib.config and binds it to tmp, so when one writes tmp later it refers to the module.

At runtime variable always wins. Fixing the second one is non-trivial, because we need to check whether there is a "shadowing" variable at every submodule level. Also this is kind of corner case to a corner case.

[out2]
tmp/a.py:2: error: Revealed type is 'Any'

[case testNewAnanlyzerTrickyImportPackage]

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Would it be useful to an incremental mode test case as well?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yes, I will add an incremental test.

@ilevkivskyi
ilevkivskyi merged commit 1c0226a into python:master Apr 4, 2019
@ilevkivskyi
ilevkivskyi deleted the fix-newan-tricky-import branch April 4, 2019 18:44
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