Skip to content

Commit

Permalink
Escalate the distinction between data=... and content=... to be stric…
Browse files Browse the repository at this point in the history
…ter (#1573)
  • Loading branch information
tomchristie authored Apr 16, 2021
1 parent 073a328 commit 397aad9
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 10 deletions.
6 changes: 3 additions & 3 deletions docs/compatibility.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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).
3 changes: 3 additions & 0 deletions httpx/_content.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import inspect
import warnings
from json import dumps as json_dumps
from typing import (
Any,
Expand Down Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion tests/client/test_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,7 @@ async def 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
Expand Down
6 changes: 3 additions & 3 deletions tests/models/test_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ async def streaming_body(data):

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"

Expand All @@ -129,7 +129,7 @@ def streaming_body(data):
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"

Expand All @@ -155,7 +155,7 @@ async def streaming_body(data):
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"


Expand Down
9 changes: 6 additions & 3 deletions tests/test_content.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -66,7 +67,8 @@ def hello_world():
[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)

Expand Down Expand Up @@ -95,7 +97,8 @@ async def hello_world():
[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)

Expand Down

0 comments on commit 397aad9

Please sign in to comment.