Skip to content

decode() error message uses broken format string #47

Description

@sumanjeet0012

The TypeError in decode() uses "multihash should be bytes, not {}" without an f-string prefix, so the {} is printed literally instead of being replaced with the actual type.

Problem

In multihash/multihash.py, the decode() function:

def decode(multihash):
    if not isinstance(multihash, bytes):
        raise TypeError("multihash should be bytes, not {}", type(multihash))
        #                     ↑ Missing 'f' prefix — {} is literal

When called with a non-bytes argument:

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

Proposed Solution

Fix the f-string:

def decode(multihash):
    if not isinstance(multihash, bytes):
        raise TypeError(f"multihash should be bytes, not {type(multihash)}")

Add a test:

def test_decode_type_error_message():
    with pytest.raises(TypeError, match="multihash should be bytes, not"):
        decode("not bytes")

Related

  • File: multihash/multihash.py, decode() function

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions