Skip to content

Commit abb32de

Browse files
[3.11] typing docs: Move some classes out of the "Generics" section (GH-104707) (#104721)
- AnyStr can be used in type annotations, contrary to the section header - Unpack can also be used in annotations, and its use is not restricted to generics. It makes more sense with other building blocks like Required. - Protocol is not necessarily generic. Also fix the indentation for two notes associated with Concatenate. (cherry picked from commit ab71acd) --------- Co-authored-by: Alex Waygood <[email protected]>
1 parent dc0c41b commit abb32de

File tree

1 file changed

+125
-122
lines changed

1 file changed

+125
-122
lines changed

Doc/library/typing.rst

Lines changed: 125 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,21 @@ These can be used as types in annotations and do not support ``[]``.
611611
avoiding type checker errors with classes that can duck type anywhere or
612612
are highly dynamic.
613613

614+
.. data:: AnyStr
615+
616+
``AnyStr`` is a :ref:`constrained type variable <typing-constrained-typevar>` defined as
617+
``AnyStr = TypeVar('AnyStr', str, bytes)``.
618+
619+
It is meant to be used for functions that may accept any kind of string
620+
without allowing different kinds of strings to mix. For example::
621+
622+
def concat(a: AnyStr, b: AnyStr) -> AnyStr:
623+
return a + b
624+
625+
concat(u"foo", u"bar") # Ok, output has type 'unicode'
626+
concat(b"foo", b"bar") # Ok, output has type 'bytes'
627+
concat(u"foo", b"bar") # Error, cannot mix unicode and bytes
628+
614629
.. data:: LiteralString
615630

616631
Special type that includes only literal strings. A string
@@ -912,13 +927,13 @@ These can be used as types in annotations using ``[]``, each having a unique syn
912927
# We don't need to pass in the lock ourselves thanks to the decorator.
913928
sum_threadsafe([1.1, 2.2, 3.3])
914929

915-
.. versionadded:: 3.10
930+
.. versionadded:: 3.10
916931

917-
.. seealso::
932+
.. seealso::
918933

919-
* :pep:`612` -- Parameter Specification Variables (the PEP which introduced
920-
``ParamSpec`` and ``Concatenate``).
921-
* :class:`ParamSpec` and :class:`Callable`.
934+
* :pep:`612` -- Parameter Specification Variables (the PEP which introduced
935+
``ParamSpec`` and ``Concatenate``).
936+
* :class:`ParamSpec` and :class:`Callable`.
922937

923938

924939
.. class:: Type(Generic[CT_co])
@@ -1203,6 +1218,34 @@ These can be used as types in annotations using ``[]``, each having a unique syn
12031218
.. versionadded:: 3.10
12041219

12051220

1221+
.. data:: Unpack
1222+
1223+
A typing operator that conceptually marks an object as having been
1224+
unpacked. For example, using the unpack operator ``*`` on a
1225+
:class:`type variable tuple <TypeVarTuple>` is equivalent to using ``Unpack``
1226+
to mark the type variable tuple as having been unpacked::
1227+
1228+
Ts = TypeVarTuple('Ts')
1229+
tup: tuple[*Ts]
1230+
# Effectively does:
1231+
tup: tuple[Unpack[Ts]]
1232+
1233+
In fact, ``Unpack`` can be used interchangeably with ``*`` in the context
1234+
of :class:`typing.TypeVarTuple <TypeVarTuple>` and
1235+
:class:`builtins.tuple <tuple>` types. You might see ``Unpack`` being used
1236+
explicitly in older versions of Python, where ``*`` couldn't be used in
1237+
certain places::
1238+
1239+
# In older versions of Python, TypeVarTuple and Unpack
1240+
# are located in the `typing_extensions` backports package.
1241+
from typing_extensions import TypeVarTuple, Unpack
1242+
1243+
Ts = TypeVarTuple('Ts')
1244+
tup: tuple[*Ts] # Syntax error on Python <= 3.10!
1245+
tup: tuple[Unpack[Ts]] # Semantically equivalent, and backwards-compatible
1246+
1247+
.. versionadded:: 3.11
1248+
12061249
Building generic types
12071250
""""""""""""""""""""""
12081251

@@ -1404,32 +1447,6 @@ These are not used in annotations. They are building blocks for creating generic
14041447

14051448
.. versionadded:: 3.11
14061449

1407-
.. data:: Unpack
1408-
1409-
A typing operator that conceptually marks an object as having been
1410-
unpacked. For example, using the unpack operator ``*`` on a
1411-
:class:`type variable tuple <TypeVarTuple>` is equivalent to using ``Unpack``
1412-
to mark the type variable tuple as having been unpacked::
1413-
1414-
Ts = TypeVarTuple('Ts')
1415-
tup: tuple[*Ts]
1416-
# Effectively does:
1417-
tup: tuple[Unpack[Ts]]
1418-
1419-
In fact, ``Unpack`` can be used interchangeably with ``*`` in the context
1420-
of types. You might see ``Unpack`` being used explicitly in older versions
1421-
of Python, where ``*`` couldn't be used in certain places::
1422-
1423-
# In older versions of Python, TypeVarTuple and Unpack
1424-
# are located in the `typing_extensions` backports package.
1425-
from typing_extensions import TypeVarTuple, Unpack
1426-
1427-
Ts = TypeVarTuple('Ts')
1428-
tup: tuple[*Ts] # Syntax error on Python <= 3.10!
1429-
tup: tuple[Unpack[Ts]] # Semantically equivalent, and backwards-compatible
1430-
1431-
.. versionadded:: 3.11
1432-
14331450
.. class:: ParamSpec(name, *, bound=None, covariant=False, contravariant=False)
14341451

14351452
Parameter specification variable. A specialized version of
@@ -1528,97 +1545,6 @@ These are not used in annotations. They are building blocks for creating generic
15281545
.. versionadded:: 3.10
15291546

15301547

1531-
.. data:: AnyStr
1532-
1533-
``AnyStr`` is a :ref:`constrained type variable <typing-constrained-typevar>` defined as
1534-
``AnyStr = TypeVar('AnyStr', str, bytes)``.
1535-
1536-
It is meant to be used for functions that may accept any kind of string
1537-
without allowing different kinds of strings to mix. For example::
1538-
1539-
def concat(a: AnyStr, b: AnyStr) -> AnyStr:
1540-
return a + b
1541-
1542-
concat(u"foo", u"bar") # Ok, output has type 'unicode'
1543-
concat(b"foo", b"bar") # Ok, output has type 'bytes'
1544-
concat(u"foo", b"bar") # Error, cannot mix unicode and bytes
1545-
1546-
.. class:: Protocol(Generic)
1547-
1548-
Base class for protocol classes. Protocol classes are defined like this::
1549-
1550-
class Proto(Protocol):
1551-
def meth(self) -> int:
1552-
...
1553-
1554-
Such classes are primarily used with static type checkers that recognize
1555-
structural subtyping (static duck-typing), for example::
1556-
1557-
class C:
1558-
def meth(self) -> int:
1559-
return 0
1560-
1561-
def func(x: Proto) -> int:
1562-
return x.meth()
1563-
1564-
func(C()) # Passes static type check
1565-
1566-
See :pep:`544` for more details. Protocol classes decorated with
1567-
:func:`runtime_checkable` (described later) act as simple-minded runtime
1568-
protocols that check only the presence of given attributes, ignoring their
1569-
type signatures.
1570-
1571-
Protocol classes can be generic, for example::
1572-
1573-
class GenProto(Protocol[T]):
1574-
def meth(self) -> T:
1575-
...
1576-
1577-
.. versionadded:: 3.8
1578-
1579-
.. decorator:: runtime_checkable
1580-
1581-
Mark a protocol class as a runtime protocol.
1582-
1583-
Such a protocol can be used with :func:`isinstance` and :func:`issubclass`.
1584-
This raises :exc:`TypeError` when applied to a non-protocol class. This
1585-
allows a simple-minded structural check, very similar to "one trick ponies"
1586-
in :mod:`collections.abc` such as :class:`~collections.abc.Iterable`. For example::
1587-
1588-
@runtime_checkable
1589-
class Closable(Protocol):
1590-
def close(self): ...
1591-
1592-
assert isinstance(open('/some/file'), Closable)
1593-
1594-
@runtime_checkable
1595-
class Named(Protocol):
1596-
name: str
1597-
1598-
import threading
1599-
assert isinstance(threading.Thread(name='Bob'), Named)
1600-
1601-
.. note::
1602-
1603-
:func:`!runtime_checkable` will check only the presence of the required
1604-
methods or attributes, not their type signatures or types.
1605-
For example, :class:`ssl.SSLObject`
1606-
is a class, therefore it passes an :func:`issubclass`
1607-
check against :data:`Callable`. However, the
1608-
``ssl.SSLObject.__init__`` method exists only to raise a
1609-
:exc:`TypeError` with a more informative message, therefore making
1610-
it impossible to call (instantiate) :class:`ssl.SSLObject`.
1611-
1612-
.. note::
1613-
1614-
An :func:`isinstance` check against a runtime-checkable protocol can be
1615-
surprisingly slow compared to an ``isinstance()`` check against
1616-
a non-protocol class. Consider using alternative idioms such as
1617-
:func:`hasattr` calls for structural checks in performance-sensitive
1618-
code.
1619-
1620-
.. versionadded:: 3.8
1621-
16221548
Other special directives
16231549
""""""""""""""""""""""""
16241550

@@ -1707,6 +1633,83 @@ These are not used in annotations. They are building blocks for declaring types.
17071633
.. versionchanged:: 3.10
17081634
``NewType`` is now a class rather than a function.
17091635

1636+
.. class:: Protocol(Generic)
1637+
1638+
Base class for protocol classes. Protocol classes are defined like this::
1639+
1640+
class Proto(Protocol):
1641+
def meth(self) -> int:
1642+
...
1643+
1644+
Such classes are primarily used with static type checkers that recognize
1645+
structural subtyping (static duck-typing), for example::
1646+
1647+
class C:
1648+
def meth(self) -> int:
1649+
return 0
1650+
1651+
def func(x: Proto) -> int:
1652+
return x.meth()
1653+
1654+
func(C()) # Passes static type check
1655+
1656+
See :pep:`544` for more details. Protocol classes decorated with
1657+
:func:`runtime_checkable` (described later) act as simple-minded runtime
1658+
protocols that check only the presence of given attributes, ignoring their
1659+
type signatures.
1660+
1661+
Protocol classes can be generic, for example::
1662+
1663+
class GenProto(Protocol[T]):
1664+
def meth(self) -> T:
1665+
...
1666+
1667+
.. versionadded:: 3.8
1668+
1669+
.. decorator:: runtime_checkable
1670+
1671+
Mark a protocol class as a runtime protocol.
1672+
1673+
Such a protocol can be used with :func:`isinstance` and :func:`issubclass`.
1674+
This raises :exc:`TypeError` when applied to a non-protocol class. This
1675+
allows a simple-minded structural check, very similar to "one trick ponies"
1676+
in :mod:`collections.abc` such as :class:`~collections.abc.Iterable`. For example::
1677+
1678+
@runtime_checkable
1679+
class Closable(Protocol):
1680+
def close(self): ...
1681+
1682+
assert isinstance(open('/some/file'), Closable)
1683+
1684+
@runtime_checkable
1685+
class Named(Protocol):
1686+
name: str
1687+
1688+
import threading
1689+
assert isinstance(threading.Thread(name='Bob'), Named)
1690+
1691+
.. note::
1692+
1693+
:func:`!runtime_checkable` will check only the presence of the required
1694+
methods or attributes, not their type signatures or types.
1695+
For example, :class:`ssl.SSLObject`
1696+
is a class, therefore it passes an :func:`issubclass`
1697+
check against :data:`Callable`. However, the
1698+
``ssl.SSLObject.__init__`` method exists only to raise a
1699+
:exc:`TypeError` with a more informative message, therefore making
1700+
it impossible to call (instantiate) :class:`ssl.SSLObject`.
1701+
1702+
.. note::
1703+
1704+
An :func:`isinstance` check against a runtime-checkable protocol can be
1705+
surprisingly slow compared to an ``isinstance()`` check against
1706+
a non-protocol class. Consider using alternative idioms such as
1707+
:func:`hasattr` calls for structural checks in performance-sensitive
1708+
code.
1709+
1710+
.. versionadded:: 3.8
1711+
1712+
17101713
.. class:: TypedDict(dict)
17111714

17121715
Special construct to add type hints to a dictionary.

0 commit comments

Comments
 (0)