Skip to content

Commit d6a85e2

Browse files
authored
Attempt to clarify the ByteString situation (#9783)
See python/cpython#102092 This makes the behaviour of typing.ByteString better match its documentation.
1 parent 82ea677 commit d6a85e2

File tree

6 files changed

+11
-4
lines changed

6 files changed

+11
-4
lines changed

stdlib/builtins.pyi

+2-3
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ from typing import ( # noqa: Y022
3636
IO,
3737
Any,
3838
BinaryIO,
39-
ByteString,
4039
ClassVar,
4140
Generic,
4241
Mapping,
@@ -592,7 +591,7 @@ class str(Sequence[str]):
592591
def __rmul__(self, __value: SupportsIndex) -> str: ... # type: ignore[misc]
593592
def __getnewargs__(self) -> tuple[str]: ...
594593

595-
class bytes(ByteString):
594+
class bytes(Sequence[int]):
596595
@overload
597596
def __new__(cls, __o: Iterable[SupportsIndex] | SupportsIndex | SupportsBytes | ReadableBuffer) -> Self: ...
598597
@overload
@@ -697,7 +696,7 @@ class bytes(ByteString):
697696

698697
def __buffer__(self, __flags: int) -> memoryview: ...
699698

700-
class bytearray(MutableSequence[int], ByteString):
699+
class bytearray(MutableSequence[int]):
701700
@overload
702701
def __init__(self) -> None: ...
703702
@overload

stdlib/typing.pyi

+1-1
Original file line numberDiff line numberDiff line change
@@ -771,7 +771,7 @@ class TextIO(IO[str]):
771771
@abstractmethod
772772
def __enter__(self) -> TextIO: ...
773773

774-
class ByteString(Sequence[int], metaclass=ABCMeta): ...
774+
ByteString: typing_extensions.TypeAlias = bytes | bytearray | memoryview
775775

776776
# Functions
777777

tests/stubtest_allowlists/py37.txt

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ collections.AsyncGenerator.ag_await
2323
collections.AsyncGenerator.ag_code
2424
collections.AsyncGenerator.ag_frame
2525
collections.AsyncGenerator.ag_running
26+
collections.ByteString # see comments in py3_common.txt
2627
collections.Callable
2728
collections.Mapping.__reversed__ # Set to None at runtime for a better error message
2829
contextvars.ContextVar.get

tests/stubtest_allowlists/py38.txt

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ collections.AsyncGenerator.ag_await
2929
collections.AsyncGenerator.ag_code
3030
collections.AsyncGenerator.ag_frame
3131
collections.AsyncGenerator.ag_running
32+
collections.ByteString # see comments in py3_common.txt
3233
collections.Callable
3334
collections.ItemsView.__reversed__
3435
collections.KeysView.__reversed__

tests/stubtest_allowlists/py39.txt

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ collections.AsyncGenerator.ag_await
2727
collections.AsyncGenerator.ag_code
2828
collections.AsyncGenerator.ag_frame
2929
collections.AsyncGenerator.ag_running
30+
collections.ByteString # see comments in py3_common.txt
3031
collections.Callable
3132
collections.Mapping.__reversed__ # Set to None at runtime for a better error message
3233
collections.ItemsView.__reversed__

tests/stubtest_allowlists/py3_common.txt

+5
Original file line numberDiff line numberDiff line change
@@ -667,6 +667,11 @@ xml.etree.ElementTree.Element.__iter__
667667
xml.etree.cElementTree.Element.__iter__
668668
typing.IO.__iter__ # See https://github.com/python/typeshed/commit/97bc450acd60c1bcdafef3ce8fbe3b95a9c0cac3
669669

670+
# Pretend typing.ByteString is a Union, to better match its documented semantics.
671+
# As a side effect, this changes the definition of collections.abc.ByteString, which is okay,
672+
# because it's not an ABC that makes any sense and was deprecated in 3.12
673+
_collections_abc.ByteString
674+
670675
# ==========
671676
# Missing from deprecated modules
672677
# Any of these can be added if someone needs them

0 commit comments

Comments
 (0)