Skip to content

Commit 090bfa9

Browse files
authored
Fix daemon crash when deleting packages (#10036)
Fixes #10035.
1 parent 55359ee commit 090bfa9

File tree

5 files changed

+52
-6
lines changed

5 files changed

+52
-6
lines changed

mypy/server/update.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1172,7 +1172,11 @@ def refresh_suppressed_submodules(
11721172
return None
11731173
# Find any submodules present in the directory.
11741174
pkgdir = os.path.dirname(path)
1175-
for fnam in fscache.listdir(pkgdir):
1175+
try:
1176+
entries = fscache.listdir(pkgdir)
1177+
except FileNotFoundError:
1178+
entries = []
1179+
for fnam in entries:
11761180
if (not fnam.endswith(('.py', '.pyi'))
11771181
or fnam.startswith("__init__.")
11781182
or fnam.count('.') != 1):

mypy/test/data.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ def parse_test_case(case: 'DataDrivenTestCase') -> None:
9595
reprocessed = [] if item.arg is None else [t.strip() for t in item.arg.split(',')]
9696
targets[passnum] = reprocessed
9797
elif item.id == 'delete':
98-
# File to delete during a multi-step test case
98+
# File/directory to delete during a multi-step test case
9999
assert item.arg is not None
100100
m = re.match(r'(.*)\.([0-9]+)$', item.arg)
101101
assert m, 'Invalid delete section: {}'.format(item.arg)

mypy/test/testcheck.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import os
44
import re
5+
import shutil
56
import sys
67

78
from typing import Dict, List, Set, Tuple
@@ -158,9 +159,13 @@ def run_case_once(self, testcase: DataDrivenTestCase,
158159
# Modify/create file
159160
copy_and_fudge_mtime(op.source_path, op.target_path)
160161
else:
161-
# Delete file
162-
# Use retries to work around potential flakiness on Windows (AppVeyor).
162+
# Delete file/directory
163163
path = op.path
164+
if os.path.isdir(path):
165+
# Sanity check to avoid unexpected deletions
166+
assert path.startswith('tmp')
167+
shutil.rmtree(path)
168+
# Use retries to work around potential flakiness on Windows (AppVeyor).
164169
retry_on_error(lambda: os.remove(path))
165170

166171
# Parse options after moving files (in case mypy.ini is being moved).

mypy/test/testfinegrained.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import os
1616
import re
17+
import shutil
1718

1819
from typing import List, Dict, Any, Tuple, Union, cast
1920

@@ -215,8 +216,13 @@ def perform_step(self,
215216
# Modify/create file
216217
copy_and_fudge_mtime(op.source_path, op.target_path)
217218
else:
218-
# Delete file
219-
os.remove(op.path)
219+
# Delete file/directory
220+
if os.path.isdir(op.path):
221+
# Sanity check to avoid unexpected deletions
222+
assert op.path.startswith('tmp')
223+
shutil.rmtree(op.path)
224+
else:
225+
os.remove(op.path)
220226
sources = self.parse_sources(main_src, step, options)
221227

222228
if step <= num_regular_incremental_steps:

test-data/unit/fine-grained-follow-imports.test

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -739,3 +739,34 @@ from . import mod2
739739
main.py:1: error: Cannot find implementation or library stub for module named "pkg"
740740
main.py:1: note: See https://mypy.readthedocs.io/en/latest/running_mypy.html#missing-imports
741741
==
742+
743+
[case testFollowImportsNormalDeletePackage]
744+
# flags: --follow-imports=normal
745+
# cmd: mypy main.py
746+
747+
[file main.py]
748+
import pkg
749+
750+
[file pkg/__init__.py]
751+
from . import mod
752+
753+
[file pkg/mod.py]
754+
from . import mod2
755+
import pkg2
756+
757+
[file pkg/mod2.py]
758+
from . import mod2
759+
import pkg2
760+
761+
[file pkg2/__init__.py]
762+
from . import mod3
763+
764+
[file pkg2/mod3.py]
765+
766+
[delete pkg/.2]
767+
[delete pkg2/.2]
768+
769+
[out]
770+
==
771+
main.py:1: error: Cannot find implementation or library stub for module named "pkg"
772+
main.py:1: note: See https://mypy.readthedocs.io/en/latest/running_mypy.html#missing-imports

0 commit comments

Comments
 (0)