]> git.ipfire.org Git - thirdparty/httpx.git/commitdiff
Raise TypeError if content is passed a 'dict' instance. (#2495)
authorTom Christie <tom@tomchristie.com>
Sun, 11 Dec 2022 19:12:08 +0000 (19:12 +0000)
committerGitHub <noreply@github.com>
Sun, 11 Dec 2022 19:12:08 +0000 (19:12 +0000)
httpx/_content.py
tests/test_content.py

index 3cbca7ac691dc30e3e9e0655771ea6027974ccad..1c450b7595942cb6c169f65272a19f74bd2d8f56 100644 (file)
@@ -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:
index d9b4ed8e74afaaeb17a420b369d32f25765270fc..6a1303cc80ce95c7aec31ccbc07e4ca42deb79ba 100644 (file)
@@ -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():