Skip to content

Commit 002722e

Browse files
authored
Don't ask to install a stub package if stubs are installed (#10670)
If we encounter an import of a submodule of a package with installed stubs, and the submodule doesn't exist, don't ask to install stubs since that's not going to help. Also make it possible to ignore this error using `--ignore-missing-imports`. Work on #10645.
1 parent e4c2668 commit 002722e

File tree

3 files changed

+25
-5
lines changed

3 files changed

+25
-5
lines changed

mypy/build.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2447,13 +2447,14 @@ def find_module_and_diagnose(manager: BuildManager,
24472447
# Don't honor a global (not per-module) ignore_missing_imports
24482448
# setting for modules that used to have bundled stubs, as
24492449
# otherwise updating mypy can silently result in new false
2450-
# negatives.
2450+
# negatives. (Unless there are stubs but they are incomplete.)
24512451
global_ignore_missing_imports = manager.options.ignore_missing_imports
24522452
py_ver = options.python_version[0]
24532453
if ((is_legacy_bundled_package(top_level, py_ver)
24542454
or is_legacy_bundled_package(second_level, py_ver))
24552455
and global_ignore_missing_imports
2456-
and not options.ignore_missing_imports_per_module):
2456+
and not options.ignore_missing_imports_per_module
2457+
and result is ModuleNotFoundReason.APPROVED_STUBS_NOT_INSTALLED):
24572458
ignore_missing_imports = False
24582459

24592460
if skip_diagnose:

mypy/modulefinder.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -230,10 +230,14 @@ def _find_module_non_stub_helper(self, components: List[str],
230230
elif not plausible_match and (self.fscache.isdir(dir_path)
231231
or self.fscache.isfile(dir_path + ".py")):
232232
plausible_match = True
233-
if (is_legacy_bundled_package(components[0], self.python_major_ver)
234-
or is_legacy_bundled_package('.'.join(components[:2]), self.python_major_ver)):
233+
if is_legacy_bundled_package(components[0], self.python_major_ver):
234+
if (len(components) == 1
235+
or (self.find_module(components[0]) is
236+
ModuleNotFoundReason.APPROVED_STUBS_NOT_INSTALLED)):
237+
return ModuleNotFoundReason.APPROVED_STUBS_NOT_INSTALLED
238+
if is_legacy_bundled_package('.'.join(components[:2]), self.python_major_ver):
235239
return ModuleNotFoundReason.APPROVED_STUBS_NOT_INSTALLED
236-
elif plausible_match:
240+
if plausible_match:
237241
return ModuleNotFoundReason.FOUND_WITHOUT_TYPE_HINTS
238242
else:
239243
return ModuleNotFoundReason.NOT_FOUND

test-data/unit/check-modules.test

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3107,3 +3107,18 @@ from google.cloud import x
31073107
main:1: error: Cannot find implementation or library stub for module named "google.cloud"
31083108
main:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
31093109
main:1: error: Cannot find implementation or library stub for module named "google"
3110+
3111+
[case testMissingSubmoduleOfInstalledStubPackage]
3112+
import bleach.xyz
3113+
from bleach.abc import fgh
3114+
[file bleach/__init__.pyi]
3115+
[out]
3116+
main:1: error: Cannot find implementation or library stub for module named "bleach.xyz"
3117+
main:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
3118+
main:2: error: Cannot find implementation or library stub for module named "bleach.abc"
3119+
3120+
[case testMissingSubmoduleOfInstalledStubPackageIgnored]
3121+
# flags: --ignore-missing-imports
3122+
import bleach.xyz
3123+
from bleach.abc import fgh
3124+
[file bleach/__init__.pyi]

0 commit comments

Comments
 (0)