Skip to content

Encode bytes subclasses with a custom repr like plain bytes - #2859

Merged
alexmojaki merged 1 commit into
pydantic:mainfrom
JoeyTan21:fix/bytes-subclass-repr
Nov 23, 2026
Merged

alexmojaki merged 1 commit into
pydantic:mainfrom
JoeyTan21:fix/bytes-subclass-repr

Conversation

@JoeyTan21

@JoeyTan21 JoeyTan21 commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

Closes #2858

The bytes encoder returns repr(o)[2:-1] to strip the b prefix and the quotes, which assumes the repr of plain bytes. Under NumPy 2 the repr of numpy.bytes_ is np.bytes_(b'hello'), so a numpy.bytes_ attribute (for example an element of an array with a bytes dtype) was recorded as a truncated repr instead of hello. Any bytes subclass that overrides its repr was affected the same way.

Changes:

  • logfire-sdk/logfire/_internal/json_encoder.py: the bytes encoder calls the repr of the bytes base class directly, so subclasses are encoded exactly like plain bytes. The bytearray encoder already converts to bytes first and is unchanged.
  • tests/test_json_args.py: a numpy_bytes case in test_log_non_scalar_args, next to the existing custom_bytes_class case. It fails on main with the truncated repr and passes with the fix.

Disclosure: found, reproduced and the draft fix tested locally with the help of an AI coding agent (Claude Code); reviewed before filing.

View guided diff

The bytes encoder sliced repr(o)[2:-1] to drop the b prefix and quotes,
which assumes the repr of plain bytes. numpy.bytes_ under NumPy 2 has the
repr np.bytes_(b'...'), so it was recorded as .bytes_(b'...' instead of
its content. Use bytes.__repr__ so subclasses are encoded like bytes.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@veria-ai

veria-ai Bot commented Nov 21, 2026 •

Copy link
Copy Markdown

PR overview

No security issues were found in this pull request.

Reviews

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

No issues found across 2 files

Confidence score: 5/5

  • Automated review surfaced no issues in the provided summaries.
  • No files require special attention.

View guided diff | Re-trigger cubic

@github-actions

Copy link
Copy Markdown
Contributor

Coverage report

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  logfire-sdk/logfire/_internal
  json_encoder.py
Project Total  

This report was generated by python-coverage-comment-action

@alexmojaki
alexmojaki added this pull request to the merge queue Nov 23, 2026
Merged via the queue into pydantic:main with commit 8c1a120 Nov 23, 2026
31 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.

numpy.bytes_ attributes are recorded as a truncated repr instead of their content

2 participants