Skip to content

Commit a25c331

Browse files
nuglifeleojiLeo Ji
andauthored
docs: rename "value restriction" to "value-constrained type variable" (#21112)
Closes #17828. The docs used "value restriction" to describe type variables like `T: (str, bytes)`, but the runtime attribute is `TypeVar.__constraints__`. This PR updates the terminology across the affected doc pages to use "value-constrained type variable" consistently, matching the runtime naming and the term discussed in #17828. Changes: - `generics.rst`: rename section title, update prose, add new anchor `value-constrained-type-variables` (old anchor `type-variable-value-restriction` kept for backward compat) - `more_types.rst`: update cross-reference text - `command_line.rst`: update cross-reference text Made with [Cursor](https://cursor.com) Co-authored-by: Leo Ji <nuglifeleoji@gmail.com>
1 parent 465b887 commit a25c331

File tree

3 files changed

+12
-10
lines changed

3 files changed

+12
-10
lines changed

docs/source/command_line.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,7 @@ potentially problematic or redundant in some way.
551551
.. note::
552552

553553
Mypy currently cannot detect and report unreachable or redundant code
554-
inside any functions using :ref:`type-variable-value-restriction`.
554+
inside any functions using :ref:`value-constrained type variables <value-constrained-type-variables>`.
555555

556556
This limitation will be removed in future releases of mypy.
557557

docs/source/generics.rst

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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

701702
By default, a type variable can be replaced with any type -- or any type that
702703
is 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,
732734
since 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

761763
This is thus subtly different from using ``str | bytes`` as an upper bound,
762764
where 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``
764766
actually 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``
775777
or ``bytes``. Regular expressions can be based on a string or a
776778
bytes 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

780782
Note 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
13341336
the same effect as in generic classes and functions.
13351337

13361338

docs/source/more_types.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,8 +303,8 @@ Here is the same example using the legacy syntax (Python 3.11 and earlier):
303303
.. note::
304304

305305
If you just need to constrain a type variable to certain types or
306-
subtypes, you can use a :ref:`value restriction
307-
<type-variable-value-restriction>`.
306+
subtypes, you can use a :ref:`value-constrained type variable
307+
<value-constrained-type-variables>`.
308308

309309
The default values of a function's arguments don't affect its signature -- only
310310
the absence or presence of a default value does. So in order to reduce

0 commit comments

Comments
 (0)