Skip to content

Commit

Permalink
Enabling ruff C416 (#3001)
Browse files Browse the repository at this point in the history
* Enabled C416 in ruff

* Ran ruff on all files

* Ran ruff format

* Update pyproject.toml

---------

Co-authored-by: Tom Christie <[email protected]>
  • Loading branch information
jamesbraza and tomchristie authored Dec 13, 2023
1 parent 2c51edd commit 2318fd8
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 41 deletions.
2 changes: 1 addition & 1 deletion httpx/_content.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ async def __aiter__(self) -> AsyncIterator[bytes]:


def encode_content(
content: Union[str, bytes, Iterable[bytes], AsyncIterable[bytes]]
content: Union[str, bytes, Iterable[bytes], AsyncIterable[bytes]],
) -> Tuple[Dict[str, str], Union[SyncByteStream, AsyncByteStream]]:
if isinstance(content, (bytes, str)):
body = content.encode("utf-8") if isinstance(content, str) else content
Expand Down
2 changes: 1 addition & 1 deletion httpx/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ def parse_content_type_charset(content_type: str) -> typing.Optional[str]:


def obfuscate_sensitive_headers(
items: typing.Iterable[typing.Tuple[typing.AnyStr, typing.AnyStr]]
items: typing.Iterable[typing.Tuple[typing.AnyStr, typing.AnyStr]],
) -> typing.Iterator[typing.Tuple[typing.AnyStr, typing.AnyStr]]:
for k, v in items:
if to_str(k.lower()) in SENSITIVE_HEADERS:
Expand Down
2 changes: 1 addition & 1 deletion tests/client/test_redirects.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ def test_can_stream_if_no_redirect():
class ConsumeBodyTransport(httpx.MockTransport):
def handle_request(self, request: httpx.Request) -> httpx.Response:
assert isinstance(request.stream, httpx.SyncByteStream)
[_ for _ in request.stream]
list(request.stream)
return self.handler(request) # type: ignore[return-value]


Expand Down
2 changes: 1 addition & 1 deletion tests/models/test_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def test_read_and_stream_data():
request.read()
assert request.stream is not None
assert isinstance(request.stream, typing.Iterable)
content = b"".join([part for part in request.stream])
content = b"".join(list(request.stream))
assert content == request.content


Expand Down
34 changes: 17 additions & 17 deletions tests/models/test_responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -397,19 +397,19 @@ def test_iter_raw():

def test_iter_raw_with_chunksize():
response = httpx.Response(200, content=streaming_body())
parts = [part for part in response.iter_raw(chunk_size=5)]
parts = list(response.iter_raw(chunk_size=5))
assert parts == [b"Hello", b", wor", b"ld!"]

response = httpx.Response(200, content=streaming_body())
parts = [part for part in response.iter_raw(chunk_size=7)]
parts = list(response.iter_raw(chunk_size=7))
assert parts == [b"Hello, ", b"world!"]

response = httpx.Response(200, content=streaming_body())
parts = [part for part in response.iter_raw(chunk_size=13)]
parts = list(response.iter_raw(chunk_size=13))
assert parts == [b"Hello, world!"]

response = httpx.Response(200, content=streaming_body())
parts = [part for part in response.iter_raw(chunk_size=20)]
parts = list(response.iter_raw(chunk_size=20))
assert parts == [b"Hello, world!"]


Expand All @@ -422,7 +422,7 @@ def streaming_body_with_empty_chunks() -> typing.Iterator[bytes]:

response = httpx.Response(200, content=streaming_body_with_empty_chunks())

parts = [part for part in response.iter_raw()]
parts = list(response.iter_raw())
assert parts == [b"Hello, ", b"world!"]


Expand All @@ -445,7 +445,7 @@ def test_iter_raw_on_async():
)

with pytest.raises(RuntimeError):
[part for part in response.iter_raw()]
list(response.iter_raw())


def test_close_on_async():
Expand Down Expand Up @@ -538,21 +538,21 @@ def test_iter_bytes():

def test_iter_bytes_with_chunk_size():
response = httpx.Response(200, content=streaming_body())
parts = [part for part in response.iter_bytes(chunk_size=5)]
parts = list(response.iter_bytes(chunk_size=5))
assert parts == [b"Hello", b", wor", b"ld!"]

response = httpx.Response(200, content=streaming_body())
parts = [part for part in response.iter_bytes(chunk_size=13)]
parts = list(response.iter_bytes(chunk_size=13))
assert parts == [b"Hello, world!"]

response = httpx.Response(200, content=streaming_body())
parts = [part for part in response.iter_bytes(chunk_size=20)]
parts = list(response.iter_bytes(chunk_size=20))
assert parts == [b"Hello, world!"]


def test_iter_bytes_with_empty_response():
response = httpx.Response(200, content=b"")
parts = [part for part in response.iter_bytes()]
parts = list(response.iter_bytes())
assert parts == []


Expand All @@ -565,7 +565,7 @@ def streaming_body_with_empty_chunks() -> typing.Iterator[bytes]:

response = httpx.Response(200, content=streaming_body_with_empty_chunks())

parts = [part for part in response.iter_bytes()]
parts = list(response.iter_bytes())
assert parts == [b"Hello, ", b"world!"]


Expand Down Expand Up @@ -611,23 +611,23 @@ def test_iter_text():

def test_iter_text_with_chunk_size():
response = httpx.Response(200, content=b"Hello, world!")
parts = [part for part in response.iter_text(chunk_size=5)]
parts = list(response.iter_text(chunk_size=5))
assert parts == ["Hello", ", wor", "ld!"]

response = httpx.Response(200, content=b"Hello, world!!")
parts = [part for part in response.iter_text(chunk_size=7)]
parts = list(response.iter_text(chunk_size=7))
assert parts == ["Hello, ", "world!!"]

response = httpx.Response(200, content=b"Hello, world!")
parts = [part for part in response.iter_text(chunk_size=7)]
parts = list(response.iter_text(chunk_size=7))
assert parts == ["Hello, ", "world!"]

response = httpx.Response(200, content=b"Hello, world!")
parts = [part for part in response.iter_text(chunk_size=13)]
parts = list(response.iter_text(chunk_size=13))
assert parts == ["Hello, world!"]

response = httpx.Response(200, content=b"Hello, world!")
parts = [part for part in response.iter_text(chunk_size=20)]
parts = list(response.iter_text(chunk_size=20))
assert parts == ["Hello, world!"]


Expand Down Expand Up @@ -664,7 +664,7 @@ def test_iter_lines():
200,
content=b"Hello,\nworld!",
)
content = [line for line in response.iter_lines()]
content = list(response.iter_lines())
assert content == ["Hello,", "world!"]


Expand Down
40 changes: 20 additions & 20 deletions tests/test_content.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ async def test_empty_content():
assert isinstance(request.stream, httpx.SyncByteStream)
assert isinstance(request.stream, httpx.AsyncByteStream)

sync_content = b"".join([part for part in request.stream])
sync_content = b"".join(list(request.stream))
async_content = b"".join([part async for part in request.stream])

assert request.headers == {"Host": "www.example.com", "Content-Length": "0"}
Expand All @@ -29,7 +29,7 @@ async def test_bytes_content():
assert isinstance(request.stream, typing.Iterable)
assert isinstance(request.stream, typing.AsyncIterable)

sync_content = b"".join([part for part in request.stream])
sync_content = b"".join(list(request.stream))
async_content = b"".join([part async for part in request.stream])

assert request.headers == {"Host": "www.example.com", "Content-Length": "13"}
Expand All @@ -42,7 +42,7 @@ async def test_bytes_content():
assert isinstance(request.stream, typing.Iterable)
assert isinstance(request.stream, typing.AsyncIterable)

sync_content = b"".join([part for part in request.stream])
sync_content = b"".join(list(request.stream))
async_content = b"".join([part async for part in request.stream])

assert request.headers == {"Host": "www.example.com", "Content-Length": "13"}
Expand All @@ -56,7 +56,7 @@ async def test_bytesio_content():
assert isinstance(request.stream, typing.Iterable)
assert not isinstance(request.stream, typing.AsyncIterable)

content = b"".join([part for part in request.stream])
content = b"".join(list(request.stream))

assert request.headers == {"Host": "www.example.com", "Content-Length": "13"}
assert content == b"Hello, world!"
Expand Down Expand Up @@ -100,7 +100,7 @@ def hello_world() -> typing.Iterator[bytes]:
assert isinstance(request.stream, typing.Iterable)
assert not isinstance(request.stream, typing.AsyncIterable)

content = b"".join([part for part in request.stream])
content = b"".join(list(request.stream))

