From 8096e9b9f3bfa08f369e634d2927a7459a434160 Mon Sep 17 00:00:00 2001 From: Varun Chawla Date: Sat, 7 Feb 2026 23:11:34 -0800 Subject: [PATCH] Add actual mypy error messages to documentation examples This change improves the googleability of mypy documentation by including actual error messages in the examples. When users encounter mypy errors, they can now more easily find relevant documentation by searching for the exact error message text. Changes made to docs/source/common_issues.rst: - Added actual error message for list invariance example (line 309) - Added actual error message for incompatible function argument example (line 332) - Added actual error message for incompatible method override examples (lines 729 and 739) Before: Comments like "# Error" or "mypy will complain about this" After: Full error messages like "# error: Incompatible types in assignment (expression has type ...)" This makes it easier for users to: 1. Search for error messages and find relevant documentation 2. Understand what error mypy will actually produce 3. Compare their errors with documented examples Fixes #2393 --- docs/source/common_issues.rst | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/source/common_issues.rst b/docs/source/common_issues.rst index e4239bd7a8eed..280c34763a3f1 100644 --- a/docs/source/common_issues.rst +++ b/docs/source/common_issues.rst @@ -306,7 +306,7 @@ unexpected errors when combined with type inference. For example: lst = [A(), A()] # Inferred type is list[A] new_lst = [B(), B()] # inferred type is list[B] - lst = new_lst # mypy will complain about this, because List is invariant + lst = new_lst # error: Incompatible types in assignment (expression has type "list[B]", variable has type "list[A]") Possible strategies in such situations are: @@ -329,7 +329,7 @@ Possible strategies in such situations are: def f_bad(x: list[A]) -> A: return x[0] - f_bad(new_lst) # Fails + f_bad(new_lst) # error: Argument 1 to "f_bad" has incompatible type "list[B]"; expected "list[A]" def f_good(x: Sequence[A]) -> A: return x[0] @@ -726,7 +726,7 @@ This example demonstrates both safe and unsafe overrides: class NarrowerArgument(A): # A more specific argument type isn't accepted - def test(self, t: list[int]) -> Sequence[str]: # Error + def test(self, t: list[int]) -> Sequence[str]: # error: Argument 1 of "test" is incompatible with supertype "A"; supertype defines the argument type as "Sequence[int]" ... class NarrowerReturn(A): @@ -736,7 +736,7 @@ This example demonstrates both safe and unsafe overrides: class GeneralizedReturn(A): # A more general return type is an error - def test(self, t: Sequence[int]) -> Iterable[str]: # Error + def test(self, t: Sequence[int]) -> Iterable[str]: # error: Return type "Iterable[str]" of "test" incompatible with return type "Sequence[str]" in supertype "A" ... You can use ``# type: ignore[override]`` to silence the error. Add it