[3.14] gh-118803: Improve documentation around `ByteString` deprecati… · python/cpython@665a349 · GitHub
Skip to content

Commit 665a349

Browse files
[3.14] gh-118803: Improve documentation around ByteString deprecation (GH-139115) (#139136)
gh-118803: Improve documentation around `ByteString` deprecation (GH-139115) (cherry picked from commit 4305cc3) Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
1 parent e560865 commit 665a349

5 files changed

Lines changed: 93 additions & 19 deletions

File tree

Lines changed: 38 additions & 9 deletions

Doc/library/collections.abc.rst

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -291,9 +291,22 @@ Collections Abstract Base Classes -- Detailed Descriptions
291291

292292
.. deprecated-removed:: 3.12 3.17
293293
The :class:`ByteString` ABC has been deprecated.
294-
For use in type annotations, prefer a union, like ``bytes | bytearray``, or
295-
:class:`collections.abc.Buffer`.
296-
For use as an ABC, prefer :class:`Sequence` or :class:`collections.abc.Buffer`.
294+
295+
Use ``isinstance(obj, collections.abc.Buffer)`` to test if ``obj``
296+
implements the :ref:`buffer protocol <bufferobjects>` at runtime. For use
297+
in type annotations, either use :class:`Buffer` or a union that
298+
explicitly specifies the types your code supports (e.g.,
299+
``bytes | bytearray | memoryview``).
300+
301+
:class:`!ByteString` was originally intended to be an abstract class that
302+
would serve as a supertype of both :class:`bytes` and :class:`bytearray`.
303+
However, since the ABC never had any methods, knowing that an object was
304+
an instance of :class:`!ByteString` never actually told you anything
305+
useful about the object. Other common buffer types such as
306+
:class:`memoryview` were also never understood as subtypes of
307+
:class:`!ByteString` (either at runtime or by static type checkers).
308+
309+
See :pep:`PEP 688 <688#current-options>` for more details.
297310

298311
.. class:: Set
299312
MutableSet

Doc/library/typing.rst

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3790,11 +3790,25 @@ Aliases to container ABCs in :mod:`collections.abc`
37903790

37913791
.. class:: ByteString(Sequence[int])
37923792

3793-
This type represents the types :class:`bytes`, :class:`bytearray`,
3794-
and :class:`memoryview` of byte sequences.
3793+
Deprecated alias to :class:`collections.abc.ByteString`.
3794+
3795+
Use ``isinstance(obj, collections.abc.Buffer)`` to test if ``obj``
3796+
implements the :ref:`buffer protocol <bufferobjects>` at runtime. For use in
3797+
type annotations, either use :class:`~collections.abc.Buffer` or a union
3798+
that explicitly specifies the types your code supports (e.g.,
3799+
``bytes | bytearray | memoryview``).
3800+
3801+
:class:`!ByteString` was originally intended to be an abstract class that
3802+
would serve as a supertype of both :class:`bytes` and :class:`bytearray`.
3803+
However, since the ABC never had any methods, knowing that an object was an
3804+
instance of :class:`!ByteString` never actually told you anything useful
3805+
about the object. Other common buffer types such as :class:`memoryview` were
3806+
also never understood as subtypes of :class:`!ByteString` (either at runtime
3807+
or by static type checkers).
3808+
3809+
See :pep:`PEP 688 <688#current-options>` for more details.
37953810

37963811
.. deprecated-removed:: 3.9 3.17
3797-
Prefer :class:`collections.abc.Buffer`, or a union like ``bytes | bytearray | memoryview``.
37983812

37993813
.. class:: Collection(Sized, Iterable[T_co], Container[T_co])
38003814

Doc/whatsnew/3.12.rst

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1192,8 +1192,22 @@ Deprecated
11921192
(Contributed by Prince Roshan in :gh:`103636`.)
11931193

11941194
* :mod:`collections.abc`: Deprecated :class:`collections.abc.ByteString`.
1195-
Prefer :class:`Sequence` or :class:`collections.abc.Buffer`.
1196-
For use in type annotations, prefer a union, like ``bytes | bytearray``, or :class:`collections.abc.Buffer`.
1195+
1196+
Use ``isinstance(obj, collections.abc.Buffer)`` to test if ``obj`` implements
1197+
the :ref:`buffer protocol <bufferobjects>` at runtime. For use in type
1198+
annotations, either use :class:`~collections.abc.Buffer` or a union
1199+
that explicitly specifies the types your code supports (e.g.,
1200+
``bytes | bytearray | memoryview``).
1201+
1202+
:class:`!ByteString` was originally intended to be an abstract class that
1203+
would serve as a supertype of both :class:`bytes` and :class:`bytearray`.
1204+
However, since the ABC never had any methods, knowing that an object was an
1205+
instance of :class:`!ByteString` never actually told you anything useful
1206+
about the object. Other common buffer types such as :class:`memoryview` were
1207+
also never understood as subtypes of :class:`!ByteString` (either at
1208+
runtime or by static type checkers).
1209+
1210+
See :pep:`PEP 688 <688#current-options>` for more details.
11971211
(Contributed by Shantanu Jain in :gh:`91896`.)
11981212

11991213
* :mod:`datetime`: :class:`datetime.datetime`'s :meth:`~datetime.datetime.utcnow` and

Lib/_collections_abc.py

Lines changed: 6 additions & 2 deletions

0 commit comments

Comments
 (0)