8000 Add star imported modules as dependencies (#6179) · python/mypy@e6f111a · GitHub
[go: up one dir, main page]

Skip to content

Commit e6f111a

Browse files
ilevkivskyimsullivan
authored andcommitted
Add star imported modules as dependencies (#6179)
1 parent 67925f2 commit e6f111a

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

mypy/build.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1775,9 +1775,13 @@ def compute_dependencies(self) -> None:
17751775
def semantic_analysis(self) -> None:
17761776
assert self.tree is not None, "Internal error: method must be called on parsed file only"
17771777
patches = [] # type: List[Tuple[int, Callable[[], None]]]
1778+
self.manager.semantic_analyzer.imports.clear()
17781779
with self.wrap_context():
17791780
self.manager.semantic_analyzer.visit_file(self.tree, self.xpath, self.options, patches)
17801781
self.patches = patches
1782+
for dep in self.manager.semantic_analyzer.imports:
1783+
self.dependencies.append(dep)
1784+
self.priorities[dep] = PRI_LOW
17811785

17821786
def semantic_analysis_pass_three(self) -> None:
17831787
assert self.tree is not None, "Internal error: method must be called on parsed file only"

mypy/semanal.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,8 @@ class SemanticAnalyzerPass2(NodeVisitor[None],
212212
is_stub_file = False # Are we analyzing a stub file?
213213
_is_typeshed_stub_file = False # Are we analyzing a typeshed stub file?
214214
imports = None # type: Set[str] # Imported modules (during phase 2 analysis)
215+
# Note: some imports (and therefore dependencies) might
216+
# not be found in phase 1, for example due to * imports.
215217
errors = None # type: Errors # Keeps track of generated errors
216218
plugin = None # type: Plugin # Mypy plugin for special casing of library features
217219

@@ -1630,7 +1632,10 @@ def visit_import_all(self, i: ImportAll) -> None:
16301632
continue
16311633
# if '__all__' exists, all nodes not included have had module_public set to
16321634
# False, and we can skip checking '_' because it's been explicitly included.
1633-
if (node.module_public and (not name.startswith('_') or '__all__' in m.names)):
1635+
if node.module_public and (not name.startswith('_') or '__all__' in m.names):
1636+
if isinstance(node.node, MypyFile):
1637+
# Star import of submodule from a package, add it as a dependency.
1638+
self.imports.add(node.node.fullname())
16341639
existing_symbol = self.lookup_current_scope(name)
16351640
if existing_symbol:
16361641
# Import can redefine a variable. They get special treatment.

test-data/unit/check-incremental.test

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4904,3 +4904,17 @@ a: Literal[2] = 2
49044904
main:2: error: Revealed type is 'builtins.int'
49054905
[out2]
49064906
main:2: error: Revealed type is 'Literal[2]'
4907+
4908+
[case testAddedSubStarImport]
4909+
# cmd: mypy -m a pack pack.mod b
4910+
# cmd2: mypy -m other
4911+
[file a.py]
4912+
from pack import *
4913+
[file pack/__init__.py]
4914+
[file pack/mod.py]
4915+
[file b.py]
4916+
import pack.mod
4917+
[file other.py]
4918+
import a
4919+
[out]
4920+
[out2]

0 commit comments

Comments
 (0)
0