Skip to content

Commit 530ddd3

Browse files
authored
Revert "gh-118803: Remove ByteString from typing and collections.abc (#118804)" (#138990)
1 parent 6d6aba2 commit 530ddd3

File tree

13 files changed

+127
-31
lines changed

13 files changed

+127
-31
lines changed

Doc/deprecations/pending-removal-in-3.14.rst

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,6 @@ Pending removal in Python 3.14
3838
is no current event loop set and it decides to create one.
3939
(Contributed by Serhiy Storchaka and Guido van Rossum in :gh:`100160`.)
4040

41-
* :mod:`collections.abc`: Deprecated :class:`!collections.abc.ByteString`.
42-
Prefer :class:`!Sequence` or :class:`~collections.abc.Buffer`.
43-
For use in typing, prefer a union, like ``bytes | bytearray``,
44-
or :class:`collections.abc.Buffer`.
45-
(Contributed by Shantanu Jain in :gh:`91896`.)
46-
4741
* :mod:`email`: Deprecated the *isdst* parameter in :func:`email.utils.localtime`.
4842
(Contributed by Alan Williams in :gh:`72346`.)
4943

@@ -96,9 +90,6 @@ Pending removal in Python 3.14
9690
if :ref:`named placeholders <sqlite3-placeholders>` are used and
9791
*parameters* is a sequence instead of a :class:`dict`.
9892

99-
* :mod:`typing`: :class:`!typing.ByteString`, deprecated since Python 3.9,
100-
now causes a :exc:`DeprecationWarning` to be emitted when it is used.
101-
10293
* :mod:`urllib`:
10394
:class:`!urllib.parse.Quoter` is deprecated: it was not intended to be a
10495
public API.

Doc/deprecations/pending-removal-in-3.17.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,15 @@ Pending removal in Python 3.17
88
but it has been retained for backward compatibility, with removal scheduled for Python
99
3.17. Users should use documented introspection helpers like :func:`typing.get_origin`
1010
and :func:`typing.get_args` instead of relying on private implementation details.
11+
- :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`.)
16+
17+
* :mod:`collections.abc`:
18+
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`.)

Doc/library/collections.abc.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,9 @@ ABC Inherits from Abstract Methods Mi
140140
``__len__``,
141141
``insert``
142142

143+
:class:`ByteString` :class:`Sequence` ``__getitem__``, Inherited :class:`Sequence` methods
144+
``__len__``
145+
143146
:class:`Set` :class:`Collection` ``__contains__``, ``__le__``, ``__lt__``, ``__eq__``, ``__ne__``,
144147
``__iter__``, ``__gt__``, ``__ge__``, ``__and__``, ``__or__``,
145148
``__len__`` ``__sub__``, ``__rsub__``, ``__xor__``, ``__rxor__``
@@ -260,6 +263,7 @@ Collections Abstract Base Classes -- Detailed Descriptions
260263

261264
.. class:: Sequence
262265
MutableSequence
266+
ByteString
263267

264268
ABCs for read-only and mutable :term:`sequences <sequence>`.
265269

@@ -285,6 +289,12 @@ Collections Abstract Base Classes -- Detailed Descriptions
285289
The :meth:`~sequence.index` method gained support for
286290
the *stop* and *start* arguments.
287291

