Skip to content

Commit a6052af

Browse files
committed
binascii: Improve bytes types
Most of these accept any buffer object. Found with python/mypy#12661 (comment)
1 parent 2a0fc1b commit a6052af

File tree

1 file changed

+25
-18
lines changed

1 file changed

+25
-18
lines changed

stdlib/binascii.pyi

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,43 @@
1+
from _typeshed import ReadableBuffer
12
import sys
23

3-
def a2b_uu(__data: str | bytes) -> bytes: ...
4+
# Many functions in binascii accept buffer objects
5+
# or ASCII-only strings.
6+
_AsciiBuffer = str | ReadableBuffer
7+
8+
def a2b_uu(__data: _AsciiBuffer) -> bytes: ...
49

510
if sys.version_info >= (3, 7):
6-
def b2a_uu(__data: bytes, *, backtick: bool = ...) -> bytes: ...
11+
def b2a_uu(__data: ReadableBuffer, *, backtick: bool = ...) -> bytes: ...
712

813
else:
9-
def b2a_uu(__data: bytes) -> bytes: ...
14+
def b2a_uu(__data: ReadableBuffer) -> bytes: ...
1015

11-
def a2b_base64(__data: str | bytes) -> bytes: ...
12-
def b2a_base64(__data: bytes, *, newline: bool = ...) -> bytes: ...
13-
def a2b_qp(data: str | bytes, header: bool = ...) -> bytes: ...
14-
def b2a_qp(data: bytes, quotetabs: bool = ..., istext: bool = ..., header: bool = ...) -> bytes: ...
16+
def a2b_base64(__data: _AsciiBuffer) -> bytes: ...
17+
def b2a_base64(__data: ReadableBuffer, *, newline: bool = ...) -> bytes: ...
18+
def a2b_qp(data: _AsciiBuffer, header: bool = ...) -> bytes: ...
19+
def b2a_qp(data: ReadableBuffer, quotetabs: bool = ..., istext: bool = ..., header: bool = ...) -> bytes: ...
1520

1621
if sys.version_info < (3, 11):
17-
def a2b_hqx(__data: str | bytes) -> bytes: ...
18-
def rledecode_hqx(__data: bytes) -> bytes: ...
19-
def rlecode_hqx(__data: bytes) -> bytes: ...
20-
def b2a_hqx(__data: bytes) -> bytes: ...
22+
def a2b_hqx(__data: _AsciiBuffer) -> bytes: ...
23+
def rledecode_hqx(__data: ReadableBuffer) -> bytes: ...
24+
def rlecode_hqx(__data: ReadableBuffer) -> bytes: ...
25+
def b2a_hqx(__data: ReadableBuffer) -> bytes: ...
2126

22-
def crc_hqx(__data: bytes, __crc: int) -> int: ...
23-
def crc32(__data: bytes, __crc: int = ...) -> int: ...
24-
def b2a_hex(__data: bytes) -> bytes: ...
27+
def crc_hqx(__data: ReadableBuffer, __crc: int) -> int: ...
28+
def crc32(__data: ReadableBuffer, __crc: int = ...) -> int: ...
2529

2630
if sys.version_info >= (3, 8):
27-
def hexlify(data: bytes, sep: str | bytes = ..., bytes_per_sep: int = ...) -> bytes: ...
31+
# sep must be str or bytes, not bytearray or any other buffer
32+
def b2a_hex(data: ReadableBuffer, sep: str | bytes = ..., bytes_per_sep: int = ...) -> bytes: ...
33+
def hexlify(data: ReadableBuffer, sep: str | bytes = ..., bytes_per_sep: int = ...) -> bytes: ...
2834

2935
else:
30-
def hexlify(__data: bytes) -> bytes: ...
36+
def b2a_hex(__data: ReadableBuffer) -> bytes: ...
37+
def hexlify(__data: ReadableBuffer) -> bytes: ...
3138

32-
def a2b_hex(__hexstr: str | bytes) -> bytes: ...
33-
def unhexlify(__hexstr: str | bytes) -> bytes: ...
39+
def a2b_hex(__hexstr: _AsciiBuffer) -> bytes: ...
40+
def unhexlify(__hexstr: _AsciiBuffer) -> bytes: ...
3441

3542
class Error(ValueError): ...
3643
class Incomplete(Exception): ...

0 commit comments

Comments
 (0)