Skip to content

Commit 8246e25

Browse files
authored
[3.13] gh-118803: Improve documentation around ByteString deprecation (#139115) (#139137)
1 parent 7de5d02 commit 8246e25

File tree

7 files changed

+114
-12
lines changed

7 files changed

+114
-12
lines changed

Doc/deprecations/index.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ Deprecations
77

88
.. include:: pending-removal-in-3.16.rst
99

10+
.. include:: pending-removal-in-3.17.rst
11+
1012
.. include:: pending-removal-in-future.rst
1113

1214
C API Deprecations
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
Pending removal in Python 3.17
2+
------------------------------
3+
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+
26+
* :mod:`typing`:
27+
28+
- Before Python 3.14, old-style unions were implemented using the private class
29+
``typing._UnionGenericAlias``. This class is no longer needed for the implementation,
30+
but it has been retained for backward compatibility, with removal scheduled for Python
31+
3.17. Users should use documented introspection helpers like :func:`typing.get_origin`
32+
and :func:`typing.get_args` instead of relying on private implementation details.
33+
- :class:`typing.ByteString`, deprecated since Python 3.9, is scheduled for removal in
34+
Python 3.17.
35+
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).
49+
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: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -289,11 +289,24 @@ Collections Abstract Base Classes -- Detailed Descriptions
289289
The :meth:`~sequence.index` method gained support for
290290
the *stop* and *start* arguments.
291291

292-
.. deprecated-removed:: 3.12 3.14
292+
.. deprecated-removed:: 3.12 3.17
293293
The :class:`ByteString` ABC has been deprecated.
294-
For use in typing, 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: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3638,11 +3638,25 @@ Aliases to container ABCs in :mod:`collections.abc`
36383638

36393639
.. class:: ByteString(Sequence[int])
36403640

3641-
This type represents the types :class:`bytes`, :class:`bytearray`,
3642-
and :class:`memoryview` of byte sequences.
3641+
Deprecated alias to :class:`collections.abc.ByteString`.
36433642

3644-
.. deprecated-removed:: 3.9 3.14
3645-
Prefer :class:`collections.abc.Buffer`, or a union like ``bytes | bytearray | memoryview``.
3643+
Use ``isinstance(obj, collections.abc.Buffer)`` to test if ``obj``
3644+
implements the :ref:`buffer protocol <bufferobjects>` at runtime. For use in
3645+
type annotations, either use :class:`~collections.abc.Buffer` or a union
3646+
that explicitly specifies the types your code supports (e.g.,
3647+
``bytes | bytearray | memoryview``).
3648+
3649+
:class:`!ByteString` was originally intended to be an abstract class that
3650+
would serve as a supertype of both :class:`bytes` and :class:`bytearray`.
3651+
However, since the ABC never had any methods, knowing that an object was an
3652+
instance of :class:`!ByteString` never actually told you anything useful
3653+
about the object. Other common buffer types such as :class:`memoryview` were
3654+
also never understood as subtypes of :class:`!ByteString` (either at runtime
3655+
or by static type checkers).
3656+
3657+
See :pep:`PEP 688 <688#current-options>` for more details.
3658+
3659+
.. deprecated-removed:: 3.9 3.17
36463660

36473661
.. class:: Collection(Sized, Iterable[T_co], Container[T_co])
36483662

Doc/whatsnew/3.12.rst

Lines changed: 18 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 typing, 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
@@ -1347,6 +1361,8 @@ Deprecated
13471361

13481362
.. include:: ../deprecations/pending-removal-in-3.16.rst
13491363

1364+
.. include:: ../deprecations/pending-removal-in-3.17.rst
1365+
13501366
.. include:: ../deprecations/pending-removal-in-future.rst
13511367

13521368
.. _whatsnew312-removed:

Doc/whatsnew/3.13.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2034,6 +2034,8 @@ New Deprecations
20342034

20352035
.. include:: ../deprecations/pending-removal-in-3.16.rst
20362036

2037+
.. include:: ../deprecations/pending-removal-in-3.17.rst
2038+
20372039
.. include:: ../deprecations/pending-removal-in-future.rst
20382040

20392041
CPython Bytecode Changes

Lib/_collections_abc.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1097,9 +1097,13 @@ def __instancecheck__(cls, instance):
10971097
return super().__instancecheck__(instance)
10981098

10991099
class ByteString(Sequence, metaclass=_DeprecateByteStringMeta):
1100-
"""This unifies bytes and bytearray.
1100+
"""Deprecated ABC serving as a common supertype of ``bytes`` and ``bytearray``.
11011101
1102-
XXX Should add all their methods.
1102+
This ABC is scheduled for removal in Python 3.17.
1103+
Use ``isinstance(obj, collections.abc.Buffer)`` to test if ``obj``
1104+
implements the buffer protocol at runtime. For use in type annotations,
1105+
either use ``Buffer`` or a union that explicitly specifies the types your
1106+
code supports (e.g., ``bytes | bytearray | memoryview``).
11031107
"""
11041108

11051109
__slots__ = ()

0 commit comments

Comments
 (0)