FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-91896: Deprecate collections.abc.ByteString by hauntsaninja · Pull Request #102096 · python/cpython · GitHub

/ cpython Public
6 changes: 6 additions & 0 deletions Doc/library/collections.abc.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,12 @@ Collections Abstract Base Classes -- Detailed Descriptions
The index() method added support for *stop* and *start*
arguments.

.. deprecated-removed:: 3.12 3.14
The :class:`ByteString` ABC has been deprecated.
Comment thread
hauntsaninja marked this conversation as resolved.
For use in typing, prefer a union, like ``bytes | bytearray``, or
:class:`collections.abc.Buffer`.
Comment thread
hauntsaninja marked this conversation as resolved.
For use as an ABC, prefer :class:`Sequence` or :class:`collections.abc.Buffer`.

.. class:: Set
MutableSet

Expand Down
3 changes: 1 addition & 2 deletions Doc/library/typing.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -2076,8 +2076,7 @@ Corresponding to collections in :mod:`collections.abc`
annotate arguments of any of the types mentioned above.

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

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

Expand Down
5 changes: 5 additions & 0 deletions Doc/whatsnew/3.12.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,11 @@ Pending Removal in Python 3.14

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

* Deprecated :class:`collections.abc.ByteString`.
Prefer :class:`Sequence` or :class:`collections.abc.Buffer`.
Comment thread
hugovk marked this conversation as resolved.
For use in typing, prefer a union, like ``bytes | bytearray``, or :class:`collections.abc.Buffer`.
(Contributed by Shantanu Jain in :gh:`91896`.)

* Creating :c:data:`immutable types <Py_TPFLAGS_IMMUTABLETYPE>` with mutable
bases using the C API.

Expand Down
23 changes: 21 additions & 2 deletions Lib/_collections_abc.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -1064,8 +1064,27 @@ def count(self, value):
Sequence.register(range)
Sequence.register(memoryview)


class ByteString(Sequence):
class _DeprecateByteStringMeta(ABCMeta):
def __new__(cls, name, bases, namespace, **kwargs):
if name != "ByteString":
import warnings

warnings._deprecated(
Comment thread
hauntsaninja marked this conversation as resolved.
"collections.abc.ByteString",
remove=(3, 14),
)
return super().__new__(cls, name, bases, namespace, **kwargs)

def __instancecheck__(cls, instance):
import warnings

warnings._deprecated(
"collections.abc.ByteString",
remove=(3, 14),
)
return super().__instancecheck__(instance)

class ByteString(Sequence, metaclass=_DeprecateByteStringMeta):
Comment thread
hauntsaninja marked this conversation as resolved.
"""This unifies bytes and bytearray.

XXX Should add all their methods.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

You can also remove this comment.

Expand Down
15 changes: 11 additions & 4 deletions Lib/test/test_collections.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -1940,14 +1940,21 @@ def assert_index_same(seq1, seq2, index_args):

def test_ByteString(self):
for sample in [bytes, bytearray]:
self.assertIsInstance(sample(), ByteString)
with self.assertWarns(DeprecationWarning):
self.assertIsInstance(sample(), ByteString)
self.assertTrue(issubclass(sample, ByteString))
for sample in [str, list, tuple]:
self.assertNotIsInstance(sample(), ByteString)
with self.assertWarns(DeprecationWarning):
self.assertNotIsInstance(sample(), ByteString)
self.assertFalse(issubclass(sample, ByteString))
self.assertNotIsInstance(memoryview(b""), ByteString)
with self.assertWarns(DeprecationWarning):
self.assertNotIsInstance(memoryview(b""), ByteString)
self.assertFalse(issubclass(memoryview, ByteString))
self.validate_abstract_methods(ByteString, '__getitem__', '__len__')
with self.assertWarns(DeprecationWarning):
self.validate_abstract_methods(ByteString, '__getitem__', '__len__')

with self.assertWarns(DeprecationWarning):
class X(ByteString): pass

def test_MutableSequence(self):
for sample in [tuple, str, bytes]:
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Deprecate :class:`collections.abc.ByteString`
Comment thread
hugovk marked this conversation as resolved.

Back | FazBrowse Home | New Git URL