@@ -693,10 +693,11 @@ contravariant, use type variables defined with special keyword arguments
693693 my_box = Box(Square())
694694 look_into(my_box) # OK, but mypy would complain here for an invariant type
695695
696+ .. _value-constrained-type-variables :
696697.. _type-variable-value-restriction :
697698
698- Type variables with value restriction
699- *************************************
699+ Value-constrained type variables
700+ *********************************
700701
701702By default, a type variable can be replaced with any type -- or any type that
702703is a subtype of the upper bound, which defaults to ``object ``. However, sometimes
@@ -727,8 +728,9 @@ The same thing is also possibly using the legacy syntax (Python 3.11 or earlier)
727728 def concat (x : AnyStr, y : AnyStr) -> AnyStr:
728729 return x + y
729730
730- No matter which syntax you use, such a type variable is called a type variable
731- with a value restriction. Importantly, this is different from a union type,
731+ No matter which syntax you use, such a type variable is called a
732+ value-constrained type variable (the allowed types are accessible at runtime
733+ via ``TypeVar.__constraints__ ``). Importantly, this is different from a union type,
732734since combinations of ``str `` and ``bytes `` are not accepted:
733735
734736.. code-block :: python
@@ -760,7 +762,7 @@ for the type variable, which in this case is ``str``.
760762
761763This is thus subtly different from using ``str | bytes `` as an upper bound,
762764where the return type would be ``S `` (see :ref: `type-variable-upper-bound `).
763- Using a value restriction is correct for ``concat ``, since ``concat ``
765+ Using a value constraint is correct for ``concat ``, since ``concat ``
764766actually returns a ``str `` instance in the above example:
765767
766768.. code-block :: python
@@ -775,7 +777,7 @@ value of :py:func:`re.compile`, where ``S`` can be either ``str``
775777or ``bytes ``. Regular expressions can be based on a string or a
776778bytes pattern.
777779
778- A type variable may not have both a value restriction and an upper bound.
780+ A type variable may not have both value constraints and an upper bound.
779781
780782Note that you may come across :py:data: `~typing.AnyStr ` imported from
781783:py:mod: `typing `. This feature is now deprecated, but it means the same
@@ -1330,7 +1332,7 @@ Here are examples using the legacy syntax (Python 3.11 and earlier):
13301332 for i, j in NewVec[int ]():
13311333 ...
13321334
1333- Using type variable bounds or value restriction in generic aliases has
1335+ Using type variable bounds or value constraints in generic aliases has
13341336the same effect as in generic classes and functions.
13351337
13361338
0 commit comments