From c508be629f132fdcbb4d906fe4dbaae32718b4f9 Mon Sep 17 00:00:00 2001 From: Sjoerd Job Postmus Date: Thu, 1 Jan 2026 20:33:37 +0100 Subject: [PATCH] Don't read errors from cache on silent import (#20508) When using `--follow-imports=silent`, we do not care about the errors in imported files, only about errors in the files we're explicitly reading. --- mypy/build.py | 3 ++- test-data/unit/check-incremental.test | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/mypy/build.py b/mypy/build.py index 1357047d78a0..c2e378290274 100644 --- a/mypy/build.py +++ b/mypy/build.py @@ -2180,7 +2180,8 @@ def __init__( self.dep_hashes = { k: v for (k, v) in zip(self.meta.dependencies, self.meta.dep_hashes) } - self.error_lines = self.meta.error_lines + # Only copy `error_lines` if the module is not silently imported. + self.error_lines = [] if self.ignore_all else self.meta.error_lines if temporary: self.load_tree(temporary=True) if not manager.use_fine_grained_cache(): diff --git a/test-data/unit/check-incremental.test b/test-data/unit/check-incremental.test index b30357e8d8f3..c051ce9691be 100644 --- a/test-data/unit/check-incremental.test +++ b/test-data/unit/check-incremental.test @@ -2465,6 +2465,25 @@ def foo(a: int, b: int) -> str: [out2] tmp/b.py:2: error: Incompatible return value type (got "int", expected "str") +[case testIncrementalWithSilentImportsReverse] +-- Same commands as previous test, just in reverse order. +-- expected output is also in reverse order +# cmd: mypy -m b +# cmd2: mypy -m a +# flags: --follow-imports=silent +[file a.py] +import b + +b.foo(1, 2) + +[file b.py] +def foo(a: int, b: int) -> str: + return a + b + +[out] +tmp/b.py:2: error: Incompatible return value type (got "int", expected "str") +[out2] + [case testForwardNamedTupleToUnionWithOtherNamedTUple] from typing import NamedTuple, Union