Skip to content

Add star imported modules as dependencies #6179

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 2 commits into from
Jan 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions mypy/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -1775,9 +1775,13 @@ def compute_dependencies(self) -> None:
def semantic_analysis(self) -> None:
assert self.tree is not None, "Internal error: method must be called on parsed file only"
patches = [] # type: List[Tuple[int, Callable[[], None]]]
self.manager.semantic_analyzer.imports.clear()
with self.wrap_context():
self.manager.semantic_analyzer.visit_file(self.tree, self.xpath, self.options, patches)
self.patches = patches
for dep in self.manager.semantic_analyzer.imports:
self.dependencies.append(dep)
self.priorities[dep] = PRI_LOW

def semantic_analysis_pass_three(self) -> None:
assert self.tree is not None, "Internal error: method must be called on parsed file only"
Expand Down
7 changes: 6 additions & 1 deletion mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,8 @@ class SemanticAnalyzerPass2(NodeVisitor[None],
is_stub_file = False # Are we analyzing a stub file?
_is_typeshed_stub_file = False # Are we analyzing a typeshed stub file?
imports = None # type: Set[str] # Imported modules (during phase 2 analysis)
# Note: some imports (and therefore dependencies) might
# not be found in phase 1, for example due to * imports.
errors = None # type: Errors # Keeps track of generated errors
plugin = None # type: Plugin # Mypy plugin for special casing of library features

Expand Down Expand Up @@ -1630,7 +1632,10 @@ def visit_import_all(self, i: ImportAll) -> None:
continue
# if '__all__' exists, all nodes not included have had module_public set to
# False, and we can skip checking '_' because it's been explicitly included.
if (node.module_public and (not name.startswith('_') or '__all__' in m.names)):
if node.module_public and (not name.startswith('_') or '__all__' in m.names):
if isinstance(node.node, MypyFile):
# Star import of submodule from a package, add it as a dependency.
self.imports.add(node.node.fullname())
existing_symbol = self.lookup_current_scope(name)
if existing_symbol:
# Import can redefine a variable. They get special treatment.
Expand Down
14 changes: 14 additions & 0 deletions test-data/unit/check-incremental.test
Original file line number Diff line number Diff line change
Expand Up @@ -4904,3 +4904,17 @@ a: Literal[2] = 2
main:2: error: Revealed type is 'builtins.int'
[out2]
main:2: error: Revealed type is 'Literal[2]'

[case testAddedSubStarImport]
# cmd: mypy -m a pack pack.mod b
# cmd2: mypy -m other
[file a.py]
from pack import *
[file pack/__init__.py]
[file pack/mod.py]
[file b.py]
import pack.mod
[file other.py]
import a
[out]
[out2]