Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make ignore_missing_imports work for libs which had bundled stubs #10582

Merged
merged 1 commit into from
Jun 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion mypy/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -2449,7 +2449,9 @@ def find_module_and_diagnose(manager: BuildManager,
# otherwise updating mypy can silently result in new false
# negatives.
global_ignore_missing_imports = manager.options.ignore_missing_imports
if top_level in legacy_bundled_packages and global_ignore_missing_imports:
if (top_level in legacy_bundled_packages
and global_ignore_missing_imports
and not options.ignore_missing_imports_per_module):
ignore_missing_imports = False

if skip_diagnose:
Expand Down
6 changes: 6 additions & 0 deletions mypy/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ def __init__(self) -> None:
self.no_silence_site_packages = False
self.no_site_packages = False
self.ignore_missing_imports = False
# Is ignore_missing_imports set in a per-module section
self.ignore_missing_imports_per_module = False
self.follow_imports = 'normal' # normal|silent|skip|error
# Whether to respect the follow_imports setting even for stub files.
# Intended to be used for disabling specific stubs.
Expand Down Expand Up @@ -325,6 +327,10 @@ def apply_changes(self, changes: Dict[str, object]) -> 'Options':
replace_object_state(new_options, self, copy_dict=True)
for key, value in changes.items():
setattr(new_options, key, value)
if changes.get("ignore_missing_imports"):
# This is the only option for which a per-module and a global
# option sometimes beheave differently.
new_options.ignore_missing_imports_per_module = True
return new_options

def build_per_module_cache(self) -> None:
Expand Down
32 changes: 31 additions & 1 deletion test-data/unit/check-modules.test
Original file line number Diff line number Diff line change
Expand Up @@ -3011,7 +3011,6 @@ certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
1() # E: "int" not callable


[case testDoNotLimitImportErrorVolume]
# flags: --disallow-any-expr --soft-error-limit=3
import xyz1 # E: Cannot find implementation or library stub for module named "xyz1" \
Expand Down Expand Up @@ -3064,3 +3063,34 @@ certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"
certifi.x # E: Expression has type "Any"

[case testIgnoreErrorFromMissingStubs1]
# flags: --config-file tmp/pyproject.toml
import certifi
from foobar1 import x
import foobar2
[file pyproject.toml]
\[tool.mypy]
ignore_missing_imports = true
\[[tool.mypy.overrides]]
module = "certifi"
ignore_missing_imports = true
\[[tool.mypy.overrides]]
module = "foobar1"
ignore_missing_imports = true

[case testIgnoreErrorFromMissingStubs2]
# flags: --config-file tmp/pyproject.toml
import certifi
from foobar1 import x
import foobar2 # E: Cannot find implementation or library stub for module named "foobar2" \
# N: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
[file pyproject.toml]
\[tool.mypy]
ignore_missing_imports = false
\[[tool.mypy.overrides]]
module = "certifi"
ignore_missing_imports = true
\[[tool.mypy.overrides]]
module = "foobar1"
ignore_missing_imports = true