Skip to content

Fix accessing qualified import in incremental mode #3548

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
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
20 changes: 20 additions & 0 deletions mypy/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -1478,6 +1478,24 @@ def calculate_mros(self) -> None:
fixup_module_pass_two(self.tree, self.manager.modules,
self.manager.options.quick_and_dirty)

def patch_dependency_parents(self) -> None:
"""
In Python, if a and a.b are both modules, running `import a.b` will
modify not only the current module's namespace, but a's namespace as
well -- see SemanticAnalyzer.add_submodules_to_parent_modules for more
details.

However, this patching process can occur after `a` has been parsed and
serialized during increment mode. Consequently, we need to repeat this
patch when deserializing a cached file.

This function should be called only when processing fresh SCCs -- the
semantic analyzer will perform this patch for us when processing stale
SCCs.
"""
for dep in self.dependencies:
self.manager.semantic_analyzer.add_submodules_to_parent_modules(dep, True)

def fix_suppressed_dependencies(self, graph: Graph) -> None:
"""Corrects whether dependencies are considered stale in silent mode.

Expand Down Expand Up @@ -2010,6 +2028,8 @@ def process_fresh_scc(graph: Graph, scc: List[str]) -> None:
graph[id].fix_cross_refs()
for id in scc:
graph[id].calculate_mros()
for id in scc:
graph[id].patch_dependency_parents()


def process_stale_scc(graph: Graph, scc: List[str], manager: BuildManager) -> None:
Expand Down
3 changes: 1 addition & 2 deletions test-data/unit/check-serialize.test
Original file line number Diff line number Diff line change
Expand Up @@ -1103,8 +1103,7 @@ main:3: error: Too few arguments for "g"
[out2]
main:3: error: Too few arguments for "g"

[case testSerializeQualifiedImport-skip]
# This fails currently: https://github.com/python/mypy/issues/3274
[case testSerializeQualifiedImport]
import b
b.c.d.f()
b.c.d.g()
Expand Down