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 <[email protected]>
1 parent e560865 commit 665a349

File tree

5 files changed

+93
-19
lines changed

5 files changed

+93
-19
lines changed
Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,28 @@
11
Pending removal in Python 3.17
22
------------------------------
33

4+
* :mod:`collections.abc`:
5+
6+
- :class:`collections.abc.ByteString` is scheduled for removal in Python 3.17.
7+
8+
Use ``isinstance(obj, collections.abc.Buffer)`` to test if ``obj``
9+
implements the :ref:`buffer protocol <bufferobjects>` at runtime. For use
10+
in type annotations, either use :class:`~collections.abc.Buffer` or a union
11+
that explicitly specifies the types your code supports (e.g.,
12+
``bytes | bytearray | memoryview``).
13+
14+
:class:`!ByteString` was originally intended to be an abstract class that
15+
would serve as a supertype of both :class:`bytes` and :class:`bytearray`.
16+
However, since the ABC never had any methods, knowing that an object was an
17+
instance of :class:`!ByteString` never actually told you anything useful
18+
about the object. Other common buffer types such as :class:`memoryview`
19+
were also never understood as subtypes of :class:`!ByteString` (either at
20+
runtime or by static type checkers).
21+
22+
See :pep:`PEP 688 <688#current-options>` for more details.
23+
(Contributed by Shantanu Jain in :gh:`91896`.)
24+
25+
426
* :mod:`typing`:
527

628
- Before Python 3.14, old-style unions were implemented using the private class
@@ -9,14 +31,21 @@ Pending removal in Python 3.17
931
3.17. Users should use documented introspection helpers like :func:`typing.get_origin`
1032
and :func:`typing.get_args` instead of relying on private implementation details.
1133
- :class:`typing.ByteString`, deprecated since Python 3.9, is scheduled for removal in
12-
Python 3.17. Prefer :class:`~collections.abc.Sequence` or
13-
:class:`~collections.abc.Buffer`. For use in type annotations, prefer a union, like
14-
``bytes | bytearray``, or :class:`collections.abc.Buffer`.
15-
(Contributed by Shantanu Jain in :gh:`91896`.)
34+
Python 3.17.
1635

17-
* :mod:`collections.abc`:
36+
Use ``isinstance(obj, collections.abc.Buffer)`` to test if ``obj``
37+
implements the :ref:`buffer protocol <bufferobjects>` at runtime. For use
38+
in type annotations, either use :class:`~collections.abc.Buffer` or a union
39+
that explicitly specifies the types your code supports (e.g.,
40+
``bytes | bytearray | memoryview``).
41+
42+
:class:`!ByteString` was originally intended to be an abstract class that
43+
would serve as a supertype of both :class:`bytes` and :class:`bytearray`.
44+
However, since the ABC never had any methods, knowing that an object was an
45+
instance of :class:`!ByteString` never actually told you anything useful
46+
about the object. Other common buffer types such as :class:`memoryview`
47+
were also never understood as subtypes of :class:`!ByteString` (either at
48+
runtime or by static type checkers).
1849

19-
- :class:`collections.abc.ByteString` is scheduled for removal in Python 3.17. Prefer
20-
:class:`~collections.abc.Sequence` or :class:`~collections.abc.Buffer`. For use in
21-
type annotations, prefer a union, like ``bytes | bytearray``, or
22-
:class:`collections.abc.Buffer`. (Contributed by Shantanu Jain in :gh:`91896`.)
50+
See :pep:`PEP 688 <688#current-options>` for more details.
51+
(Contributed by Shantanu Jain in :gh:`91896`.)

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
Original file line numberDiff line numberDiff line change
@@ -1082,9 +1082,13 @@ def __instancecheck__(cls, instance):
10821082
return super().__instancecheck__(instance)
10831083

10841084
class ByteString(Sequence, metaclass=_DeprecateByteStringMeta):
1085-
"""This unifies bytes and bytearray.
1085+
"""Deprecated ABC serving as a common supertype of ``bytes`` and ``bytearray``.
10861086
1087-
XXX Should add all their methods.
1087+
This ABC is scheduled for removal in Python 3.17.
1088+
Use ``isinstance(obj, collections.abc.Buffer)`` to test if ``obj``
1089+
implements the buffer protocol at runtime. For use in type annotations,
1090+
either use ``Buffer`` or a union that explicitly specifies the types your
1091+
code supports (e.g., ``bytes | bytearray | memoryview``).
10881092
"""
10891093

10901094
__slots__ = ()

0 commit comments

Comments
 (0)