From 804dfa10120f1cdf68fd897a9d89fb56b1bd8802 Mon Sep 17 00:00:00 2001 From: hauntsaninja Date: Mon, 20 Feb 2023 17:15:29 -0800 Subject: [PATCH 1/9] gh-91896: Deprecate collections.abc.ByteString Getting a DeprecationWarning on issubclass proved to be difficult, because it could affect unrelated looking things like `isinstance(bytes, Sequence)` --- Lib/_collections_abc.py | 24 +++++++++++++++++++++--- Lib/test/test_collections.py | 15 +++++++++++---- 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/Lib/_collections_abc.py b/Lib/_collections_abc.py index c62233b81a5c95..c8f19e48210749 100644 --- a/Lib/_collections_abc.py +++ b/Lib/_collections_abc.py @@ -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( + "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): """This unifies bytes and bytearray. XXX Should add all their methods. @@ -1076,7 +1095,6 @@ class ByteString(Sequence): ByteString.register(bytes) ByteString.register(bytearray) - class MutableSequence(Sequence): """All the operations on a read-write sequence. diff --git a/Lib/test/test_collections.py b/Lib/test/test_collections.py index bfe18c7fc50330..1aaa052ae12532 100644 --- a/Lib/test/test_collections.py +++ b/Lib/test/test_collections.py @@ -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]: From 4c7eace70c777a171da385683c32c8ca4df61668 Mon Sep 17 00:00:00 2001 From: hauntsaninja Date: Mon, 20 Feb 2023 17:18:20 -0800 Subject: [PATCH 2/9] add back a whitespace --- Lib/_collections_abc.py | 1 + 1 file changed, 1 insertion(+) diff --git a/Lib/_collections_abc.py b/Lib/_collections_abc.py index c8f19e48210749..ce084f90c6771d 100644 --- a/Lib/_collections_abc.py +++ b/Lib/_collections_abc.py @@ -1095,6 +1095,7 @@ class ByteString(Sequence, metaclass=_DeprecateByteStringMeta): ByteString.register(bytes) ByteString.register(bytearray) + class MutableSequence(Sequence): """All the operations on a read-write sequence. From d534015410146802771ec40955961bbcc1de527c Mon Sep 17 00:00:00 2001 From: hauntsaninja Date: Mon, 20 Feb 2023 17:22:19 -0800 Subject: [PATCH 3/9] docs --- Doc/library/collections.abc.rst | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Doc/library/collections.abc.rst b/Doc/library/collections.abc.rst index 132b0ce7192ac1..a109c018fea154 100644 --- a/Doc/library/collections.abc.rst +++ b/Doc/library/collections.abc.rst @@ -272,6 +272,11 @@ Collections Abstract Base Classes -- Detailed Descriptions The index() method added support for *stop* and *start* arguments. + .. deprecated:: 3.12 + The :class:`ByteString` ABC has been deprecated. + For use in typing, prefer a union, like ``bytes | bytearray``. + For use as an ABC, prefer :class:`Sequence`. + .. class:: Set MutableSet From 476a25970adc339bac3be4a5ee2642402d54c63e Mon Sep 17 00:00:00 2001 From: hauntsaninja Date: Mon, 20 Feb 2023 17:23:03 -0800 Subject: [PATCH 4/9] typing docs --- Doc/library/typing.rst | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Doc/library/typing.rst b/Doc/library/typing.rst index 356f919a1897b2..5eccf907b767e1 100644 --- a/Doc/library/typing.rst +++ b/Doc/library/typing.rst @@ -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 a union, like ``bytes | bytearray | memoryview``. .. class:: Collection(Sized, Iterable[T_co], Container[T_co]) From 46404a0165b02439200dc787895874e75828d2c9 Mon Sep 17 00:00:00 2001 From: hauntsaninja Date: Tue, 7 Mar 2023 18:44:55 -0800 Subject: [PATCH 5/9] pep 688 docs --- Doc/library/collections.abc.rst | 5 +++-- Doc/library/typing.rst | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/Doc/library/collections.abc.rst b/Doc/library/collections.abc.rst index a109c018fea154..db932cd5d09075 100644 --- a/Doc/library/collections.abc.rst +++ b/Doc/library/collections.abc.rst @@ -274,8 +274,9 @@ Collections Abstract Base Classes -- Detailed Descriptions .. deprecated:: 3.12 The :class:`ByteString` ABC has been deprecated. - For use in typing, prefer a union, like ``bytes | bytearray``. - For use as an ABC, prefer :class:`Sequence`. + For use in typing, prefer a union, like ``bytes | bytearray``, or + :class:`collections.abc.Buffer`. + For use as an ABC, prefer :class:`Sequence` or :class:`collections.abc.Buffer`. .. class:: Set MutableSet diff --git a/Doc/library/typing.rst b/Doc/library/typing.rst index 5eccf907b767e1..934d2b9458e07c 100644 --- a/Doc/library/typing.rst +++ b/Doc/library/typing.rst @@ -2076,7 +2076,7 @@ Corresponding to collections in :mod:`collections.abc` annotate arguments of any of the types mentioned above. .. deprecated:: 3.9 - Prefer a union, like ``bytes | bytearray | memoryview``. + Prefer :class:`collections.abc.Buffer`, or a union like ``bytes | bytearray | memoryview``. .. class:: Collection(Sized, Iterable[T_co], Container[T_co]) From b1b4fd2f27b9c5434d44649702406e5c73662633 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Wed, 8 Mar 2023 02:45:49 +0000 Subject: [PATCH 6/9] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Library/2023-03-08-02-45-46.gh-issue-91896.kgON_a.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2023-03-08-02-45-46.gh-issue-91896.kgON_a.rst diff --git a/Misc/NEWS.d/next/Library/2023-03-08-02-45-46.gh-issue-91896.kgON_a.rst b/Misc/NEWS.d/next/Library/2023-03-08-02-45-46.gh-issue-91896.kgON_a.rst new file mode 100644 index 00000000000000..b5282d3d612916 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2023-03-08-02-45-46.gh-issue-91896.kgON_a.rst @@ -0,0 +1 @@ +Deprecate :class:`collections.abc.ByteString` From dc22a743d89f6f3211ccd3b37336345a2d3986b1 Mon Sep 17 00:00:00 2001 From: hauntsaninja Date: Thu, 9 Mar 2023 12:54:09 -0800 Subject: [PATCH 7/9] what's new, removed --- Doc/library/collections.abc.rst | 2 +- Doc/whatsnew/3.12.rst | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/Doc/library/collections.abc.rst b/Doc/library/collections.abc.rst index db932cd5d09075..63e3365b5133b8 100644 --- a/Doc/library/collections.abc.rst +++ b/Doc/library/collections.abc.rst @@ -272,7 +272,7 @@ Collections Abstract Base Classes -- Detailed Descriptions The index() method added support for *stop* and *start* arguments. - .. deprecated:: 3.12 + .. deprecated-removed:: 3.12 3.14 The :class:`ByteString` ABC has been deprecated. For use in typing, prefer a union, like ``bytes | bytearray``, or :class:`collections.abc.Buffer`. diff --git a/Doc/whatsnew/3.12.rst b/Doc/whatsnew/3.12.rst index 45a5e5062d55b6..db6e7ca1c2de56 100644 --- a/Doc/whatsnew/3.12.rst +++ b/Doc/whatsnew/3.12.rst @@ -409,6 +409,10 @@ Deprecated * :class:`typing.Hashable` and :class:`typing.Sized` aliases for :class:`collections.abc.Hashable` and :class:`collections.abc.Sized`. (:gh:`94309`.) +* :class:`collections.abc.ByteString`, prefer :class:`Sequence` or :class:`collections.abc.Buffer`. + For use in typing, prefer a union, like ``bytes | bytearray``, or :class:`collections.abc.Buffer`. + (Contributed by Shantanu Jain in :gh:`102096`.) + * The :mod:`sqlite3` :ref:`default adapters and converters ` are now deprecated. Instead, use the :ref:`sqlite3-adapter-converter-recipes` From eeaff445f51f7aaa285b3bcd4e02c69399da95ab Mon Sep 17 00:00:00 2001 From: hauntsaninja Date: Thu, 9 Mar 2023 13:14:47 -0800 Subject: [PATCH 8/9] move to pending removal --- Doc/whatsnew/3.12.rst | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/Doc/whatsnew/3.12.rst b/Doc/whatsnew/3.12.rst index db6e7ca1c2de56..f191581ef25b1c 100644 --- a/Doc/whatsnew/3.12.rst +++ b/Doc/whatsnew/3.12.rst @@ -409,10 +409,6 @@ Deprecated * :class:`typing.Hashable` and :class:`typing.Sized` aliases for :class:`collections.abc.Hashable` and :class:`collections.abc.Sized`. (:gh:`94309`.) -* :class:`collections.abc.ByteString`, prefer :class:`Sequence` or :class:`collections.abc.Buffer`. - For use in typing, prefer a union, like ``bytes | bytearray``, or :class:`collections.abc.Buffer`. - (Contributed by Shantanu Jain in :gh:`102096`.) - * The :mod:`sqlite3` :ref:`default adapters and converters ` are now deprecated. Instead, use the :ref:`sqlite3-adapter-converter-recipes` @@ -499,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`. + 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 ` with mutable bases using the C API. From 8777d294c16fc256544b68122dc96b2624ae8471 Mon Sep 17 00:00:00 2001 From: hauntsaninja Date: Sat, 11 Mar 2023 12:12:37 -0800 Subject: [PATCH 9/9] add test --- Lib/test/test_collections.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Lib/test/test_collections.py b/Lib/test/test_collections.py index 1aaa052ae12532..09880a580c62bb 100644 --- a/Lib/test/test_collections.py +++ b/Lib/test/test_collections.py @@ -1956,6 +1956,10 @@ def test_ByteString(self): with self.assertWarns(DeprecationWarning): class X(ByteString): pass + with self.assertWarns(DeprecationWarning): + # No metaclass conflict + class Z(ByteString, Awaitable): pass + def test_MutableSequence(self): for sample in [tuple, str, bytes]: self.assertNotIsInstance(sample(), MutableSequence)