diff --git a/mypy/build.py b/mypy/build.py index 59d46cdf9bb8..a39b78798d98 100644 --- a/mypy/build.py +++ b/mypy/build.py @@ -2447,13 +2447,14 @@ def find_module_and_diagnose(manager: BuildManager, # Don't honor a global (not per-module) ignore_missing_imports # setting for modules that used to have bundled stubs, as # otherwise updating mypy can silently result in new false - # negatives. + # negatives. (Unless there are stubs but they are incomplete.) global_ignore_missing_imports = manager.options.ignore_missing_imports py_ver = options.python_version[0] if ((is_legacy_bundled_package(top_level, py_ver) or is_legacy_bundled_package(second_level, py_ver)) and global_ignore_missing_imports - and not options.ignore_missing_imports_per_module): + and not options.ignore_missing_imports_per_module + and result is ModuleNotFoundReason.APPROVED_STUBS_NOT_INSTALLED): ignore_missing_imports = False if skip_diagnose: diff --git a/mypy/modulefinder.py b/mypy/modulefinder.py index 4e608097289b..eb763652175e 100644 --- a/mypy/modulefinder.py +++ b/mypy/modulefinder.py @@ -230,10 +230,14 @@ def _find_module_non_stub_helper(self, components: List[str], elif not plausible_match and (self.fscache.isdir(dir_path) or self.fscache.isfile(dir_path + ".py")): plausible_match = True - if (is_legacy_bundled_package(components[0], self.python_major_ver) - or is_legacy_bundled_package('.'.join(components[:2]), self.python_major_ver)): + if is_legacy_bundled_package(components[0], self.python_major_ver): + if (len(components) == 1 + or (self.find_module(components[0]) is + ModuleNotFoundReason.APPROVED_STUBS_NOT_INSTALLED)): + return ModuleNotFoundReason.APPROVED_STUBS_NOT_INSTALLED + if is_legacy_bundled_package('.'.join(components[:2]), self.python_major_ver): return ModuleNotFoundReason.APPROVED_STUBS_NOT_INSTALLED - elif plausible_match: + if plausible_match: return ModuleNotFoundReason.FOUND_WITHOUT_TYPE_HINTS else: return ModuleNotFoundReason.NOT_FOUND diff --git a/test-data/unit/check-modules.test b/test-data/unit/check-modules.test index 9a68b00b6007..ad7322a2573f 100644 --- a/test-data/unit/check-modules.test +++ b/test-data/unit/check-modules.test @@ -3107,3 +3107,18 @@ from google.cloud import x main:1: error: Cannot find implementation or library stub for module named "google.cloud" main:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports main:1: error: Cannot find implementation or library stub for module named "google" + +[case testMissingSubmoduleOfInstalledStubPackage] +import bleach.xyz +from bleach.abc import fgh +[file bleach/__init__.pyi] +[out] +main:1: error: Cannot find implementation or library stub for module named "bleach.xyz" +main:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports +main:2: error: Cannot find implementation or library stub for module named "bleach.abc" + +[case testMissingSubmoduleOfInstalledStubPackageIgnored] +# flags: --ignore-missing-imports +import bleach.xyz +from bleach.abc import fgh +[file bleach/__init__.pyi]