Skip to content
Myr

Enabling `ruff` C416 (#3001)

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

* Enabled C416 in ruff * Ran ruff on all files * Ran ruff format * Update pyproject.toml ---------

authored by James Braza · committed by GitHub · parent 2c51edd · Browse files · 6 files+41 added−41 removed
This commit

State

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

Trailers

  • Co-authored-by: Tom Christie <tom@tomchristie.com>

Diff stats

6 files+41 added−41 removed

  • tests:+39 added−39 removed
  • other:+2 added−2 removed

6 files+41 added−41 removed

modified:httpx/_content.py+1 added-1 removed
103 unmodified lines
104 yield b"" # pragma: no cover105106107def encode_content(108 content: Union[str, bytes, Iterable[bytes], AsyncIterable[bytes]]108 content: Union[str, bytes, Iterable[bytes], AsyncIterable[bytes]],109) -> Tuple[Dict[str, str], Union[SyncByteStream, AsyncByteStream]]:110 if isinstance(content, (bytes, str)):111 body = content.encode("utf-8") if isinstance(content, str) else content112 content_length = len(body)
126 unmodified lines
modified:httpx/_utils.py+1 added-1 removed
150 unmodified lines
151SENSITIVE_HEADERS = {"authorization", "proxy-authorization"}152153154def obfuscate_sensitive_headers(155 items: typing.Iterable[typing.Tuple[typing.AnyStr, typing.AnyStr]]155 items: typing.Iterable[typing.Tuple[typing.AnyStr, typing.AnyStr]],156) -> typing.Iterator[typing.Tuple[typing.AnyStr, typing.AnyStr]]:157 for k, v in items:158 if to_str(k.lower()) in SENSITIVE_HEADERS:159 v = to_bytes_or_str("[secure]", match_type_of=v)
283 unmodified lines
modified:tests/client/test_redirects.py+1 added-1 removed
343 unmodified lines
344345class ConsumeBodyTransport(httpx.MockTransport):346 def handle_request(self, request: httpx.Request) -> httpx.Response:347 assert isinstance(request.stream, httpx.SyncByteStream)348 [_ for _ in request.stream]348 list(request.stream)349 return self.handler(request) # type: ignore[return-value]350351352def test_cannot_redirect_streaming_body():
95 unmodified lines
modified:tests/models/test_requests.py+1 added-1 removed
80 unmodified lines
81 request = httpx.Request("POST", "http://example.org", json={"test": 123})82 request.read()83 assert request.stream is not None84 assert isinstance(request.stream, typing.Iterable)85 content = b"".join([part for part in request.stream])85 content = b"".join(list(request.stream))86 assert content == request.content878889@pytest.mark.anyio
139 unmodified lines
modified:tests/models/test_responses.py+17 added-17 removed
395 unmodified lines
396397398def test_iter_raw_with_chunksize():399 response = httpx.Response(200, content=streaming_body())400 parts = [part for part in response.iter_raw(chunk_size=5)]400 parts = list(response.iter_raw(chunk_size=5))401 assert parts == [b"Hello", b", wor", b"ld!"]402403 response = httpx.Response(200, content=streaming_body())404 parts = [part for part in response.iter_raw(chunk_size=7)]404 parts = list(response.iter_raw(chunk_size=7))405 assert parts == [b"Hello, ", b"world!"]406407 response = httpx.Response(200, content=streaming_body())408 parts = [part for part in response.iter_raw(chunk_size=13)]408 parts = list(response.iter_raw(chunk_size=13))409 assert parts == [b"Hello, world!"]410411 response = httpx.Response(200, content=streaming_body())412 parts = [part for part in response.iter_raw(chunk_size=20)]412 parts = list(response.iter_raw(chunk_size=20))413 assert parts == [b"Hello, world!"]414415416def test_iter_raw_doesnt_return_empty_chunks():
4 unmodified lines
421 yield b""422423 response = httpx.Response(200, content=streaming_body_with_empty_chunks())424425 parts = [part for part in response.iter_raw()]425 parts = list(response.iter_raw())426 assert parts == [b"Hello, ", b"world!"]427428429def test_iter_raw_on_iterable():
14 unmodified lines
444 content=async_streaming_body(),445 )446447 with pytest.raises(RuntimeError):448 [part for part in response.iter_raw()]448 list(response.iter_raw())449450451def test_close_on_async():452 response = httpx.Response(
84 unmodified lines
537538539def test_iter_bytes_with_chunk_size():540 response = httpx.Response(200, content=streaming_body())541 parts = [part for part in response.iter_bytes(chunk_size=5)]541 parts = list(response.iter_bytes(chunk_size=5))542 assert parts == [b"Hello", b", wor", b"ld!"]543544 response = httpx.Response(200, content=streaming_body())545 parts = [part for part in response.iter_bytes(chunk_size=13)]545 parts = list(response.iter_bytes(chunk_size=13))546 assert parts == [b"Hello, world!"]547548 response = httpx.Response(200, content=streaming_body())549 parts = [part for part in response.iter_bytes(chunk_size=20)]549 parts = list(response.iter_bytes(chunk_size=20))550 assert parts == [b"Hello, world!"]551552553def test_iter_bytes_with_empty_response():554 response = httpx.Response(200, content=b"")555 parts = [part for part in response.iter_bytes()]555 parts = list(response.iter_bytes())556 assert parts == []557558559def test_iter_bytes_doesnt_return_empty_chunks():
4 unmodified lines
564 yield b""565566 response = httpx.Response(200, content=streaming_body_with_empty_chunks())567568 parts = [part for part in response.iter_bytes()]568 parts = list(response.iter_bytes())569 assert parts == [b"Hello, ", b"world!"]570571572@pytest.mark.anyio
37 unmodified lines
610611612def test_iter_text_with_chunk_size():613 response = httpx.Response(200, content=b"Hello, world!")614 parts = [part for part in response.iter_text(chunk_size=5)]614 parts = list(response.iter_text(chunk_size=5))615 assert parts == ["Hello", ", wor", "ld!"]616617 response = httpx.Response(200, content=b"Hello, world!!")618 parts = [part for part in response.iter_text(chunk_size=7)]618 parts = list(response.iter_text(chunk_size=7))619 assert parts == ["Hello, ", "world!!"]620621 response = httpx.Response(200, content=b"Hello, world!")622 parts = [part for part in response.iter_text(chunk_size=7)]622 parts = list(response.iter_text(chunk_size=7))623 assert parts == ["Hello, ", "world!"]624625 response = httpx.Response(200, content=b"Hello, world!")626 parts = [part for part in response.iter_text(chunk_size=13)]626 parts = list(response.iter_text(chunk_size=13))627 assert parts == ["Hello, world!"]628629 response = httpx.Response(200, content=b"Hello, world!")630 parts = [part for part in response.iter_text(chunk_size=20)]630 parts = list(response.iter_text(chunk_size=20))631 assert parts == ["Hello, world!"]632633634@pytest.mark.anyio
28 unmodified lines
663 response = httpx.Response(664 200,665 content=b"Hello,\nworld!",666 )667 content = [line for line in response.iter_lines()]667 content = list(response.iter_lines())668 assert content == ["Hello,", "world!"]669670671@pytest.mark.anyio
366 unmodified lines
modified:tests/test_content.py+20 added-20 removed
13 unmodified lines
14 request = httpx.Request(method, url)15 assert isinstance(request.stream, httpx.SyncByteStream)16 assert isinstance(request.stream, httpx.AsyncByteStream)1718 sync_content = b"".join([part for part in request.stream])18 sync_content = b"".join(list(request.stream))19 async_content = b"".join([part async for part in request.stream])2021 assert request.headers == {"Host": "www.example.com", "Content-Length": "0"}22 assert sync_content == b""
5 unmodified lines
28 request = httpx.Request(method, url, content=b"Hello, world!")29 assert isinstance(request.stream, typing.Iterable)30 assert isinstance(request.stream, typing.AsyncIterable)3132 sync_content = b"".join([part for part in request.stream])32 sync_content = b"".join(list(request.stream))33 async_content = b"".join([part async for part in request.stream])3435 assert request.headers == {"Host": "www.example.com", "Content-Length": "13"}36 assert sync_content == b"Hello, world!"
4 unmodified lines
41 request = httpx.Request(method, url, data=b"Hello, world!") # type: ignore42 assert isinstance(request.stream, typing.Iterable)43 assert isinstance(request.stream, typing.AsyncIterable)4445 sync_content = b"".join([part for part in request.stream])45 sync_content = b"".join(list(request.stream))46 async_content = b"".join([part async for part in request.stream])4748 assert request.headers == {"Host": "www.example.com", "Content-Length": "13"}49 assert sync_content == b"Hello, world!"
5 unmodified lines
55 request = httpx.Request(method, url, content=io.BytesIO(b"Hello, world!"))56 assert isinstance(request.stream, typing.Iterable)57 assert not isinstance(request.stream, typing.AsyncIterable)5859 content = b"".join([part for part in request.stream])59 content = b"".join(list(request.stream))6061 assert request.headers == {"Host": "www.example.com", "Content-Length": "13"}62 assert content == b"Hello, world!"63
35 unmodified lines
99 request = httpx.Request(method, url, content=hello_world())100 assert isinstance(request.stream, typing.Iterable)101 assert not isinstance(request.stream, typing.AsyncIterable)102103 content = b"".join([part for part in request.stream])103 content = b"".join(list(request.stream))104105 assert request.headers == {106 "Host": "www.example.com",107 "Transfer-Encoding": "chunked",108 }109 assert content == b"Hello, world!"110111 with pytest.raises(httpx.StreamConsumed):112 [part for part in request.stream]112 list(request.stream)113114 # Support 'data' for compat with requests.115 with pytest.warns(DeprecationWarning):116 request = httpx.Request(method, url, data=hello_world()) # type: ignore117 assert isinstance(request.stream, typing.Iterable)118 assert not isinstance(request.stream, typing.AsyncIterable)119120 content = b"".join([part for part in request.stream])120 content = b"".join(list(request.stream))121122 assert request.headers == {123 "Host": "www.example.com",124 "Transfer-Encoding": "chunked",
42 unmodified lines
167 request = httpx.Request(method, url, json={"Hello": "world!"})168 assert isinstance(request.stream, typing.Iterable)169 assert isinstance(request.stream, typing.AsyncIterable)170171 sync_content = b"".join([part for part in request.stream])171 sync_content = b"".join(list(request.stream))172 async_content = b"".join([part async for part in request.stream])173174 assert request.headers == {175 "Host": "www.example.com",
9 unmodified lines
185 request = httpx.Request(method, url, data={"Hello": "world!"})186 assert isinstance(request.stream, typing.Iterable)187 assert isinstance(request.stream, typing.AsyncIterable)188189 sync_content = b"".join([part for part in request.stream])189 sync_content = b"".join(list(request.stream))190 async_content = b"".join([part async for part in request.stream])191192 assert request.headers == {193 "Host": "www.example.com",
9 unmodified lines
203 request = httpx.Request(method, url, data={"example": True})204 assert isinstance(request.stream, typing.Iterable)205 assert isinstance(request.stream, typing.AsyncIterable)206207 sync_content = b"".join([part for part in request.stream])207 sync_content = b"".join(list(request.stream))208 async_content = b"".join([part async for part in request.stream])209210 assert request.headers == {211 "Host": "www.example.com",
9 unmodified lines
221 request = httpx.Request(method, url, data={"example": None})222 assert isinstance(request.stream, typing.Iterable)223 assert isinstance(request.stream, typing.AsyncIterable)224225 sync_content = b"".join([part for part in request.stream])225 sync_content = b"".join(list(request.stream))226 async_content = b"".join([part async for part in request.stream])227228 assert request.headers == {229 "Host": "www.example.com",
9 unmodified lines
239 request = httpx.Request(method, url, data={"example": ["a", 1, True]})240 assert isinstance(request.stream, typing.Iterable)241 assert isinstance(request.stream, typing.AsyncIterable)242243 sync_content = b"".join([part for part in request.stream])243 sync_content = b"".join(list(request.stream))244 async_content = b"".join([part async for part in request.stream])245246 assert request.headers == {247 "Host": "www.example.com",
16 unmodified lines
264 )265 assert isinstance(request.stream, typing.Iterable)266 assert isinstance(request.stream, typing.AsyncIterable)267268 sync_content = b"".join([part for part in request.stream])268 sync_content = b"".join(list(request.stream))269 async_content = b"".join([part async for part in request.stream])270271 assert request.headers == {272 "Host": "www.example.com",
30 unmodified lines
303 request = httpx.Request(method, url, data=data, files=files, headers=headers)304 assert isinstance(request.stream, typing.Iterable)305 assert isinstance(request.stream, typing.AsyncIterable)306307 sync_content = b"".join([part for part in request.stream])307 sync_content = b"".join(list(request.stream))308 async_content = b"".join([part async for part in request.stream])309310 assert request.headers == {311 "Host": "www.example.com",
35 unmodified lines
347 request = httpx.Request(method, url, data={}, files={})348 assert isinstance(request.stream, typing.Iterable)349 assert isinstance(request.stream, typing.AsyncIterable)350351 sync_content = b"".join([part for part in request.stream])351 sync_content = b"".join(list(request.stream))352 async_content = b"".join([part async for part in request.stream])353354 assert request.headers == {"Host": "www.example.com", "Content-Length": "0"}355 assert sync_content == b""
18 unmodified lines
374 request = httpx.Request(method, url, files=files, headers=headers)375 assert isinstance(request.stream, typing.Iterable)376 assert isinstance(request.stream, typing.AsyncIterable)377378 sync_content = b"".join([part for part in request.stream])378 sync_content = b"".join(list(request.stream))379 async_content = b"".join([part async for part in request.stream])380381 assert request.headers == {382 "Host": "www.example.com",
37 unmodified lines
420 response = httpx.Response(200)421 assert isinstance(response.stream, typing.Iterable)422 assert isinstance(response.stream, typing.AsyncIterable)423424 sync_content = b"".join([part for part in response.stream])424 sync_content = b"".join(list(response.stream))425 async_content = b"".join([part async for part in response.stream])426427 assert response.headers == {}428 assert sync_content == b""
5 unmodified lines
434 response = httpx.Response(200, content=b"Hello, world!")435 assert isinstance(response.stream, typing.Iterable)436 assert isinstance(response.stream, typing.AsyncIterable)437438 sync_content = b"".join([part for part in response.stream])438 sync_content = b"".join(list(response.stream))439 async_content = b"".join([part async for part in response.stream])440441 assert response.headers == {"Content-Length": "13"}442 assert sync_content == b"Hello, world!"
9 unmodified lines
452 response = httpx.Response(200, content=hello_world())453 assert isinstance(response.stream, typing.Iterable)454 assert not isinstance(response.stream, typing.AsyncIterable)455456 content = b"".join([part for part in response.stream])456 content = b"".join(list(response.stream))457458 assert response.headers == {"Transfer-Encoding": "chunked"}459 assert content == b"Hello, world!"460461 with pytest.raises(httpx.StreamConsumed):462 [part for part in response.stream]462 list(response.stream)463464465@pytest.mark.anyio466async def test_response_aiterator_content():
20 unmodified lines