Skip to content

Commit 9012c55

Browse files
[3.12] More reorganisation of the typing docs (GH-105787) (#105810)
More reorganisation of the typing docs (GH-105787) (cherry picked from commit da911a6) Co-authored-by: Alex Waygood <[email protected]>
1 parent 1573f16 commit 9012c55

File tree

1 file changed

+117
-101
lines changed

1 file changed

+117
-101
lines changed

Doc/library/typing.rst

+117-101
Original file line numberDiff line numberDiff line change
@@ -2420,6 +2420,18 @@ These protocols are decorated with :func:`runtime_checkable`.
24202420
An ABC with one abstract method ``__round__``
24212421
that is covariant in its return type.
24222422

2423+
ABCs for working with IO
2424+
------------------------
2425+
2426+
.. class:: IO
2427+
TextIO
2428+
BinaryIO
2429+
2430+
Generic type ``IO[AnyStr]`` and its subclasses ``TextIO(IO[str])``
2431+
and ``BinaryIO(IO[bytes])``
2432+
represent the types of I/O streams such as returned by
2433+
:func:`open`.
2434+
24232435
Functions and decorators
24242436
------------------------
24252437

@@ -3007,11 +3019,15 @@ Constant
30073019

30083020
.. versionadded:: 3.5.2
30093021

3010-
Generic concrete collections
3011-
----------------------------
3022+
.. _generic-concrete-collections:
30123023

3013-
Corresponding to built-in types
3014-
"""""""""""""""""""""""""""""""
3024+
Deprecated aliases
3025+
------------------
3026+
3027+
.. _corresponding-to-built-in-types:
3028+
3029+
Aliases to built-in types
3030+
"""""""""""""""""""""""""
30153031

30163032
.. class:: Dict(dict, MutableMapping[KT, VT])
30173033

@@ -3073,8 +3089,10 @@ Corresponding to built-in types
30733089

30743090
.. note:: :data:`Tuple` is a special form.
30753091

3076-
Corresponding to types in :mod:`collections`
3077-
""""""""""""""""""""""""""""""""""""""""""""
3092+
.. _corresponding-to-types-in-collections:
3093+
3094+
Aliases to types in :mod:`collections`
3095+
""""""""""""""""""""""""""""""""""""""
30783096

30793097
.. class:: DefaultDict(collections.defaultdict, MutableMapping[KT, VT])
30803098

@@ -3129,17 +3147,10 @@ Corresponding to types in :mod:`collections`
31293147
:class:`collections.deque` now supports subscripting (``[]``).
31303148
See :pep:`585` and :ref:`types-genericalias`.
31313149

3132-
Other concrete types
3133-
""""""""""""""""""""
3150+
.. _other-concrete-types:
31343151

3135-
.. class:: IO
3136-
TextIO
3137-
BinaryIO
3138-
3139-
Generic type ``IO[AnyStr]`` and its subclasses ``TextIO(IO[str])``
3140-
and ``BinaryIO(IO[bytes])``
3141-
represent the types of I/O streams such as returned by
3142-
:func:`open`.
3152+
Aliases to other concrete types
3153+
"""""""""""""""""""""""""""""""
31433154

31443155
.. deprecated-removed:: 3.8 3.13
31453156
The ``typing.io`` namespace is deprecated and will be removed.
@@ -3186,11 +3197,11 @@ Other concrete types
31863197
currently planned, but users are encouraged to use
31873198
:class:`str` instead of ``Text``.
31883199

3189-
Abstract Base Classes
3190-
---------------------
3200+
.. _abstract-base-classes:
3201+
.. _corresponding-to-collections-in-collections-abc:
31913202

3192-
Corresponding to collections in :mod:`collections.abc`
3193-
""""""""""""""""""""""""""""""""""""""""""""""""""""""
3203+
Aliases to container ABCs in :mod:`collections.abc`
3204+
"""""""""""""""""""""""""""""""""""""""""""""""""""
31943205

31953206
.. class:: AbstractSet(Collection[T_co])
31963207

@@ -3305,86 +3316,10 @@ Corresponding to collections in :mod:`collections.abc`
33053316
:class:`collections.abc.ValuesView` now supports subscripting (``[]``).
33063317
See :pep:`585` and :ref:`types-genericalias`.
33073318

3308-
Corresponding to other types in :mod:`collections.abc`
3309-
""""""""""""""""""""""""""""""""""""""""""""""""""""""
3310-
3311-
.. class:: Iterable(Generic[T_co])
3312-
3313-
Deprecated alias to :class:`collections.abc.Iterable`.
3314-
3315-
.. deprecated:: 3.9
3316-
:class:`collections.abc.Iterable` now supports subscripting (``[]``).
3317-
See :pep:`585` and :ref:`types-genericalias`.
3318-
3319-
.. class:: Iterator(Iterable[T_co])
3320-
3321-
Deprecated alias to :class:`collections.abc.Iterator`.
3322-
3323-
.. deprecated:: 3.9
3324-
:class:`collections.abc.Iterator` now supports subscripting (``[]``).
3325-
See :pep:`585` and :ref:`types-genericalias`.
3326-
3327-
.. class:: Generator(Iterator[YieldType], Generic[YieldType, SendType, ReturnType])
3328-
3329-
Deprecated alias to :class:`collections.abc.Generator`.
3330-
3331-
A generator can be annotated by the generic type
3332-
``Generator[YieldType, SendType, ReturnType]``. For example::
3333-
3334-
def echo_round() -> Generator[int, float, str]:
3335-
sent = yield 0
3336-
while sent >= 0:
3337-
sent = yield round(sent)
3338-
return 'Done'
3339-
3340-
Note that unlike many other generics in the typing module, the ``SendType``
3341-
of :class:`Generator` behaves contravariantly, not covariantly or
3342-
invariantly.
3343-
3344-
If your generator will only yield values, set the ``SendType`` and
3345-
``ReturnType`` to ``None``::
3346-
3347-
def infinite_stream(start: int) -> Generator[int, None, None]:
3348-
while True:
3349-
yield start
3350-
start += 1
3351-
3352-
Alternatively, annotate your generator as having a return type of
3353-
either ``Iterable[YieldType]`` or ``Iterator[YieldType]``::
3354-
3355-
def infinite_stream(start: int) -> Iterator[int]:
3356-
while True:
3357-
yield start
3358-
start += 1
3359-
3360-
.. deprecated:: 3.9
3361-
:class:`collections.abc.Generator` now supports subscripting (``[]``).
3362-
See :pep:`585` and :ref:`types-genericalias`.
3363-
3364-
.. class:: Hashable
3365-
3366-
Deprecated alias to :class:`collections.abc.Hashable`.
3367-
3368-
.. deprecated:: 3.12
3369-
Use :class:`collections.abc.Hashable` directly instead.
3370-
3371-
.. class:: Reversible(Iterable[T_co])
3372-
3373-
Deprecated alias to :class:`collections.abc.Reversible`.
3319+
.. _asynchronous-programming:
33743320

3375-
.. deprecated:: 3.9
3376-
:class:`collections.abc.Reversible` now supports subscripting (``[]``).
3377-
See :pep:`585` and :ref:`types-genericalias`.
3378-
3379-
.. class:: Sized
3380-
3381-
Deprecated alias to :class:`collections.abc.Sized`.
3382-
3383-
.. deprecated:: 3.12
3384-
Use :class:`collections.abc.Sized` directly instead.
3385-
3386-
Asynchronous programming
3387-
""""""""""""""""""""""""
3321+
Aliases to asynchronous ABCs in :mod:`collections.abc`
3322+
""""""""""""""""""""""""""""""""""""""""""""""""""""""
33883323

33893324
.. class:: Coroutine(Awaitable[ReturnType], Generic[YieldType, SendType, ReturnType])
33903325

@@ -3475,9 +3410,90 @@ Asynchronous programming
34753410
:class:`collections.abc.Awaitable` now supports subscripting (``[]``).
34763411
See :pep:`585` and :ref:`types-genericalias`.
34773412

3413+
.. _corresponding-to-other-types-in-collections-abc:
3414+
3415+
Aliases to other ABCs in :mod:`collections.abc`
3416+
"""""""""""""""""""""""""""""""""""""""""""""""
3417+
3418+
.. class:: Iterable(Generic[T_co])
3419+
3420+
Deprecated alias to :class:`collections.abc.Iterable`.
3421+
3422+
.. deprecated:: 3.9
3423+
:class:`collections.abc.Iterable` now supports subscripting (``[]``).
3424+
See :pep:`585` and :ref:`types-genericalias`.
3425+
3426+
.. class:: Iterator(Iterable[T_co])
3427+
3428+
Deprecated alias to :class:`collections.abc.Iterator`.
3429+
3430+
.. deprecated:: 3.9
3431+
:class:`collections.abc.Iterator` now supports subscripting (``[]``).
3432+
See :pep:`585` and :ref:`types-genericalias`.
3433+
3434+
.. class:: Generator(Iterator[YieldType], Generic[YieldType, SendType, ReturnType])
3435+
3436+
Deprecated alias to :class:`collections.abc.Generator`.
3437+
3438+
A generator can be annotated by the generic type
3439+
``Generator[YieldType, SendType, ReturnType]``. For example::
3440+
3441+
def echo_round() -> Generator[int, float, str]:
3442+
sent = yield 0
3443+
while sent >= 0:
3444+
sent = yield round(sent)
3445+
return 'Done'
3446+
3447+
Note that unlike many other generics in the typing module, the ``SendType``
3448+
of :class:`Generator` behaves contravariantly, not covariantly or
3449+
invariantly.
3450+
3451+
If your generator will only yield values, set the ``SendType`` and
3452+
``ReturnType`` to ``None``::
3453+
3454+
def infinite_stream(start: int) -> Generator[int, None, None]:
3455+
while True:
3456+
yield start
3457+
start += 1
3458+
3459+
Alternatively, annotate your generator as having a return type of
3460+
either ``Iterable[YieldType]`` or ``Iterator[YieldType]``::
3461+
3462+
def infinite_stream(start: int) -> Iterator[int]:
3463+
while True:
3464+
yield start
3465+
start += 1
3466+
3467+
.. deprecated:: 3.9
3468+
:class:`collections.abc.Generator` now supports subscripting (``[]``).
3469+
See :pep:`585` and :ref:`types-genericalias`.
3470+
3471+
.. class:: Hashable
3472+
3473+
Deprecated alias to :class:`collections.abc.Hashable`.
3474+
3475+
.. deprecated:: 3.12
3476+
Use :class:`collections.abc.Hashable` directly instead.
3477+
3478+
.. class:: Reversible(Iterable[T_co])
3479+
3480+
Deprecated alias to :class:`collections.abc.Reversible`.
3481+
3482+
.. deprecated:: 3.9
3483+
:class:`collections.abc.Reversible` now supports subscripting (``[]``).
3484+
See :pep:`585` and :ref:`types-genericalias`.
3485+
3486+
.. class:: Sized
3487+
3488+
Deprecated alias to :class:`collections.abc.Sized`.
3489+
3490+
.. deprecated:: 3.12
3491+
Use :class:`collections.abc.Sized` directly instead.
3492+
3493+
.. _context-manager-types:
34783494

3479-
Context manager types
3480-
"""""""""""""""""""""
3495+
Aliases to :mod:`contextlib` ABCs
3496+
"""""""""""""""""""""""""""""""""
34813497

34823498
.. class:: ContextManager(Generic[T_co])
34833499

0 commit comments

Comments
 (0)