Skip to content

Commit 00ca6bf

Browse files
authored
Silence errors from third party packages in daemon (#13768)
If daemon was running and additional third party code was installed, mypy daemon would report errors from the code. This was inconsistent with non-daemon mypy and the initial daemon run. If mypy was run with strict options, this could results lots of errors being generated. The same code is used to silence errors from typeshed. I'm using typeshed in the test case since it was easier to do. I also manually verified that this works with pytest, aiohttp and some typeshed stub packages. Fixes #13140.
1 parent ddd9177 commit 00ca6bf

File tree

3 files changed

+54
-7
lines changed

3 files changed

+54
-7
lines changed

mypy/build.py

+13-5
Original file line numberDiff line numberDiff line change
@@ -1940,6 +1940,8 @@ def __init__(
19401940
raise
19411941
if follow_imports == "silent":
19421942
self.ignore_all = True
1943+
elif path and is_silent_import_module(manager, path):
1944+
self.ignore_all = True
19431945
self.path = path
19441946
if path:
19451947
self.abspath = os.path.abspath(path)
@@ -2613,11 +2615,8 @@ def find_module_and_diagnose(
26132615
else:
26142616
skipping_module(manager, caller_line, caller_state, id, result)
26152617
raise ModuleNotFound
2616-
if not manager.options.no_silence_site_packages:
2617-
for dir in manager.search_paths.package_path + manager.search_paths.typeshed_path:
2618-
if is_sub_path(result, dir):
2619-
# Silence errors in site-package dirs and typeshed
2620-
follow_imports = "silent"
2618+
if is_silent_import_module(manager, result):
2619+
follow_imports = "silent"
26212620
return (result, follow_imports)
26222621
else:
26232622
# Could not find a module. Typically the reason is a
@@ -3560,3 +3559,12 @@ def record_missing_stub_packages(cache_dir: str, missing_stub_packages: set[str]
35603559
else:
35613560
if os.path.isfile(fnam):
35623561
os.remove(fnam)
3562+
3563+
3564+
def is_silent_import_module(manager: BuildManager, path: str) -> bool:
3565+
if not manager.options.no_silence_site_packages:
3566+
for dir in manager.search_paths.package_path + manager.search_paths.typeshed_path:
3567+
if is_sub_path(path, dir):
3568+
# Silence errors in site-package dirs and typeshed
3569+
return True
3570+
return False

mypy/server/update.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -963,9 +963,10 @@ def key(node: FineGrainedDeferredNode) -> int:
963963

964964
nodes = sorted(nodeset, key=key)
965965

966-
options = graph[module_id].options
966+
state = graph[module_id]
967+
options = state.options
967968
manager.errors.set_file_ignored_lines(
968-
file_node.path, file_node.ignored_lines, options.ignore_errors
969+
file_node.path, file_node.ignored_lines, options.ignore_errors or state.ignore_all
969970
)
970971

971972
targets = set()

test-data/unit/fine-grained-modules.test

+38
Original file line numberDiff line numberDiff line change
@@ -2206,3 +2206,41 @@ a.py:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missin
22062206
a.py:1: error: Library stubs not installed for "jack"
22072207
a.py:1: note: Hint: "python3 -m pip install types-JACK-Client"
22082208
a.py:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
2209+
2210+
[case testIgnoreErrorsFromTypeshed]
2211+
# flags: --custom-typeshed-dir tmp/ts --follow-imports=normal
2212+
# cmd1: mypy a.py
2213+
# cmd2: mypy a.py
2214+
2215+
[file a.py]
2216+
import foobar
2217+
2218+
[file ts/stdlib/abc.pyi]
2219+
[file ts/stdlib/builtins.pyi]
2220+
class object: pass
2221+
class str: pass
2222+
class ellipsis: pass
2223+
[file ts/stdlib/sys.pyi]
2224+
[file ts/stdlib/types.pyi]
2225+
[file ts/stdlib/typing.pyi]
2226+
def cast(x): ...
2227+
[file ts/stdlib/typing_extensions.pyi]
2228+
[file ts/stdlib/VERSIONS]
2229+
[file ts/stubs/mypy_extensions/mypy_extensions.pyi]
2230+
2231+
[file ts/stdlib/foobar.pyi.2]
2232+
# We report no errors from typeshed. It would be better to test ignoring
2233+
# errors from PEP 561 packages, but it's harder to test and uses the
2234+
# same code paths, so we are using typeshed instead.
2235+
import baz
2236+
import zar
2237+
undefined
2238+
2239+
[file ts/stdlib/baz.pyi.2]
2240+
import whatever
2241+
undefined
2242+
2243+
[out]
2244+
a.py:1: error: Cannot find implementation or library stub for module named "foobar"
2245+
a.py:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports
2246+
==

0 commit comments

Comments
 (0)