From a6052aff32002c92d3c32bb74cc8f72acfe13a98 Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Sat, 23 Apr 2022 10:10:11 -0700 Subject: [PATCH 1/3] binascii: Improve bytes types Most of these accept any buffer object. Found with https://github.com/python/mypy/pull/12661#issuecomment-1107533048 --- stdlib/binascii.pyi | 43 +++++++++++++++++++++++++------------------ 1 file changed, 25 insertions(+), 18 deletions(-) diff --git a/stdlib/binascii.pyi b/stdlib/binascii.pyi index 317bb9979b92..5f5b1fbd67fc 100644 --- a/stdlib/binascii.pyi +++ b/stdlib/binascii.pyi @@ -1,36 +1,43 @@ +from _typeshed import ReadableBuffer import sys -def a2b_uu(__data: str | bytes) -> bytes: ... +# Many functions in binascii accept buffer objects +# or ASCII-only strings. +_AsciiBuffer = str | ReadableBuffer + +def a2b_uu(__data: _AsciiBuffer) -> bytes: ... if sys.version_info >= (3, 7): - def b2a_uu(__data: bytes, *, backtick: bool = ...) -> bytes: ... + def b2a_uu(__data: ReadableBuffer, *, backtick: bool = ...) -> bytes: ... else: - def b2a_uu(__data: bytes) -> bytes: ... + def b2a_uu(__data: ReadableBuffer) -> bytes: ... -def a2b_base64(__data: str | bytes) -> bytes: ... -def b2a_base64(__data: bytes, *, newline: bool = ...) -> bytes: ... -def a2b_qp(data: str | bytes, header: bool = ...) -> bytes: ... -def b2a_qp(data: bytes, quotetabs: bool = ..., istext: bool = ..., header: bool = ...) -> bytes: ... +def a2b_base64(__data: _AsciiBuffer) -> bytes: ... +def b2a_base64(__data: ReadableBuffer, *, newline: bool = ...) -> bytes: ... +def a2b_qp(data: _AsciiBuffer, header: bool = ...) -> bytes: ... +def b2a_qp(data: ReadableBuffer, quotetabs: bool = ..., istext: bool = ..., header: bool = ...) -> bytes: ... if sys.version_info < (3, 11): - def a2b_hqx(__data: str | bytes) -> bytes: ... - def rledecode_hqx(__data: bytes) -> bytes: ... - def rlecode_hqx(__data: bytes) -> bytes: ... - def b2a_hqx(__data: bytes) -> bytes: ... + def a2b_hqx(__data: _AsciiBuffer) -> bytes: ... + def rledecode_hqx(__data: ReadableBuffer) -> bytes: ... + def rlecode_hqx(__data: ReadableBuffer) -> bytes: ... + def b2a_hqx(__data: ReadableBuffer) -> bytes: ... -def crc_hqx(__data: bytes, __crc: int) -> int: ... -def crc32(__data: bytes, __crc: int = ...) -> int: ... -def b2a_hex(__data: bytes) -> bytes: ... +def crc_hqx(__data: ReadableBuffer, __crc: int) -> int: ... +def crc32(__data: ReadableBuffer, __crc: int = ...) -> int: ... if sys.version_info >= (3, 8): - def hexlify(data: bytes, sep: str | bytes = ..., bytes_per_sep: int = ...) -> bytes: ... + # sep must be str or bytes, not bytearray or any other buffer + def b2a_hex(data: ReadableBuffer, sep: str | bytes = ..., bytes_per_sep: int = ...) -> bytes: ... + def hexlify(data: ReadableBuffer, sep: str | bytes = ..., bytes_per_sep: int = ...) -> bytes: ... else: - def hexlify(__data: bytes) -> bytes: ... + def b2a_hex(__data: ReadableBuffer) -> bytes: ... + def hexlify(__data: ReadableBuffer) -> bytes: ... -def a2b_hex(__hexstr: str | bytes) -> bytes: ... -def unhexlify(__hexstr: str | bytes) -> bytes: ... +def a2b_hex(__hexstr: _AsciiBuffer) -> bytes: ... +def unhexlify(__hexstr: _AsciiBuffer) -> bytes: ... class Error(ValueError): ... class Incomplete(Exception): ... From 1edc56d52136a12ad78706f7ae15185676724ce4 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 23 Apr 2022 17:12:55 +0000 Subject: [PATCH 2/3] [pre-commit.ci] auto fixes from pre-commit.com hooks --- stdlib/binascii.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stdlib/binascii.pyi b/stdlib/binascii.pyi index 5f5b1fbd67fc..af6ef102300f 100644 --- a/stdlib/binascii.pyi +++ b/stdlib/binascii.pyi @@ -1,5 +1,5 @@ -from _typeshed import ReadableBuffer import sys +from _typeshed import ReadableBuffer # Many functions in binascii accept buffer objects # or ASCII-only strings. From 97eb0636ac06366df8cb5b7111be52469928b57c Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Sat, 23 Apr 2022 10:27:27 -0700 Subject: [PATCH 3/3] TypeAlias --- stdlib/binascii.pyi | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/stdlib/binascii.pyi b/stdlib/binascii.pyi index af6ef102300f..53f72ad6a88f 100644 --- a/stdlib/binascii.pyi +++ b/stdlib/binascii.pyi @@ -1,9 +1,10 @@ import sys from _typeshed import ReadableBuffer +from typing_extensions import TypeAlias # Many functions in binascii accept buffer objects # or ASCII-only strings. -_AsciiBuffer = str | ReadableBuffer +_AsciiBuffer: TypeAlias = str | ReadableBuffer def a2b_uu(__data: _AsciiBuffer) -> bytes: ...