Skip to content

Commit 09b7695

Browse files
hauntsaninjaAlexWaygoodhugovk
authored
gh-91896: Deprecate collections.abc.ByteString (#102096)
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com> Co-authored-by: Hugo van Kemenade <hugovk@users.noreply.github.com>
1 parent 2ba931f commit 09b7695

File tree

6 files changed

+49
-8
lines changed

6 files changed

+49
-8
lines changed

Doc/library/collections.abc.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,12 @@ Collections Abstract Base Classes -- Detailed Descriptions
273273
The index() method added support for *stop* and *start*
274274
arguments.
275275

276+
.. deprecated-removed:: 3.12 3.14
277+
The :class:`ByteString` ABC has been deprecated.
278+
For use in typing, prefer a union, like ``bytes | bytearray``, or
279+
:class:`collections.abc.Buffer`.
280+
For use as an ABC, prefer :class:`Sequence` or :class:`collections.abc.Buffer`.
281+
276282
.. class:: Set
277283
MutableSet
278284

Doc/library/typing.rst

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2139,8 +2139,7 @@ Corresponding to collections in :mod:`collections.abc`
21392139
annotate arguments of any of the types mentioned above.
21402140

21412141
.. deprecated:: 3.9
2142-
:class:`collections.abc.ByteString` now supports subscripting (``[]``).
2143-
See :pep:`585` and :ref:`types-genericalias`.
2142+
Prefer :class:`collections.abc.Buffer`, or a union like ``bytes | bytearray | memoryview``.
21442143

21452144
.. class:: Collection(Sized, Iterable[T_co], Container[T_co])
21462145

Doc/whatsnew/3.12.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -792,6 +792,11 @@ Pending Removal in Python 3.14
792792

793793
(Contributed by Jason R. Coombs and Hugo van Kemenade in :gh:`93963`.)
794794

795+
* Deprecated :class:`collections.abc.ByteString`.
796+
Prefer :class:`Sequence` or :class:`collections.abc.Buffer`.
797+
For use in typing, prefer a union, like ``bytes | bytearray``, or :class:`collections.abc.Buffer`.
798+
(Contributed by Shantanu Jain in :gh:`91896`.)
799+
795800
* Creating immutable types (:data:`Py_TPFLAGS_IMMUTABLETYPE`) with mutable
796801
bases using the C API.
797802

Lib/_collections_abc.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1071,8 +1071,27 @@ def count(self, value):
10711071
Sequence.register(range)
10721072
Sequence.register(memoryview)
10731073

1074-
1075-
class ByteString(Sequence):
1074+
class _DeprecateByteStringMeta(ABCMeta):
1075+
def __new__(cls, name, bases, namespace, **kwargs):
1076+
if name != "ByteString":
1077+
import warnings
1078+
1079+
warnings._deprecated(
1080+
"collections.abc.ByteString",
1081+
remove=(3, 14),
1082+
)
1083+
return super().__new__(cls, name, bases, namespace, **kwargs)
1084+
1085+
def __instancecheck__(cls, instance):
1086+
import warnings
1087+
1088+
warnings._deprecated(
1089+
"collections.abc.ByteString",
1090+
remove=(3, 14),
1091+
)
1092+
return super().__instancecheck__(instance)
1093+
1094+
class ByteString(Sequence, metaclass=_DeprecateByteStringMeta):
10761095
"""This unifies bytes and bytearray.
10771096
10781097
XXX Should add all their methods.

Lib/test/test_collections.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1940,14 +1940,25 @@ def assert_index_same(seq1, seq2, index_args):
19401940

19411941
def test_ByteString(self):
19421942
for sample in [bytes, bytearray]:
1943-
self.assertIsInstance(sample(), ByteString)
1943+
with self.assertWarns(DeprecationWarning):
1944+
self.assertIsInstance(sample(), ByteString)
19441945
self.assertTrue(issubclass(sample, ByteString))
19451946
for sample in [str, list, tuple]:
1946-
self.assertNotIsInstance(sample(), ByteString)
1947+
with self.assertWarns(DeprecationWarning):
1948+
self.assertNotIsInstance(sample(), ByteString)
19471949
self.assertFalse(issubclass(sample, ByteString))
1948-
self.assertNotIsInstance(memoryview(b""), ByteString)
1950+
with self.assertWarns(DeprecationWarning):
1951+
self.assertNotIsInstance(memoryview(b""), ByteString)
19491952
self.assertFalse(issubclass(memoryview, ByteString))
1950-
self.validate_abstract_methods(ByteString, '__getitem__', '__len__')
1953+
with self.assertWarns(DeprecationWarning):
1954+
self.validate_abstract_methods(ByteString, '__getitem__', '__len__')
1955+
1956+
with self.assertWarns(DeprecationWarning):
1957+
class X(ByteString): pass
1958+
1959+
with self.assertWarns(DeprecationWarning):
1960+
# No metaclass conflict
1961+
class Z(ByteString, Awaitable): pass
19511962

19521963
def test_Buffer(self):
19531964
for sample in [bytes, bytearray, memoryview]:
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Deprecate :class:`collections.abc.ByteString`

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy