Skip to content

Accept Any in bytes and bytearray percent-formatting - #16529

Merged
srittau merged 2 commits into
python:mainfrom
Cherith1222:fix/16528-bytes-mod
Oct 14, 2026
Merged

srittau merged 2 commits into
python:mainfrom
Cherith1222:fix/16528-bytes-mod

Conversation

@Cherith1222

@Cherith1222 Cherith1222 commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #16528.

bytes.__mod__ and bytearray.__mod__ take Any. The format string chooses the conversion, and not every value works with every format, so this is not object.

https://docs.python.org/3/library/stdtypes.html#printf-style-bytes-formatting
https://peps.python.org/pep-0461/

Agent: Cursor (Grok 4.7)
Cherith reviewed this diff and confirmed it with the person who helped draft it.

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).
@github-actions

This comment has been minimized.

@jonathandung

Copy link
Copy Markdown
Contributor

The title should be "Accept object in bytes and bytearray percent-formatting".

@Cherith1222 Cherith1222 changed the title Accept numbers in bytes and bytearray percent-formatting Accept object in bytes and bytearray percent-formatting Oct 14, 2026
@Cherith1222

Copy link
Copy Markdown
Contributor Author

Updated the title.

Comment thread stdlib/builtins.pyi Outdated
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: ...

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

We should use Any as argument (as it was before), since not literally every value is supported in every situation. The comment also makes sense as documentation for the Any (which we require nowadays).

Same below.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Updated both arguments to Any and kept the comment.

Not every value works with every format string, so the argument stays Any and the comment stays with it.

AI-assisted (Cursor).
@Cherith1222 Cherith1222 changed the title Accept object in bytes and bytearray percent-formatting Accept Any in bytes and bytearray percent-formatting Oct 14, 2026
@github-actions

Copy link
Copy Markdown
Contributor

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

@srittau
srittau merged commit 541803c into python:main Oct 14, 2026
88 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bytes and bytearray __mod__ reject valid numeric formatting

3 participants