From 2fd1a0e4256f9706751211bc3e28de398746cc13 Mon Sep 17 00:00:00 2001 From: Jukka Lehtosalo Date: Thu, 29 Sep 2022 15:03:19 +0100 Subject: [PATCH 1/4] Silence errors from installed third-party in daemon --- mypy/build.py | 18 +++++++++++++----- mypy/server/update.py | 5 +++-- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/mypy/build.py b/mypy/build.py index 5ca2f9490991..6b8d2a9ea49c 100644 --- a/mypy/build.py +++ b/mypy/build.py @@ -1940,6 +1940,8 @@ def __init__( raise if follow_imports == "silent": self.ignore_all = True + elif path and is_silent_import_module(manager, path): + self.ignore_all = True self.path = path if path: self.abspath = os.path.abspath(path) @@ -2613,11 +2615,8 @@ def find_module_and_diagnose( else: skipping_module(manager, caller_line, caller_state, id, result) raise ModuleNotFound - if not manager.options.no_silence_site_packages: - for dir in manager.search_paths.package_path + manager.search_paths.typeshed_path: - if is_sub_path(result, dir): - # Silence errors in site-package dirs and typeshed - follow_imports = "silent" + if is_silent_import_module(manager, result): + follow_imports = "silent" return (result, follow_imports) else: # 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] else: if os.path.isfile(fnam): os.remove(fnam) + + +def is_silent_import_module(manager: BuildManager, path: str) -> bool: + if not manager.options.no_silence_site_packages: + for dir in manager.search_paths.package_path + manager.search_paths.typeshed_path: + if is_sub_path(path, dir): + # Silence errors in site-package dirs and typeshed + return True + return False diff --git a/mypy/server/update.py b/mypy/server/update.py index 65ce31da7c7a..cd2c415cfd2d 100644 --- a/mypy/server/update.py +++ b/mypy/server/update.py @@ -963,9 +963,10 @@ def key(node: FineGrainedDeferredNode) -> int: nodes = sorted(nodeset, key=key) - options = graph[module_id].options + state = graph[module_id] + options = state.options manager.errors.set_file_ignored_lines( - file_node.path, file_node.ignored_lines, options.ignore_errors + file_node.path, file_node.ignored_lines, options.ignore_errors or state.ignore_all ) targets = set() From a3169e83c500bc4af9527ee18322f62620122017 Mon Sep 17 00:00:00 2001 From: Jukka Lehtosalo Date: Thu, 29 Sep 2022 15:57:41 +0100 Subject: [PATCH 2/4] Add WIP test case (doesn't work yet) --- test-data/unit/fine-grained-modules.test | 32 ++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/test-data/unit/fine-grained-modules.test b/test-data/unit/fine-grained-modules.test index 8cb78b392e90..a612a563e792 100644 --- a/test-data/unit/fine-grained-modules.test +++ b/test-data/unit/fine-grained-modules.test @@ -2206,3 +2206,35 @@ a.py:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missin a.py:1: error: Library stubs not installed for "jack" a.py:1: note: Hint: "python3 -m pip install types-JACK-Client" a.py:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports + +[case testXXX] +# flags: --custom-typeshed-dir tmp/ts --follow-imports=normal +import foobar + +[file ts/stdlib/abc.pyi] +[file ts/stdlib/builtins.pyi] +class object: pass +class str: pass +class ellipsis: pass +[file ts/stdlib/sys.pyi] +[file ts/stdlib/types.pyi] +[file ts/stdlib/typing.pyi] +def cast(x): ... +[file ts/stdlib/typing_extensions.pyi] +[file ts/stdlib/VERSIONS] +[file ts/stubs/mypy_extensions/mypy_extensions.pyi] + +[file ts/stdlib/foobar.pyi.2] +# We report no errors from typeshed +import baz +import zar +undefined + +[file ts/stdlib/baz.pyi.2] +import whatever +undefined + +[out] +main:2: error: Cannot find implementation or library stub for module named "foobar" +main:2: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports +== From 093be285fb928d3bdbde187b7d5684f219ab7359 Mon Sep 17 00:00:00 2001 From: Jukka Lehtosalo Date: Thu, 29 Sep 2022 16:27:19 +0100 Subject: [PATCH 3/4] Fix test case --- test-data/unit/fine-grained-modules.test | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/test-data/unit/fine-grained-modules.test b/test-data/unit/fine-grained-modules.test index a612a563e792..b0e361775e49 100644 --- a/test-data/unit/fine-grained-modules.test +++ b/test-data/unit/fine-grained-modules.test @@ -2209,6 +2209,10 @@ a.py:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missin [case testXXX] # flags: --custom-typeshed-dir tmp/ts --follow-imports=normal +# cmd1: mypy a.py +# cmd2: mypy a.py + +[file a.py] import foobar [file ts/stdlib/abc.pyi] @@ -2235,6 +2239,6 @@ import whatever undefined [out] -main:2: error: Cannot find implementation or library stub for module named "foobar" -main:2: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports +a.py:1: error: Cannot find implementation or library stub for module named "foobar" +a.py:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports == From e51f766ec4985826b322fcfb920d38920c9603cd Mon Sep 17 00:00:00 2001 From: Jukka Lehtosalo Date: Thu, 29 Sep 2022 16:33:54 +0100 Subject: [PATCH 4/4] Rename test case --- test-data/unit/fine-grained-modules.test | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/test-data/unit/fine-grained-modules.test b/test-data/unit/fine-grained-modules.test index b0e361775e49..dcf28ad35357 100644 --- a/test-data/unit/fine-grained-modules.test +++ b/test-data/unit/fine-grained-modules.test @@ -2207,7 +2207,7 @@ a.py:1: error: Library stubs not installed for "jack" a.py:1: note: Hint: "python3 -m pip install types-JACK-Client" a.py:1: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports -[case testXXX] +[case testIgnoreErrorsFromTypeshed] # flags: --custom-typeshed-dir tmp/ts --follow-imports=normal # cmd1: mypy a.py # cmd2: mypy a.py @@ -2229,7 +2229,9 @@ def cast(x): ... [file ts/stubs/mypy_extensions/mypy_extensions.pyi] [file ts/stdlib/foobar.pyi.2] -# We report no errors from typeshed +# We report no errors from typeshed. It would be better to test ignoring +# errors from PEP 561 packages, but it's harder to test and uses the +# same code paths, so we are using typeshed instead. import baz import zar undefined