Skip to content

fix: correct broken format string in decode() TypeError message - #59

Open
AmSach wants to merge 1 commit into
multiformats:masterfrom
AmSach:fix/decode-type-error-message
Open

fix: correct broken format string in decode() TypeError message#59
AmSach wants to merge 1 commit into
multiformats:masterfrom
AmSach:fix/decode-type-error-message

Conversation

@AmSach

@AmSach AmSach commented Jul 5, 2026

Copy link
Copy Markdown

Fixes #47.

What was broken

The decode() function in multihash/multihash.py raised a TypeError using a non-f-string format:

raise TypeError("multihash should be bytes, not {}", type(multihash))

Because the literal string is not prefixed with f, the {} placeholder is not interpolated — it is printed verbatim, with the offending type captured only as a separate element of e.args. The user sees:

TypeError: multihash should be bytes, not {}
# Expected: TypeError: multihash should be bytes, not <class 'str'>

Fix

Switch the literal to a proper f-string so the type is interpolated into the message at raise time:

raise TypeError(f"multihash should be bytes, not {type(multihash)}")

Tested

  • Added test_decode_type_error_message_interpolates_type in tests/test_multihash.py that asserts the message contains "should be bytes, not" and does not contain the literal "{}".
  • Verified the new test fails on the unpatched code (literal {} present) and passes on the patched code.
  • Full test suite: 229 tests pass.
  • Lint: ruff check clean on touched files.

Files changed

  • multihash/multihash.py — fix the format string (1 line)
  • tests/test_multihash.py — add regression test (21 lines)
  • newsfragments/47.bugfix.rst — release note (new file)

The decode() function raised TypeError with a non-f-string format,
causing '{}' to be printed literally instead of being replaced with the
actual type. Switch to an f-string so the offending type is interpolated
into the error message (e.g. "<class 'str'>").

Closes multiformats#47
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.

decode() error message uses broken format string

1 participant