Skip to content
Myr

Escalate the distinction between data=... and content=... to be stricter (#1573)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Tom Christie · committed by GitHub · parent 073a328 · Browse files · 5 files+16 added−10 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

5 files+16 added−10 removed

  • tests:+10 added−7 removed
  • other:+6 added−3 removed

5 files+16 added−10 removed

modified:docs/compatibility.md+3 added-3 removed
modified:httpx/_content.py+3 added
1import inspect2import warnings3from json import dumps as json_dumps4from typing import (5 Any,6 AsyncIterable,
141 unmodified lines
148 #149 # However for compat with requests, we *do* still support150 # `data=<bytes...>` usages. We deal with that case here, treating it151 # as if `content=<...>` had been supplied instead.152 message = "Use 'content=<...>' to upload raw bytes/text content."153 warnings.warn(message, DeprecationWarning)154 return encode_content(data)155156 if content is not None:157 return encode_content(content)
30 unmodified lines
modified:tests/client/test_auth.py+1 added-1 removed
628 unmodified lines
629 yield b"Example request body" # pragma: nocover630631 async with httpx.AsyncClient(transport=httpx.MockTransport(app)) as client:632 with pytest.raises(httpx.StreamConsumed):633 await client.post(url, data=streaming_body(), auth=auth)633 await client.post(url, content=streaming_body(), auth=auth)634635636@pytest.mark.asyncio637async def test_async_auth_reads_response_body() -> None:
62 unmodified lines
modified:tests/models/test_requests.py+3 added-3 removed
110 unmodified lines
111 yield data # pragma: nocover112113 data = streaming_body(b"test 123")114115 request = httpx.Request("POST", "http://example.org", data=data)115 request = httpx.Request("POST", "http://example.org", content=data)116 assert "Content-Length" not in request.headers117 assert request.headers["Transfer-Encoding"] == "chunked"118119
8 unmodified lines
128129 data = streaming_body(b"abcd")130131 headers = {"Content-Length": "4"}132 request = httpx.Request("POST", "http://example.org", data=data, headers=headers)132 request = httpx.Request("POST", "http://example.org", content=data, headers=headers)133 assert "Transfer-Encoding" not in request.headers134 assert request.headers["Content-Length"] == "4"135136
17 unmodified lines
154155 data = streaming_body(b"test 123")156 headers = {"Content-Length": "8"}157158 request = httpx.Request("POST", "http://example.org", data=data, headers=headers)158 request = httpx.Request("POST", "http://example.org", content=data, headers=headers)159 assert request.headers["Content-Length"] == "8"160161162def test_url():
13 unmodified lines
modified:tests/test_content.py+6 added-3 removed
33 unmodified lines
34 assert sync_content == b"Hello, world!"35 assert async_content == b"Hello, world!"3637 # Support 'data' for compat with requests.38 with pytest.warns(DeprecationWarning):38 headers, stream = encode_request(data=b"Hello, world!") # type: ignore39 headers, stream = encode_request(data=b"Hello, world!") # type: ignore40 assert isinstance(stream, typing.Iterable)41 assert isinstance(stream, typing.AsyncIterable)4243 sync_content = b"".join([part for part in stream])
22 unmodified lines
66 with pytest.raises(httpx.StreamConsumed):67 [part for part in stream]6869 # Support 'data' for compat with requests.70 with pytest.warns(DeprecationWarning):69 headers, stream = encode_request(data=hello_world()) # type: ignore71 headers, stream = encode_request(data=hello_world()) # type: ignore72 assert isinstance(stream, typing.Iterable)73 assert not isinstance(stream, typing.AsyncIterable)7475 content = b"".join([part for part in stream])
20 unmodified lines
96 with pytest.raises(httpx.StreamConsumed):97 [part async for part in stream]9899 # Support 'data' for compat with requests.100 with pytest.warns(DeprecationWarning):98 headers, stream = encode_request(data=hello_world()) # type: ignore101 headers, stream = encode_request(data=hello_world()) # type: ignore102 assert not isinstance(stream, typing.Iterable)103 assert isinstance(stream, typing.AsyncIterable)104105 content = b"".join([part async for part in stream])
308 unmodified lines