8000 gh-91896: Deprecate collections.abc.ByteString by hauntsaninja · Pull Request #102096 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-91896: Deprecate collections.abc.ByteString #102096

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
May 4, 2023
Next Next commit
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)`
  • Loading branch information
hauntsaninja committed Feb 21, 2023
commit 804dfa10120f1cdf68fd897a9d89fb56b1bd8802
24 changes: 21 additions & 3 deletions Lib/_collections_abc.py
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(
"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.
Copy link
Member

Choose a reason for hiding this comment

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

You can also remove this comment.

Expand All @@ -1076,7 +1095,6 @@ class ByteString(Sequence):
ByteString.register(bytes)
ByteString.register(bytearray)


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

Expand Down
15 changes: 11 additions & 4 deletions Lib/test/test_collections.py
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
0