Skip to content

Commit

Permalink
Don't ask to install a stub package if stubs are installed (#10670)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
JukkaL committed Jun 22, 2021
1 parent 9637f99 commit 96366d1
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
5 changes: 3 additions & 2 deletions mypy/build.py
Expand Up @@ -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:
Expand Down
10 changes: 7 additions & 3 deletions mypy/modulefinder.py
Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions test-data/unit/check-modules.test
Expand Up @@ -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]

0 comments on commit 96366d1

Please sign in to comment.