Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit09b7695

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 parent2ba931f commit09b7695

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-
classByteString(Sequence):
1074+
class_DeprecateByteStringMeta(ABCMeta):
1075+
def__new__(cls,name,bases,namespace,**kwargs):
1076+
ifname!="ByteString":
1077+
importwarnings
1078+
1079+
warnings._deprecated(
1080+
"collections.abc.ByteString",
1081+
remove=(3,14),
1082+
)
1083+
returnsuper().__new__(cls,name,bases,namespace,**kwargs)
1084+
1085+
def__instancecheck__(cls,instance):
1086+
importwarnings
1087+
1088+
warnings._deprecated(
1089+
"collections.abc.ByteString",
1090+
remove=(3,14),
1091+
)
1092+
returnsuper().__instancecheck__(instance)
1093+
1094+
classByteString(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
deftest_ByteString(self):
19421942
forsamplein [bytes,bytearray]:
1943-
self.assertIsInstance(sample(),ByteString)
1943+
withself.assertWarns(DeprecationWarning):
1944+
self.assertIsInstance(sample(),ByteString)
19441945
self.assertTrue(issubclass(sample,ByteString))
19451946
forsamplein [str,list,tuple]:
1946-
self.assertNotIsInstance(sample(),ByteString)
1947+
withself.assertWarns(DeprecationWarning):
1948+
self.assertNotIsInstance(sample(),ByteString)
19471949
self.assertFalse(issubclass(sample,ByteString))
1948-
self.assertNotIsInstance(memoryview(b""),ByteString)
1950+
withself.assertWarns(DeprecationWarning):
1951+
self.assertNotIsInstance(memoryview(b""),ByteString)
19491952
self.assertFalse(issubclass(memoryview,ByteString))
1950-
self.validate_abstract_methods(ByteString,'__getitem__','__len__')
1953+
withself.assertWarns(DeprecationWarning):
1954+
self.validate_abstract_methods(ByteString,'__getitem__','__len__')
1955+
1956+
withself.assertWarns(DeprecationWarning):
1957+
classX(ByteString):pass
1958+
1959+
withself.assertWarns(DeprecationWarning):
1960+
# No metaclass conflict
1961+
classZ(ByteString,Awaitable):pass
19511962

19521963
deftest_Buffer(self):
19531964
forsamplein [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)

[8]ページ先頭

©2009-2025 Movatter.jp