Skip to content

Commit 4ccde20

Browse files
authored
Fine-grained: Fix crash on module deletion if parent module is ignored (#4469)
We used to assume that the parent module of a package submodule always exists, which is not the case if the parent module is not included in the build. Also fix test runner to allow '/' in command lines, needed for files inside packages.
1 parent 6fce774 commit 4ccde20

File tree

3 files changed

+20
-4
lines changed

3 files changed

+20
-4
lines changed

mypy/server/update.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -493,9 +493,13 @@ def delete_module(module_id: str,
493493
del manager.saved_cache[module_id]
494494
components = module_id.split('.')
495495
if len(components) > 1:
496-
parent = manager.modules['.'.join(components[:-1])]
497-
if components[-1] in parent.names:
498-
del parent.names[components[-1]]
496+
# Delete reference to module in parent module.
497+
parent_id = '.'.join(components[:-1])
498+
# If parent module is ignored, it won't be included in the modules dictionary.
499+
if parent_id in manager.modules:
500+
parent = manager.modules[parent_id]
501+
if components[-1] in parent.names:
502+
del parent.names[components[-1]]
499503
return new_graph
500504

501505

mypy/test/testfinegrained.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ def parse_sources(self, program_text: str) -> Optional[List[Tuple[str, str]]]:
137137
description.
138138
"""
139139
# TODO: Support defining separately for each incremental step.
140-
m = re.search('# cmd: mypy ([a-zA-Z0-9_. ]+)$', program_text, flags=re.MULTILINE)
140+
m = re.search('# cmd: mypy ([a-zA-Z0-9_./ ]+)$', program_text, flags=re.MULTILINE)
141141
if m:
142142
# The test case wants to use a non-default set of files.
143143
paths = m.group(1).strip().split()

test-data/unit/fine-grained-modules.test

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -619,3 +619,15 @@ import x
619619
a/b.py:3: error: Revealed type is 'Any'
620620
==
621621
a/b.py:3: error: Unsupported operand types for + ("int" and "str")
622+
623+
[case testDeleteModuleWithinPackageInitIgnored]
624+
# cmd: mypy x.py a/b.py
625+
# flags: --follow-imports=skip --ignore-missing-imports
626+
[file x.py]
627+
import a.b
628+
[file a/__init__.py]
629+
[file a/b.py]
630+
x = 1
631+
[delete a/b.py.2]
632+
[out]
633+
==

0 commit comments

Comments
 (0)