From 4ad291f42fd9624cd187f596748cc9df47f2e35f Mon Sep 17 00:00:00 2001 From: hauntsaninja Date: Mon, 20 Feb 2023 14:31:43 -0800 Subject: [PATCH 1/9] Attempt to clarify the ByteString situation See https://github.com/python/cpython/issues/102092 This makes the behaviour of typing.ByteString better match its documentation. I also remove collections.abc.ByteString. There is no legitimate use case for it. Let me know if you think there's a case where it would help users to have this around. --- stdlib/_collections_abc.pyi | 1 - stdlib/typing.pyi | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/stdlib/_collections_abc.pyi b/stdlib/_collections_abc.pyi index 352da6cfb331..1be18d4d7587 100644 --- a/stdlib/_collections_abc.pyi +++ b/stdlib/_collections_abc.pyi @@ -6,7 +6,6 @@ from typing import ( # noqa: Y022,Y038 AsyncIterable as AsyncIterable, AsyncIterator as AsyncIterator, Awaitable as Awaitable, - ByteString as ByteString, Callable as Callable, Collection as Collection, Container as Container, diff --git a/stdlib/typing.pyi b/stdlib/typing.pyi index d06b081d3ddc..e3c21c27ddfd 100644 --- a/stdlib/typing.pyi +++ b/stdlib/typing.pyi @@ -715,7 +715,7 @@ class TextIO(IO[str]): @abstractmethod def __enter__(self) -> TextIO: ... -class ByteString(Sequence[int], metaclass=ABCMeta): ... +ByteString = bytes | bytearray | memoryview # Functions From 24fd3c072263d72fc14bdaaccf36a73dc3683382 Mon Sep 17 00:00:00 2001 From: hauntsaninja Date: Mon, 20 Feb 2023 14:47:40 -0800 Subject: [PATCH 2/9] . --- stdlib/builtins.pyi | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/stdlib/builtins.pyi b/stdlib/builtins.pyi index a8bedc8374bd..e1ba13f02597 100644 --- a/stdlib/builtins.pyi +++ b/stdlib/builtins.pyi @@ -36,7 +36,6 @@ from typing import ( # noqa: Y022 IO, Any, BinaryIO, - ByteString, ClassVar, Generic, Mapping, @@ -587,7 +586,7 @@ class str(Sequence[str]): def __rmul__(self, __n: SupportsIndex) -> str: ... # type: ignore[misc] def __getnewargs__(self) -> tuple[str]: ... -class bytes(ByteString): +class bytes: @overload def __new__(cls, __o: Iterable[SupportsIndex] | SupportsIndex | SupportsBytes | ReadableBuffer) -> Self: ... @overload @@ -690,7 +689,7 @@ class bytes(ByteString): if sys.version_info >= (3, 11): def __bytes__(self) -> bytes: ... -class bytearray(MutableSequence[int], ByteString): +class bytearray(MutableSequence[int]): @overload def __init__(self) -> None: ... @overload From 4dcbddc645ab46b90bced50e7a62335f927fa5d2 Mon Sep 17 00:00:00 2001 From: hauntsaninja Date: Mon, 20 Feb 2023 14:49:12 -0800 Subject: [PATCH 3/9] add back collections abc ..... for now ......... --- stdlib/_collections_abc.pyi | 1 + 1 file changed, 1 insertion(+) diff --git a/stdlib/_collections_abc.pyi b/stdlib/_collections_abc.pyi index 1be18d4d7587..352da6cfb331 100644 --- a/stdlib/_collections_abc.pyi +++ b/stdlib/_collections_abc.pyi @@ -6,6 +6,7 @@ from typing import ( # noqa: Y022,Y038 AsyncIterable as AsyncIterable, AsyncIterator as AsyncIterator, Awaitable as Awaitable, + ByteString as ByteString, Callable as Callable, Collection as Collection, Container as Container, From 2ec5f71e6d05d0ddde1161b687c22557b1ded47b Mon Sep 17 00:00:00 2001 From: hauntsaninja Date: Mon, 20 Feb 2023 15:05:07 -0800 Subject: [PATCH 4/9] . --- stdlib/builtins.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stdlib/builtins.pyi b/stdlib/builtins.pyi index e1ba13f02597..5d697eccca47 100644 --- a/stdlib/builtins.pyi +++ b/stdlib/builtins.pyi @@ -586,7 +586,7 @@ class str(Sequence[str]): def __rmul__(self, __n: SupportsIndex) -> str: ... # type: ignore[misc] def __getnewargs__(self) -> tuple[str]: ... -class bytes: +class bytes(Sequence[int]): @overload def __new__(cls, __o: Iterable[SupportsIndex] | SupportsIndex | SupportsBytes | ReadableBuffer) -> Self: ... @overload From 3a16bd6cc1bcd26ddca5d002a14f01152096125c Mon Sep 17 00:00:00 2001 From: hauntsaninja Date: Mon, 20 Feb 2023 15:47:58 -0800 Subject: [PATCH 5/9] allowlist --- tests/stubtest_allowlists/py3_common.txt | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/stubtest_allowlists/py3_common.txt b/tests/stubtest_allowlists/py3_common.txt index a035c9ad7bf5..481ebf6af72b 100644 --- a/tests/stubtest_allowlists/py3_common.txt +++ b/tests/stubtest_allowlists/py3_common.txt @@ -608,6 +608,11 @@ xml.etree.ElementTree.Element.__iter__ xml.etree.cElementTree.Element.__iter__ typing.IO.__iter__ # See https://github.com/python/typeshed/commit/97bc450acd60c1bcdafef3ce8fbe3b95a9c0cac3 +# Pretend typing.ByteString is a Union, to better match its documented semantics. +# As a side effect, this changes the definition of collections.abc.ByteString, which is okay, +# because it's not an ABC that makes any sense. +_collections_abc.ByteString + # ========== # Missing from deprecated modules # Any of these can be added if someone needs them From ab3306823f642126f511820f0da8669ced43b16e Mon Sep 17 00:00:00 2001 From: hauntsaninja Date: Mon, 20 Feb 2023 15:48:31 -0800 Subject: [PATCH 6/9] allowlist --- tests/stubtest_allowlists/py37.txt | 1 + tests/stubtest_allowlists/py38.txt | 1 + tests/stubtest_allowlists/py39.txt | 1 + 3 files changed, 3 insertions(+) diff --git a/tests/stubtest_allowlists/py37.txt b/tests/stubtest_allowlists/py37.txt index 2be7e1217268..954304002f51 100644 --- a/tests/stubtest_allowlists/py37.txt +++ b/tests/stubtest_allowlists/py37.txt @@ -23,6 +23,7 @@ collections.AsyncGenerator.ag_await collections.AsyncGenerator.ag_code collections.AsyncGenerator.ag_frame collections.AsyncGenerator.ag_running +collections.ByteString # see comments in py3_common.txt collections.Callable collections.Mapping.__reversed__ # Set to None at runtime for a better error message contextvars.ContextVar.get diff --git a/tests/stubtest_allowlists/py38.txt b/tests/stubtest_allowlists/py38.txt index 7066216fb76d..87b6b1f0b5d8 100644 --- a/tests/stubtest_allowlists/py38.txt +++ b/tests/stubtest_allowlists/py38.txt @@ -29,6 +29,7 @@ collections.AsyncGenerator.ag_await collections.AsyncGenerator.ag_code collections.AsyncGenerator.ag_frame collections.AsyncGenerator.ag_running +collections.ByteString collections.Callable collections.ItemsView.__reversed__ collections.KeysView.__reversed__ diff --git a/tests/stubtest_allowlists/py39.txt b/tests/stubtest_allowlists/py39.txt index a7937b3b931e..5abe7a7b8542 100644 --- a/tests/stubtest_allowlists/py39.txt +++ b/tests/stubtest_allowlists/py39.txt @@ -27,6 +27,7 @@ collections.AsyncGenerator.ag_await collections.AsyncGenerator.ag_code collections.AsyncGenerator.ag_frame collections.AsyncGenerator.ag_running +collections.ByteString collections.Callable collections.Mapping.__reversed__ # Set to None at runtime for a better error message collections.ItemsView.__reversed__ From 12b8bb5249b0a40a3f759217b1081896e0a9cd99 Mon Sep 17 00:00:00 2001 From: hauntsaninja Date: Mon, 20 Feb 2023 15:50:13 -0800 Subject: [PATCH 7/9] comment --- tests/stubtest_allowlists/py38.txt | 2 +- tests/stubtest_allowlists/py39.txt | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/stubtest_allowlists/py38.txt b/tests/stubtest_allowlists/py38.txt index 87b6b1f0b5d8..dc2b429a2dc0 100644 --- a/tests/stubtest_allowlists/py38.txt +++ b/tests/stubtest_allowlists/py38.txt @@ -29,7 +29,7 @@ collections.AsyncGenerator.ag_await collections.AsyncGenerator.ag_code collections.AsyncGenerator.ag_frame collections.AsyncGenerator.ag_running -collections.ByteString +collections.ByteString # see comments in py3_common.txt collections.Callable collections.ItemsView.__reversed__ collections.KeysView.__reversed__ diff --git a/tests/stubtest_allowlists/py39.txt b/tests/stubtest_allowlists/py39.txt index 5abe7a7b8542..fc108f51e528 100644 --- a/tests/stubtest_allowlists/py39.txt +++ b/tests/stubtest_allowlists/py39.txt @@ -27,7 +27,7 @@ collections.AsyncGenerator.ag_await collections.AsyncGenerator.ag_code collections.AsyncGenerator.ag_frame collections.AsyncGenerator.ag_running -collections.ByteString +collections.ByteString # see comments in py3_common.txt collections.Callable collections.Mapping.__reversed__ # Set to None at runtime for a better error message collections.ItemsView.__reversed__ From ee57021948f38097bf178d1c1c54e10ade978add Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Tue, 21 Feb 2023 16:59:24 +0000 Subject: [PATCH 8/9] Fix flake8 --- stdlib/typing.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stdlib/typing.pyi b/stdlib/typing.pyi index e3c21c27ddfd..10f6a98533ea 100644 --- a/stdlib/typing.pyi +++ b/stdlib/typing.pyi @@ -715,7 +715,7 @@ class TextIO(IO[str]): @abstractmethod def __enter__(self) -> TextIO: ... -ByteString = bytes | bytearray | memoryview +ByteString: typing_extensions.TypeAlias = bytes | bytearray | memoryview # Functions From 2217fff9a8a6ce155cc597604b2d8c5a117f501b Mon Sep 17 00:00:00 2001 From: Shantanu <12621235+hauntsaninja@users.noreply.github.com> Date: Sun, 7 May 2023 17:44:42 -0700 Subject: [PATCH 9/9] Update tests/stubtest_allowlists/py3_common.txt --- tests/stubtest_allowlists/py3_common.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/stubtest_allowlists/py3_common.txt b/tests/stubtest_allowlists/py3_common.txt index 481ebf6af72b..9a20449ad95c 100644 --- a/tests/stubtest_allowlists/py3_common.txt +++ b/tests/stubtest_allowlists/py3_common.txt @@ -610,7 +610,7 @@ typing.IO.__iter__ # See https://github.com/python/typeshed/commit/97bc450acd60 # Pretend typing.ByteString is a Union, to better match its documented semantics. # As a side effect, this changes the definition of collections.abc.ByteString, which is okay, -# because it's not an ABC that makes any sense. +# because it's not an ABC that makes any sense and was deprecated in 3.12 _collections_abc.ByteString # ==========