Repository: python/mypy (Python)

Issue:
Errors are shown in third-party packages with inline annotations when using daemon
To reproduce:
* Start daemon on a code that uses e.g. `pytest` before installing `pytest`
* Install `pytest` (it has inline annotations)
* Run `dmypy run` again and get a lot of error messages.


Hints:
Note that restarting the daemon fixes the issue.
This also happens with `types-*` packages. For example:
* Start daemon on a file that imports `requests` using `--disallow-untyped-defs`
* Install `types-requests`
* Run `dmypy run` and get lots of errors 

Again restarting the daemon helps.

Patch:
diff --git a/mypy/build.py b/mypy/build.py
--- 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
--- 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()


Test patch:
diff --git a/test-data/unit/fine-grained-modules.test b/test-data/unit/fine-grained-modules.test
--- a/test-data/unit/fine-grained-modules.test
+++ b/test-data/unit/fine-grained-modules.test
@@ -2206,3 +2206,41 @@ 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 testIgnoreErrorsFromTypeshed]
+# 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]
+[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. 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
+
+[file ts/stdlib/baz.pyi.2]
+import whatever
+undefined
+
+[out]
+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
+==