292+
.. deprecated-removed:: 3.12 3.17
293+
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`.
297+
288298
.. class:: Set
289299
MutableSet
290300

Doc/library/stdtypes.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5544,6 +5544,7 @@ list is non-exhaustive.
55445544
* :class:`collections.abc.MutableMapping`
55455545
* :class:`collections.abc.Sequence`
55465546
* :class:`collections.abc.MutableSequence`
5547+
* :class:`collections.abc.ByteString`
55475548
* :class:`collections.abc.MappingView`
55485549
* :class:`collections.abc.KeysView`
55495550
* :class:`collections.abc.ItemsView`

Doc/library/typing.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3788,6 +3788,14 @@ Aliases to container ABCs in :mod:`collections.abc`
37883788
:class:`collections.abc.Set` now supports subscripting (``[]``).
37893789
See :pep:`585` and :ref:`types-genericalias`.
37903790

3791+
.. class:: ByteString(Sequence[int])
3792+
3793+
This type represents the types :class:`bytes`, :class:`bytearray`,
3794+
and :class:`memoryview` of byte sequences.
3795+
3796+
.. deprecated-removed:: 3.9 3.17
3797+
Prefer :class:`collections.abc.Buffer`, or a union like ``bytes | bytearray | memoryview``.
3798+
37913799
.. class:: Collection(Sized, Iterable[T_co], Container[T_co])
37923800

37933801
Deprecated alias to :class:`collections.abc.Collection`.
@@ -4081,6 +4089,10 @@ convenience. This is subject to change, and not all deprecations are listed.
40814089
- 3.9
40824090
- Undecided (see :ref:`deprecated-aliases` for more information)
40834091
- :pep:`585`
4092+
* - :class:`typing.ByteString`
4093+
- 3.9
4094+
- 3.17
4095+
- :gh:`91896`
40844096
* - :data:`typing.Text`
40854097
- 3.11
40864098
- Undecided

Doc/whatsnew/3.12.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1191,9 +1191,9 @@ Deprecated
11911191
replaced by :data:`calendar.JANUARY` and :data:`calendar.FEBRUARY`.
11921192
(Contributed by Prince Roshan in :gh:`103636`.)
11931193

1194-
* :mod:`collections.abc`: Deprecated :class:`!collections.abc.ByteString`.
1194+
* :mod:`collections.abc`: Deprecated :class:`collections.abc.ByteString`.
11951195
Prefer :class:`Sequence` or :class:`collections.abc.Buffer`.
1196-
For use in typing, prefer a union, like ``bytes | bytearray``, or :class:`collections.abc.Buffer`.
1196+
For use in type annotations, prefer a union, like ``bytes | bytearray``, or :class:`collections.abc.Buffer`.
11971197
(Contributed by Shantanu Jain in :gh:`91896`.)
11981198

11991199
* :mod:`datetime`: :class:`datetime.datetime`'s :meth:`~datetime.datetime.utcnow` and
@@ -1301,7 +1301,7 @@ Deprecated
13011301
:class:`collections.abc.Hashable` and :class:`collections.abc.Sized` respectively, are
13021302
deprecated. (:gh:`94309`.)
13031303

1304-
* :class:`!typing.ByteString`, deprecated since Python 3.9, now causes a
1304+
* :class:`typing.ByteString`, deprecated since Python 3.9, now causes a
13051305
:exc:`DeprecationWarning` to be emitted when it is used.
13061306
(Contributed by Alex Waygood in :gh:`91896`.)
13071307

Doc/whatsnew/3.14.rst

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2560,15 +2560,6 @@ asyncio
25602560
blocking_code()
25612561
runner.run(operation_two())
25622562
2563-
2564-
collections.abc
2565-
---------------
2566-
2567-
* Remove :class:`!ByteString`, which has been deprecated since Python 3.12.
2568-
(Contributed by Nikita Sobolev in :gh:`118803`.)
2569-
2570-
2571-
25722563
email
25732564
-----
25742565

@@ -2647,13 +2638,6 @@ sqlite3
26472638
(Contributed by Erlend E. Aasland in :gh:`118928` and :gh:`101693`.)
26482639

26492640

2650-
typing
2651-
------
2652-
2653-
* Remove :class:`!ByteString`, which has been deprecated since Python 3.12.
2654-
(Contributed by Nikita Sobolev in :gh:`118803`.)
2655-
2656-
26572641
urllib
26582642
------
26592643

Doc/whatsnew/3.5.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -936,7 +936,7 @@ methods to match the corresponding methods of :class:`str`.
936936
collections.abc
937937
---------------
938938

939-
The :meth:`Sequence.index() <collections.abc.MutableSequence.index>` method now
939+
The :meth:`!Sequence.index` method now
940940
accepts *start* and *stop* arguments to match the corresponding methods
941941
of :class:`tuple`, :class:`list`, etc.
942942
(Contributed by Devin Jeanpierre in :issue:`23086`.)

Lib/_collections_abc.py

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def _f(): pass
4949
"Mapping", "MutableMapping",
5050
"MappingView", "KeysView", "ItemsView", "ValuesView",
5151
"Sequence", "MutableSequence",
52-
"Buffer",
52+
"ByteString", "Buffer",
5353
]
5454

5555
# This module has been renamed from collections.abc to _collections_abc to
@@ -1061,6 +1061,37 @@ def count(self, value):
10611061
Sequence.register(range)
10621062
Sequence.register(memoryview)
10631063

1064+
class _DeprecateByteStringMeta(ABCMeta):
1065+
def __new__(cls, name, bases, namespace, **kwargs):
1066+
if name != "ByteString":
1067+
import warnings
1068+
1069+
warnings._deprecated(
1070+
"collections.abc.ByteString",
1071+
remove=(3, 17),
1072+
)
1073+
return super().__new__(cls, name, bases, namespace, **kwargs)
1074+
1075+
def __instancecheck__(cls, instance):
1076+
import warnings
1077+
1078+
warnings._deprecated(
1079+
"collections.abc.ByteString",
1080+
remove=(3, 17),
1081+
)
1082+
return super().__instancecheck__(instance)
1083+
1084+
class ByteString(Sequence, metaclass=_DeprecateByteStringMeta):
1085+
"""This unifies bytes and bytearray.
1086+
1087+
XXX Should add all their methods.
1088+
"""
1089+
1090+
__slots__ = ()
1091+
1092+
ByteString.register(bytes)
1093+
ByteString.register(bytearray)
1094+
10641095

10651096
class MutableSequence(Sequence):
10661097
"""All the operations on a read-write sequence.

Lib/test/test_collections.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
from collections.abc import Set, MutableSet
2727
from collections.abc import Mapping, MutableMapping, KeysView, ItemsView, ValuesView
2828
from collections.abc import Sequence, MutableSequence
29-
from collections.abc import Buffer
29+
from collections.abc import ByteString, Buffer
3030

3131

3232
class TestUserObjects(unittest.TestCase):
@@ -1934,6 +1934,28 @@ def assert_index_same(seq1, seq2, index_args):
19341934
assert_index_same(
19351935
nativeseq, seqseq, (letter, start, stop))
19361936

1937+
def test_ByteString(self):
1938+
for sample in [bytes, bytearray]:
1939+
with self.assertWarns(DeprecationWarning):
1940+
self.assertIsInstance(sample(), ByteString)
1941+
self.assertTrue(issubclass(sample, ByteString))
1942+
for sample in [str, list, tuple]:
1943+
with self.assertWarns(DeprecationWarning):
1944+
self.assertNotIsInstance(sample(), ByteString)
1945+
self.assertFalse(issubclass(sample, ByteString))
1946+
with self.assertWarns(DeprecationWarning):
1947+
self.assertNotIsInstance(memoryview(b""), ByteString)
1948+
self.assertFalse(issubclass(memoryview, ByteString))
1949+
with self.assertWarns(DeprecationWarning):
1950+
self.validate_abstract_methods(ByteString, '__getitem__', '__len__')
1951+
1952+
with self.assertWarns(DeprecationWarning):
1953+
class X(ByteString): pass
1954+
1955+
with self.assertWarns(DeprecationWarning):
1956+
# No metaclass conflict
1957+
class Z(ByteString, Awaitable): pass
1958+
19371959
def test_Buffer(self):
19381960
for sample in [bytes, bytearray, memoryview]:
19391961
self.assertIsInstance(sample(b"x"), Buffer)

0 commit comments

Comments
 (0)