From 648345864e13f44f5da8d480b987762791a22909 Mon Sep 17 00:00:00 2001 From: "Kai (Kazuya Ito)" Date: Sat, 28 Feb 2026 16:29:42 +0900 Subject: [PATCH 1/9] Clarify error message for TypeVar function --- mypy/message_registry.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mypy/message_registry.py b/mypy/message_registry.py index 9de31514b6bd..6d0f7abec9db 100644 --- a/mypy/message_registry.py +++ b/mypy/message_registry.py @@ -196,7 +196,7 @@ def with_additional_msg(self, info: str) -> ErrorMessage: TYPEVAR_ARG_MUST_BE_TYPE: Final = '{} "{}" must be a type' TYPEVAR_UNEXPECTED_ARGUMENT: Final = 'Unexpected argument to "TypeVar()"' UNBOUND_TYPEVAR: Final = ( - "A function returning TypeVar should receive at least one argument containing the same TypeVar" + "A function returning TypeVar should have at least one same TypeVar parameter" ) TYPE_PARAMETERS_SHOULD_BE_DECLARED: Final = ( "All type parameters should be declared ({} not declared)" From b40570bc339f6381a62861ed69ab12a76ff42ab0 Mon Sep 17 00:00:00 2001 From: "Kai (Kazuya Ito)" Date: Sat, 28 Feb 2026 16:31:25 +0900 Subject: [PATCH 2/9] Update error messages for TypeVar function definitions --- test-data/unit/check-typevar-unbound.test | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test-data/unit/check-typevar-unbound.test b/test-data/unit/check-typevar-unbound.test index 79d326f9fedf..cf68341987f6 100644 --- a/test-data/unit/check-typevar-unbound.test +++ b/test-data/unit/check-typevar-unbound.test @@ -3,19 +3,19 @@ from typing import TypeVar T = TypeVar('T') -def f() -> T: # E: A function returning TypeVar should receive at least one argument containing the same TypeVar +def f() -> T: # E: A function returning TypeVar should have at least one same TypeVar parameter ... f() U = TypeVar('U', bound=int) -def g() -> U: # E: A function returning TypeVar should receive at least one argument containing the same TypeVar \ +def g() -> U: # E: A function returning TypeVar should have at least one same TypeVar parameter \ # N: Consider using the upper bound "int" instead ... V = TypeVar('V', int, str) -def h() -> V: # E: A function returning TypeVar should receive at least one argument containing the same TypeVar +def h() -> V: # E: A function returning TypeVar should have at least one same TypeVar parameter ... [case testInnerFunctionTypeVar] From 20d69ad5136c5b79fa2820253d9b8f0c4b496e63 Mon Sep 17 00:00:00 2001 From: "Kai (Kazuya Ito)" Date: Sat, 28 Feb 2026 16:33:02 +0900 Subject: [PATCH 3/9] Update error messages for TypeVar function signatures --- test-data/unit/check-generics.test | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/test-data/unit/check-generics.test b/test-data/unit/check-generics.test index 26d709850c1e..cb8adafe9f48 100644 --- a/test-data/unit/check-generics.test +++ b/test-data/unit/check-generics.test @@ -1599,9 +1599,9 @@ A = TypeVar('A') B = TypeVar('B') def f1(x: A) -> A: ... -def f2(x: A) -> B: ... # E: A function returning TypeVar should receive at least one argument containing the same TypeVar +def f2(x: A) -> B: ... # E: A function returning TypeVar should have at least one same TypeVar parameter def f3(x: B) -> B: ... -def f4(x: int) -> A: ... # E: A function returning TypeVar should receive at least one argument containing the same TypeVar +def f4(x: int) -> A: ... # E: A function returning TypeVar should have at least one same TypeVar parameter y1 = f1 if int(): @@ -1650,8 +1650,8 @@ B = TypeVar('B') T = TypeVar('T') def outer(t: T) -> None: def f1(x: A) -> A: ... - def f2(x: A) -> B: ... # E: A function returning TypeVar should receive at least one argument containing the same TypeVar - def f3(x: T) -> A: ... # E: A function returning TypeVar should receive at least one argument containing the same TypeVar + def f2(x: A) -> B: ... # E: A function returning TypeVar should have at least one same TypeVar parameter + def f3(x: T) -> A: ... # E: A function returning TypeVar should have at least one same TypeVar parameter def f4(x: A) -> T: ... def f5(x: T) -> T: ... @@ -1818,7 +1818,7 @@ from typing import TypeVar A = TypeVar('A') B = TypeVar('B') def f1(x: int, y: A) -> A: ... -def f2(x: int, y: A) -> B: ... # E: A function returning TypeVar should receive at least one argument containing the same TypeVar +def f2(x: int, y: A) -> B: ... # E: A function returning TypeVar should have at least one same TypeVar parameter def f3(x: A, y: B) -> B: ... g = f1 g = f2 From 7a186893ccb0bf84836a8998f6d19f7b9fbba73a Mon Sep 17 00:00:00 2001 From: "Kai (Kazuya Ito)" Date: Sat, 28 Feb 2026 16:34:29 +0900 Subject: [PATCH 4/9] Fix error message for static method Self type Correct error message for static method returning Self type. --- test-data/unit/check-selftype.test | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test-data/unit/check-selftype.test b/test-data/unit/check-selftype.test index c15720035256..79e93833d5f5 100644 --- a/test-data/unit/check-selftype.test +++ b/test-data/unit/check-selftype.test @@ -1541,7 +1541,7 @@ class C: def meth(cls) -> Self: ... @staticmethod def bad() -> Self: ... # E: Static methods cannot use Self type \ - # E: A function returning TypeVar should receive at least one argument containing the same TypeVar \ + # E: A function returning TypeVar should have at least one same TypeVar parameter \ # N: Consider using the upper bound "C" instead class D(C): ... From 80bd543b7cb6869aa4ced973d3f78d8f9984f035 Mon Sep 17 00:00:00 2001 From: "Kai (Kazuya Ito)" Date: Sat, 28 Feb 2026 16:36:16 +0900 Subject: [PATCH 5/9] Resolve type variable errors in check-classes test Fix type variable issues in error function. --- test-data/unit/check-classes.test | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test-data/unit/check-classes.test b/test-data/unit/check-classes.test index 5a66eff2bd3b..074c4b3022a6 100644 --- a/test-data/unit/check-classes.test +++ b/test-data/unit/check-classes.test @@ -3716,7 +3716,7 @@ def error(u_c: Type[U]) -> P: # Error here, see below return new_pro(u_c) # Error here, see below [out] main:11: note: Revealed type is "__main__.WizUser" -main:12: error: A function returning TypeVar should receive at least one argument containing the same TypeVar +main:12: error: A function returning TypeVar should have at least one same TypeVar parameter main:12: note: Consider using the upper bound "ProUser" instead main:13: error: Value of type variable "P" of "new_pro" cannot be "U" main:13: error: Incompatible return value type (got "U", expected "P") From 411af35812c4d49104c7a382ca009ff6e0d7f59d Mon Sep 17 00:00:00 2001 From: "Kai (Kazuya Ito)" Date: Sat, 28 Feb 2026 16:37:27 +0900 Subject: [PATCH 6/9] Update type annotation error messages in tests --- test-data/unit/check-inference.test | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test-data/unit/check-inference.test b/test-data/unit/check-inference.test index 22edd12b0c4c..12a26e9bb6bd 100644 --- a/test-data/unit/check-inference.test +++ b/test-data/unit/check-inference.test @@ -590,7 +590,7 @@ def ff() -> None: x = f() # E: Need type annotation for "x" reveal_type(x) # N: Revealed type is "Any" -def f() -> T: pass # E: A function returning TypeVar should receive at least one argument containing the same TypeVar +def f() -> T: pass # E: A function returning TypeVar should have at least one same TypeVar parameter def g(a: T) -> None: pass g(None) # Ok @@ -2693,7 +2693,7 @@ def main() -> None: [case testDontMarkUnreachableAfterInferenceUninhabited] from typing import TypeVar T = TypeVar('T') -def f() -> T: pass # E: A function returning TypeVar should receive at least one argument containing the same TypeVar +def f() -> T: pass # E: A function returning TypeVar should have at least one same TypeVar parameter class C: x = f() # E: Need type annotation for "x" From ebc9e73e4c94fda7713a704007e880faef28b795 Mon Sep 17 00:00:00 2001 From: "Kai (Kazuya Ito)" Date: Sat, 28 Feb 2026 16:38:46 +0900 Subject: [PATCH 7/9] Update error message for TypeVar function definition --- test-data/unit/check-errorcodes.test | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test-data/unit/check-errorcodes.test b/test-data/unit/check-errorcodes.test index ba2f23fd7294..5ad49641ac9b 100644 --- a/test-data/unit/check-errorcodes.test +++ b/test-data/unit/check-errorcodes.test @@ -284,7 +284,7 @@ z: y # E: Variable "__main__.y" is not valid as a type [valid-type] \ from typing import TypeVar T = TypeVar('T') -def f() -> T: pass # E: A function returning TypeVar should receive at least one argument containing the same TypeVar [type-var] +def f() -> T: pass # E: A function returning TypeVar should have at least one same TypeVar parameter [type-var] x = f() # E: Need type annotation for "x" [var-annotated] y = [] # E: Need type annotation for "y" (hint: "y: list[] = ...") [var-annotated] [builtins fixtures/list.pyi] From cb69353d64f5f682f6eb69847d20bf27dabd10ef Mon Sep 17 00:00:00 2001 From: "Kai (Kazuya Ito)" Date: Sat, 28 Feb 2026 16:39:58 +0900 Subject: [PATCH 8/9] Update error message for TypeVar parameter requirement --- test-data/unit/check-unreachable-code.test | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test-data/unit/check-unreachable-code.test b/test-data/unit/check-unreachable-code.test index 22c4c80916cb..992a8c687acc 100644 --- a/test-data/unit/check-unreachable-code.test +++ b/test-data/unit/check-unreachable-code.test @@ -1567,7 +1567,7 @@ from typing import TypeVar T = TypeVar("T") class Foo: - def __setitem__(self, key: str, value: str) -> T: # E: A function returning TypeVar should receive at least one argument containing the same TypeVar + def __setitem__(self, key: str, value: str) -> T: # E: A function returning TypeVar should have at least one same TypeVar parameter raise Exception def f() -> None: From 70686dc596d752c2e53a5dd37816aaa3873e121a Mon Sep 17 00:00:00 2001 From: "Kai (Kazuya Ito)" Date: Sat, 28 Feb 2026 16:41:16 +0900 Subject: [PATCH 9/9] Clarify TypeVar parameter requirement in run_job Updated error message for run_job function to clarify TypeVar requirements. --- test-data/unit/check-parameter-specification.test | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test-data/unit/check-parameter-specification.test b/test-data/unit/check-parameter-specification.test index b0808105a385..1d931b5f570d 100644 --- a/test-data/unit/check-parameter-specification.test +++ b/test-data/unit/check-parameter-specification.test @@ -1161,7 +1161,7 @@ def callback(func: Callable[[Any], Any]) -> None: ... class Job(Generic[P]): ... @callback -def run_job(job: Job[...]) -> T: ... # E: A function returning TypeVar should receive at least one argument containing the same TypeVar +def run_job(job: Job[...]) -> T: ... # E: A function returning TypeVar should have at least one same TypeVar parameter [builtins fixtures/tuple.pyi] [case testTupleAndDictOperationsOnParamSpecArgsAndKwargs]