]> git.ipfire.org Git - thirdparty/httpx.git/commitdiff
Escalate the distinction between data=... and content=... to be stricter (#1573)
authorTom Christie <tom@tomchristie.com>
Fri, 16 Apr 2021 09:06:12 +0000 (10:06 +0100)
committerGitHub <noreply@github.com>
Fri, 16 Apr 2021 09:06:12 +0000 (10:06 +0100)
docs/compatibility.md
httpx/_content.py
tests/client/test_auth.py
tests/models/test_requests.py
tests/test_content.py

index b71bc38c775903ba3d93e9fdd2daa31778402a51..c330c15d222b09fcd58fad4a12ed5a989d7c36c8 100644 (file)
@@ -28,8 +28,8 @@ And using `data=...` to send form data:
 httpx.post(..., data={"message": "Hello, world"})
 ```
 
-If you're using a type checking tool such as `mypy`, you'll see warnings issues if using test/byte content with the `data` argument.
-However, for compatibility reasons with `requests`, we do still handle the case where `data=...` is used with raw binary and text contents.
+Using the `data=<text/byte content>` will raise a deprecation warning,
+and is expected to be fully removed with the HTTPX 1.0 release.
 
 ## Content encoding
 
@@ -147,6 +147,6 @@ while request is not None:
 
 `requests` allows event hooks to mutate `Request` and `Response` objects. See [examples](https://requests.readthedocs.io/en/master/user/advanced/#event-hooks) given in the documentation for `requests`.
 
-In HTTPX, event hooks may access properties of requests and responses, but event hook callbacks cannot mutate the original request/response. 
+In HTTPX, event hooks may access properties of requests and responses, but event hook callbacks cannot mutate the original request/response.
 
 If you are looking for more control, consider checking out [Custom Transports](advanced.md#custom-transports).
index 4b16d1e970af2e6a21e33f23348090550ffb44fa..e4a906520b87ea9c345909c71f3974d2aa5d246a 100644 (file)
@@ -1,4 +1,5 @@
 import inspect
+import warnings
 from json import dumps as json_dumps
 from typing import (
     Any,
@@ -148,6 +149,8 @@ def encode_request(
         # However for compat with requests, we *do* still support
         # `data=<bytes...>` usages. We deal with that case here, treating it
         # as if `content=<...>` had been supplied instead.
+        message = "Use 'content=<...>' to upload raw bytes/text content."
+        warnings.warn(message, DeprecationWarning)
         return encode_content(data)
 
     if content is not None:
index c41afeff870790686d893bc417ec5ef84bcc60f3..b6cb42d0bb67a7e92062f736bf7e9d6ea5b6ba69 100644 (file)
@@ -630,7 +630,7 @@ async def test_digest_auth_unavailable_streaming_body():
 
     async with httpx.AsyncClient(transport=httpx.MockTransport(app)) as client:
         with pytest.raises(httpx.StreamConsumed):
-            await client.post(url, data=streaming_body(), auth=auth)
+            await client.post(url, content=streaming_body(), auth=auth)
 
 
 @pytest.mark.asyncio
index 8756a45985a4a4c9ce46a01c7e1e2fa5e027d413..fc55791cef5c314e528a690be821a95ddfcaf751 100644 (file)
@@ -112,7 +112,7 @@ def test_transfer_encoding_header():
 
     data = streaming_body(b"test 123")
 
-    request = httpx.Request("POST", "http://example.org", data=data)
+    request = httpx.Request("POST", "http://example.org", content=data)
     assert "Content-Length" not in request.headers
     assert request.headers["Transfer-Encoding"] == "chunked"
 
@@ -129,7 +129,7 @@ def test_ignore_transfer_encoding_header_if_content_length_exists():
     data = streaming_body(b"abcd")
 
     headers = {"Content-Length": "4"}
-    request = httpx.Request("POST", "http://example.org", data=data, headers=headers)
+    request = httpx.Request("POST", "http://example.org", content=data, headers=headers)
     assert "Transfer-Encoding" not in request.headers
     assert request.headers["Content-Length"] == "4"
 
@@ -155,7 +155,7 @@ def test_override_content_length_header():
     data = streaming_body(b"test 123")
     headers = {"Content-Length": "8"}
 
-    request = httpx.Request("POST", "http://example.org", data=data, headers=headers)
+    request = httpx.Request("POST", "http://example.org", content=data, headers=headers)
     assert request.headers["Content-Length"] == "8"
 
 
index d692a3036161163b502396955d97d0f3902dff2c..b10596619863ca06e97ae6b6c8032c9fcae6dc5c 100644 (file)
@@ -35,7 +35,8 @@ async def test_bytes_content():
     assert async_content == b"Hello, world!"
 
     # Support 'data' for compat with requests.
-    headers, stream = encode_request(data=b"Hello, world!")  # type: ignore
+    with pytest.warns(DeprecationWarning):
+        headers, stream = encode_request(data=b"Hello, world!")  # type: ignore
     assert isinstance(stream, typing.Iterable)
     assert isinstance(stream, typing.AsyncIterable)
 
@@ -66,7 +67,8 @@ async def test_iterator_content():
         [part for part in stream]
 
     # Support 'data' for compat with requests.
-    headers, stream = encode_request(data=hello_world())  # type: ignore
+    with pytest.warns(DeprecationWarning):
+        headers, stream = encode_request(data=hello_world())  # type: ignore
     assert isinstance(stream, typing.Iterable)
     assert not isinstance(stream, typing.AsyncIterable)
 
@@ -95,7 +97,8 @@ async def test_aiterator_content():
         [part async for part in stream]
 
     # Support 'data' for compat with requests.
-    headers, stream = encode_request(data=hello_world())  # type: ignore
+    with pytest.warns(DeprecationWarning):
+        headers, stream = encode_request(data=hello_world())  # type: ignore
     assert not isinstance(stream, typing.Iterable)
     assert isinstance(stream, typing.AsyncIterable)