assert request.headers == {
"Host": "www.example.com",
Expand All @@ -109,15 +109,15 @@ def hello_world() -> typing.Iterator[bytes]:
assert content == b"Hello, world!"

with pytest.raises(httpx.StreamConsumed):
[part for part in request.stream]
list(request.stream)

# Support 'data' for compat with requests.
with pytest.warns(DeprecationWarning):
request = httpx.Request(method, url, data=hello_world()) # type: ignore
assert isinstance(request.stream, typing.Iterable)
assert not isinstance(request.stream, typing.AsyncIterable)

content = b"".join([part for part in request.stream])
content = b"".join(list(request.stream))

assert request.headers == {
"Host": "www.example.com",
Expand Down Expand Up @@ -168,7 +168,7 @@ async def test_json_content():
assert isinstance(request.stream, typing.Iterable)
assert isinstance(request.stream, typing.AsyncIterable)

sync_content = b"".join([part for part in request.stream])
sync_content = b"".join(list(request.stream))
async_content = b"".join([part async for part in request.stream])

assert request.headers == {
Expand All @@ -186,7 +186,7 @@ async def test_urlencoded_content():
assert isinstance(request.stream, typing.Iterable)
assert isinstance(request.stream, typing.AsyncIterable)

sync_content = b"".join([part for part in request.stream])
sync_content = b"".join(list(request.stream))
async_content = b"".join([part async for part in request.stream])

assert request.headers == {
Expand All @@ -204,7 +204,7 @@ async def test_urlencoded_boolean():
assert isinstance(request.stream, typing.Iterable)
assert isinstance(request.stream, typing.AsyncIterable)

sync_content = b"".join([part for part in request.stream])
sync_content = b"".join(list(request.stream))
async_content = b"".join([part async for part in request.stream])

assert request.headers == {
Expand All @@ -222,7 +222,7 @@ async def test_urlencoded_none():
assert isinstance(request.stream, typing.Iterable)
assert isinstance(request.stream, typing.AsyncIterable)

sync_content = b"".join([part for part in request.stream])
sync_content = b"".join(list(request.stream))
async_content = b"".join([part async for part in request.stream])

assert request.headers == {
Expand All @@ -240,7 +240,7 @@ async def test_urlencoded_list():
assert isinstance(request.stream, typing.Iterable)
assert isinstance(request.stream, typing.AsyncIterable)

sync_content = b"".join([part for part in request.stream])
sync_content = b"".join(list(request.stream))
async_content = b"".join([part async for part in request.stream])

assert request.headers == {
Expand All @@ -265,7 +265,7 @@ async def test_multipart_files_content():
assert isinstance(request.stream, typing.Iterable)
assert isinstance(request.stream, typing.AsyncIterable)

sync_content = b"".join([part for part in request.stream])
sync_content = b"".join(list(request.stream))
async_content = b"".join([part async for part in request.stream])

assert request.headers == {
Expand Down Expand Up @@ -304,7 +304,7 @@ async def test_multipart_data_and_files_content():
assert isinstance(request.stream, typing.Iterable)
assert isinstance(request.stream, typing.AsyncIterable)

sync_content = b"".join([part for part in request.stream])
sync_content = b"".join(list(request.stream))
async_content = b"".join([part async for part in request.stream])

assert request.headers == {
Expand Down Expand Up @@ -348,7 +348,7 @@ async def test_empty_request():
assert isinstance(request.stream, typing.Iterable)
assert isinstance(request.stream, typing.AsyncIterable)

sync_content = b"".join([part for part in request.stream])
sync_content = b"".join(list(request.stream))
async_content = b"".join([part async for part in request.stream])

assert request.headers == {"Host": "www.example.com", "Content-Length": "0"}
Expand All @@ -375,7 +375,7 @@ async def test_multipart_multiple_files_single_input_content():
assert isinstance(request.stream, typing.Iterable)
assert isinstance(request.stream, typing.AsyncIterable)

sync_content = b"".join([part for part in request.stream])
sync_content = b"".join(list(request.stream))
async_content = b"".join([part async for part in request.stream])

assert request.headers == {
Expand Down Expand Up @@ -421,7 +421,7 @@ async def test_response_empty_content():
assert isinstance(response.stream, typing.Iterable)
assert isinstance(response.stream, typing.AsyncIterable)

sync_content = b"".join([part for part in response.stream])
sync_content = b"".join(list(response.stream))
async_content = b"".join([part async for part in response.stream])

assert response.headers == {}
Expand All @@ -435,7 +435,7 @@ async def test_response_bytes_content():
assert isinstance(response.stream, typing.Iterable)
assert isinstance(response.stream, typing.AsyncIterable)

sync_content = b"".join([part for part in response.stream])
sync_content = b"".join(list(response.stream))
async_content = b"".join([part async for part in response.stream])

assert response.headers == {"Content-Length": "13"}
Expand All @@ -453,13 +453,13 @@ def hello_world() -> typing.Iterator[bytes]:
assert isinstance(response.stream, typing.Iterable)
assert not isinstance(response.stream, typing.AsyncIterable)

content = b"".join([part for part in response.stream])
content = b"".join(list(response.stream))

assert response.headers == {"Transfer-Encoding": "chunked"}
assert content == b"Hello, world!"

with pytest.raises(httpx.StreamConsumed):
[part for part in response.stream]
list(response.stream)


@pytest.mark.anyio
Expand Down

0 comments on commit 2318fd8

Please sign in to comment.