Skip to content

Fix Headers bytes-key lookups and __eq__ with non-iterables (#3898, #3899) - #3900

Open
KaelLee335 wants to merge 2 commits into
encode:masterfrom
KaelLee335:fix-headers-3898-3899
Open

KaelLee335 wants to merge 2 commits into
encode:masterfrom
KaelLee335:fix-headers-3898-3899

Conversation

@KaelLee335

Copy link
Copy Markdown

Fixes #3898, fixes #3899.

Two independent Headers bugs, both in httpx/_models.py:

#3898 — bytes keys cannot be looked up. HeaderTypes accepts Mapping[bytes, bytes] and Headers({b"X-Foo": b"bar"}) builds fine (keys normalized to 'x-foo'), but b"x-foo" in h, h[b"x-foo"] and h.get(b"x-foo") raise AttributeError because __getitem__ / __delitem__ / __contains__ call key.lower().encode() on a bytes key. Fix: use the existing _normalize_header_key() helper, which already handles both str and bytes. Inherited pop() / setdefault() benefit through __getitem__ / __delitem__.

#3899 — __eq__ raises on non-iterables. Headers() == 123 and Headers() == object() raise TypeError: 'int' object is not iterable because __eq__ only caught ValueError. Fix: catch (ValueError, TypeError), matching dict's convention that == against an unrelated type returns False.

Note on scope: this is not a blanket "all bytes APIs work" change. get_list(bytes) and __setitem__(bytes, ...) still only accept str; they are separate pre-existing gaps, not regressions introduced here. Likewise Headers == <invalid mapping key> can still raise AttributeError; those inputs are outside #3899's non-iterable case.

Tests

  • tests/models/test_headers.py: bytes-key in / getitem / get / del (all four str/bytes case combos, missing-key defaults, duplicate deletion), and Headers == 123 / object() returning False.
  • python -m pytest tests/models/test_headers.py -q — 48 passed.
  • ruff check passed.

This branch has not been deployed

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

Labels

None yet

1 participant