Skip to content

Commit 60d6c78

Browse files
committed
fixup! add bound/variance parameters to TypeVarTuple
1 parent 7a6e221 commit 60d6c78

3 files changed

Lines changed: 17 additions & 12 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
Python 3.9. The `typing` implementation has always raised an error, and the
1414
`typing_extensions` implementation has raised an error on Python 3.10+ since
1515
`typing_extensions` v4.6.0. Patch by Brian Schubert.
16-
- add `bound` and variance parameters to `TypeVarTuple`
16+
- Add `bound` and variance parameters to `TypeVarTuple`.
1717

1818
# Release 4.15.0 (August 25, 2025)
1919

src/test_typing_extensions.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1756,11 +1756,9 @@ def test_annotation_and_optional_default(self):
17561756
annotation : annotation,
17571757
Optional[int] : Optional[int],
17581758
Optional[List[str]] : Optional[List[str]],
1759-
Optional[annotation] : Optional[annotation],
1759+
Optional[annotation] : Optional[annotation],
17601760
Union[str, None, str] : Optional[str],
17611761
Unpack[Tuple[int, None]]: Unpack[Tuple[int, None]],
1762-
# Note: A starred *Ts will use typing.Unpack in 3.11+ see Issue #485
1763-
Unpack[Ts] : Unpack[Ts],
17641762
}
17651763
# contains a ForwardRef, TypeVar(~prefix) or no expression
17661764
do_not_stringify_cases = {
@@ -1776,6 +1774,8 @@ def test_annotation_and_optional_default(self):
17761774
Union[str, "Union[None, StrAlias]"]: Optional[str],
17771775
Union["annotation", T_default] : Union[annotation, T_default],
17781776
Annotated["annotation", "nested"] : Annotated[Union[int, None], "data", "nested"],
1777+
# Note: A starred *Ts will use typing.Unpack in 3.11+ see Issue #485
1778+
Unpack[Ts] : Unpack[Ts],
17791779
}
17801780
if TYPING_3_10_0: # cannot construct UnionTypes before 3.10
17811781
do_not_stringify_cases["str | NoneAlias | StrAlias"] = str | None
@@ -6765,8 +6765,9 @@ def test_repr(self):
67656765
self.assertEqual(repr(Ts_contra), '-Ts_contra')
67666766
self.assertEqual(repr(Ts_infer), 'Ts_infer')
67676767
else:
6768-
# Not worth creating our own version of TypeVarTuple
6769-
# to backport the repr
6768+
# On other versions we use typing.TypeVarTuple, but it is not aware of
6769+
# variance. Not worth creating our own version of TypeVarTuple
6770+
# for this.
67706771
self.assertEqual(repr(Ts), 'Ts')
67716772
self.assertEqual(repr(Ts_2), 'Ts_2')
67726773

@@ -7115,6 +7116,10 @@ def test_typing_extensions_defers_when_possible(self):
71157116
exclude |= {
71167117
'TypeAliasType'
71177118
}
7119+
if sys.version_info < (3, 15):
7120+
exclude |= {
7121+
'TypeVarTuple'
7122+
}
71187123
if not typing_extensions._PEP_728_IMPLEMENTED:
71197124
exclude |= {'TypedDict', 'is_typeddict'}
71207125
for item in typing_extensions.__all__:

src/typing_extensions.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1671,7 +1671,7 @@ def TypeAlias(self, parameters):
16711671

16721672
def _set_default(type_param, default):
16731673
type_param.has_default = lambda: default is not NoDefault
1674-
type_param.__default__ = default
1674+
type_param.__default__ = typing._type_check(default, "Default must be a type.")
16751675

16761676

16771677
def _set_module(typevarlike):
@@ -1824,7 +1824,7 @@ def __new__(cls, name, *, bound=None,
18241824
paramspec = typing.ParamSpec(name, bound=bound,
18251825
covariant=covariant,
18261826
contravariant=contravariant)
1827-
paramspec.__infer_variance__ = infer_variance
1827+
paramspec.__infer_variance__ = bool(infer_variance)
18281828

18291829
_set_default(paramspec, default)
18301830
_set_module(paramspec)
@@ -2593,10 +2593,10 @@ def __new__(cls, name, *, bound=None,
25932593
tvt = typing.TypeVarTuple(name)
25942594
_set_default(tvt, default)
25952595

2596-
tvt.__bound__ = bound
2597-
tvt.__covariant__ = covariant
2598-
tvt.__contravariant__ = contravariant
2599-
tvt.__infer_variance__ = infer_variance
2596+
tvt.__bound__ = typing._type_check(bound, "Bound must be a type.")
2597+
tvt.__covariant__ = bool(covariant)
2598+
tvt.__contravariant__ = bool(contravariant)
2599+
tvt.__infer_variance__ = bool(infer_variance)
26002600

26012601
_set_module(tvt)
26022602

0 commit comments

Comments
 (0)