Skip to content

Headers.__eq__ raises TypeError for non-iterable operands instead of returning False #3899

Backlog

Description

@KaelLee335

Description

Headers.__eq__ only catches ValueError from Headers(other). For non-iterable objects such as int or object(), iterating raises TypeError ('int' object is not iterable), which is not caught, so Headers() == 123 raises instead of returning False.

The data-model convention (and dict's behaviour) is that == against an unrelated type returns False. As a MutableMapping[str, str] subclass, Headers should follow that contract.

Steps to reproduce

from httpx import Headers

h = Headers({"X-Foo": "bar"})
for other in [123, "plain string", object(), [("a", "b", "c")]]:
    try:
        print(f"Headers() == {other!r} ->", h == other)
    except Exception as e:
        print(f"Headers() == {other!r} raised: {type(e).__name__}: {e}")

Actual output:

Headers() == 123 raised: TypeError: 'int' object is not iterable
Headers() == 'plain string' -> False
Headers() == <object object at 0x...> raised: TypeError: 'object' object is not iterable
Headers() == [('a', 'b', 'c')] -> False

Expected behavior

Headers() == 123 and Headers() == object() should return False, matching dict() == 123.

Suggested fix

Broaden the exception handling to except (ValueError, TypeError): return False, or guard with an isinstance(other, (Headers, Mapping, Sequence)) check before comparing.

Environment

  • httpx 0.29.0 (main @ e347fb5)
  • Python 3.13

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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