◐ Shell
clean mode source ↗

builtins: Audit bytes arguments by JelleZijlstra · Pull Request #7631 · python/typeshed

JelleZijlstra

def __new__(cls: type[Self], __x: str | bytes | SupportsInt | SupportsIndex | SupportsTrunc = ...) -> Self: ...
def __new__(cls: type[Self], __x: str | ReadableBuffer | SupportsInt | SupportsIndex | SupportsTrunc = ...) -> Self: ...
@overload
def __new__(cls: type[Self], __x: str | bytes | bytearray, base: SupportsIndex) -> Self: ...

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

>>> int(memoryview(b"0xdeadbeef"), 16)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: int() can't convert non-string with explicit base
>>> int(memoryview(b"123"))
123

Showing that the first overload accepts buffers but the second doesn't.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

def from_bytes(
cls: type[Self],
bytes: Iterable[SupportsIndex] | SupportsBytes, # TODO buffer object argument
bytes: Iterable[SupportsIndex] | SupportsBytes | ReadableBuffer,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

>>> int.from_bytes([1, 2, 3])
66051
>>> int.from_bytes(memoryview(b"123"))
3224115
self, __sub: ReadableBuffer | SupportsIndex, __start: SupportsIndex | None = ..., __end: SupportsIndex | None = ...
) -> int: ...
if sys.version_info >= (3, 8):
def hex(self, sep: str | bytes = ..., bytes_per_sep: SupportsIndex = ...) -> str: ...

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

>>> b"xy".hex(memoryview(b"x"))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: sep must be str or bytes.
>>> b"xy".hex(bytearray(b"x"))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: sep must be str or bytes.
@overload
def __getitem__(self, __s: slice) -> bytes: ...
def __add__(self, __s: bytes) -> bytes: ...
def __add__(self, __s: ReadableBuffer) -> bytes: ...

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

>>> b"x" + memoryview(b"y")
b'xy'
def __setitem__(self, __s: slice, __x: Iterable[SupportsIndex] | bytes) -> None: ...
def __delitem__(self, __i: SupportsIndex | slice) -> None: ...
def __add__(self, __s: bytes) -> bytearray: ...
def __iadd__(self: Self, __s: Iterable[int]) -> Self: ...

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was wrong; ba += [1, 2, 3] fails

opener: _Opener | None = ...,
) -> IO[Any]: ...
def ord(__c: str | bytes) -> int: ...
def ord(__c: str | bytes | bytearray) -> int: ...

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

>>> ord(memoryview(b"x"))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: ord() expected string of length 1, but memoryview found