From 6ec5d82ef5c84bf9e94fd0b79ba57fb485b6fc5a Mon Sep 17 00:00:00 2001 From: Leo Ji Date: Wed, 1 Apr 2026 01:33:05 +0000 Subject: [PATCH] fix: use "constraints and a bound" in TypeVar error message PEP 484 uses "constraints" for the positional arguments of TypeVar (e.g. TypeVar("T", int, str)) and "bound" for the keyword argument. The existing message "cannot have both values and an upper bound" used neither term correctly. Updated to: TypeVar cannot have both constraints and a bound Closes #20973 Made-with: Cursor --- mypy/semanal.py | 2 +- test-data/unit/semanal-errors.test | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/mypy/semanal.py b/mypy/semanal.py index efd656682bc78..45aa8ae390413 100644 --- a/mypy/semanal.py +++ b/mypy/semanal.py @@ -4936,7 +4936,7 @@ def process_typevar_parameters( return None elif param_name == "bound": if has_values: - self.fail("TypeVar cannot have both values and an upper bound", context) + self.fail("TypeVar cannot have both constraints and a bound", context) return None tv_arg = self.get_typevarlike_argument("TypeVar", param_name, param_value, context) if tv_arg is None: diff --git a/test-data/unit/semanal-errors.test b/test-data/unit/semanal-errors.test index 7e1f737c61b21..26358958ce425 100644 --- a/test-data/unit/semanal-errors.test +++ b/test-data/unit/semanal-errors.test @@ -1062,7 +1062,7 @@ k = TypeVar('k', contravariant=1) # E: TypeVar "contravariant" may only be a lit [case testMoreInvalidTypevarArguments] from typing import TypeVar -T = TypeVar('T', int, str, bound=bool) # E: TypeVar cannot have both values and an upper bound +T = TypeVar('T', int, str, bound=bool) # E: TypeVar cannot have both constraints and a bound S = TypeVar('S', covariant=True, contravariant=True) \ # E: TypeVar cannot be both covariant and contravariant [builtins fixtures/bool.pyi]