Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Raise TypeError if content is passed a 'dict' instance. #2495

Merged
merged 2 commits into from
Dec 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion httpx/_content.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,11 @@ def encode_content(
headers = {"Content-Length": str(content_length)} if body else {}
return headers, ByteStream(body)

elif isinstance(content, Iterable):
elif isinstance(content, Iterable) and not isinstance(content, dict):
# `not isinstance(content, dict)` is a bit oddly specific, but it
# catches a case that's easy for users to make in error, and would
# otherwise pass through here, like any other bytes-iterable,
# because `dict` happens to be iterable. See issue #2491.
content_length_or_none = peek_filelike_length(content)

if content_length_or_none is None:
Expand Down
3 changes: 3 additions & 0 deletions tests/test_content.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,9 @@ def test_invalid_argument():
with pytest.raises(TypeError):
httpx.Request(method, url, content=123) # type: ignore

with pytest.raises(TypeError):
httpx.Request(method, url, content={"a": "b"}) # type: ignore


@pytest.mark.asyncio
async def test_multipart_multiple_files_single_input_content():
Expand Down