From 3ab0b25c5b3a7b5b27a59dcc2e47b7d26fb9c985 Mon Sep 17 00:00:00 2001 From: cherith <315844584+Cherith1222@users.noreply.github.com> Date: Wed, 14 Oct 2026 11:08:14 +0800 Subject: [PATCH 1/2] Accept numbers in bytes and bytearray percent-formatting The format string chooses the conversion, so an int is valid for b%d and an arbitrary object is valid for %a and %r. The parameter type now matches str.__mod__. AI-assisted (Cursor). --- stdlib/builtins.pyi | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/stdlib/builtins.pyi b/stdlib/builtins.pyi index 9c8394ced492..42353e16da3c 100644 --- a/stdlib/builtins.pyi +++ b/stdlib/builtins.pyi @@ -66,7 +66,7 @@ from typing import ( # noqa: Y022 # ruff: ignore[deprecated-import] # we can't import `Literal` from typing or mypy crashes: see #11247 # ruff: ignore[deprecated-import] -from typing_extensions import Buffer, Literal, LiteralString, Self, TypeIs, TypeVarTuple, deprecated, disjoint_base # noqa: Y023 +from typing_extensions import Literal, LiteralString, Self, TypeIs, TypeVarTuple, deprecated, disjoint_base # noqa: Y023 if sys.version_info >= (3, 14): from _typeshed import AnnotateFunc @@ -828,7 +828,8 @@ class bytes(Sequence[int]): def __add__(self, value: ReadableBuffer, /) -> bytes: ... def __mul__(self, value: SupportsIndex, /) -> bytes: ... def __rmul__(self, value: SupportsIndex, /) -> bytes: ... - def __mod__(self, value: Buffer | SupportsBytes | tuple[Buffer | SupportsBytes, ...], /) -> bytes: ... + # The format string chooses the conversion, including numbers and %a/%r (PEP 461). + def __mod__(self, value: object, /) -> bytes: ... def __rmod__(self, value: bytes, /) -> bytes: ... # Incompatible with Sequence.__contains__ def __contains__(self, key: SupportsIndex | ReadableBuffer, /) -> bool: ... # type: ignore[override] @@ -960,7 +961,8 @@ class bytearray(MutableSequence[int]): def __mul__(self, value: SupportsIndex, /) -> bytearray: ... def __rmul__(self, value: SupportsIndex, /) -> bytearray: ... def __imul__(self, value: SupportsIndex, /) -> Self: ... - def __mod__(self, value: Buffer | SupportsBytes | tuple[Buffer | SupportsBytes, ...], /) -> bytearray: ... + # The format string chooses the conversion, including numbers and %a/%r (PEP 461). + def __mod__(self, value: object, /) -> bytearray: ... def __rmod__(self, value: bytearray, /) -> bytearray: ... # Incompatible with Sequence.__contains__ def __contains__(self, key: SupportsIndex | ReadableBuffer, /) -> bool: ... # type: ignore[override] From 4a8486fbb38341c39e7fd7d251099ab117430ab2 Mon Sep 17 00:00:00 2001 From: cherith <315844584+Cherith1222@users.noreply.github.com> Date: Wed, 14 Oct 2026 19:27:23 +0800 Subject: [PATCH 2/2] Use Any for bytes and bytearray percent-formatting Not every value works with every format string, so the argument stays Any and the comment stays with it. AI-assisted (Cursor). --- stdlib/builtins.pyi | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/stdlib/builtins.pyi b/stdlib/builtins.pyi index 42353e16da3c..4c78ea9b478f 100644 --- a/stdlib/builtins.pyi +++ b/stdlib/builtins.pyi @@ -829,7 +829,7 @@ class bytes(Sequence[int]): def __mul__(self, value: SupportsIndex, /) -> bytes: ... def __rmul__(self, value: SupportsIndex, /) -> bytes: ... # The format string chooses the conversion, including numbers and %a/%r (PEP 461). - def __mod__(self, value: object, /) -> bytes: ... + def __mod__(self, value: Any, /) -> bytes: ... def __rmod__(self, value: bytes, /) -> bytes: ... # Incompatible with Sequence.__contains__ def __contains__(self, key: SupportsIndex | ReadableBuffer, /) -> bool: ... # type: ignore[override] @@ -962,7 +962,7 @@ class bytearray(MutableSequence[int]): def __rmul__(self, value: SupportsIndex, /) -> bytearray: ... def __imul__(self, value: SupportsIndex, /) -> Self: ... # The format string chooses the conversion, including numbers and %a/%r (PEP 461). - def __mod__(self, value: object, /) -> bytearray: ... + def __mod__(self, value: Any, /) -> bytearray: ... def __rmod__(self, value: bytearray, /) -> bytearray: ... # Incompatible with Sequence.__contains__ def __contains__(self, key: SupportsIndex | ReadableBuffer, /) -> bool: ... # type: ignore[override]