Skip to content

Commit

Permalink
Use more precise headers type (#8776)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dreamsorcerer authored Aug 20, 2024
1 parent b35d317 commit 11171b8
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGES/8768.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Used more precise type for ``ClientResponseError.headers``, fixing some type errors when using them -- by :user:`Dreamorcerer`.
6 changes: 4 additions & 2 deletions aiohttp/client_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import asyncio
from typing import TYPE_CHECKING, Optional, Tuple, Union

from multidict import MultiMapping

from .http_parser import RawResponseMessage
from .typedefs import LooseHeaders, StrOrURL
from .typedefs import StrOrURL

try:
import ssl
Expand Down Expand Up @@ -69,7 +71,7 @@ def __init__(
*,
status: Optional[int] = None,
message: str = "",
headers: Optional[LooseHeaders] = None,
headers: Optional[MultiMapping[str]] = None,
) -> None:
self.request_info = request_info
if status is not None:
Expand Down
11 changes: 6 additions & 5 deletions tests/test_client_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def test_pickle(self) -> None:
history=(),
status=400,
message="Something wrong",
headers={},
headers=CIMultiDict(foo="bar"),
)
err.foo = "bar" # type: ignore[attr-defined]
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
Expand All @@ -53,7 +53,8 @@ def test_pickle(self) -> None:
assert err2.history == ()
assert err2.status == 400
assert err2.message == "Something wrong"
assert err2.headers == {}
# Use headers.get() to verify static type is correct.
assert err2.headers.get("foo") == "bar"
assert err2.foo == "bar"

def test_repr(self) -> None:
Expand All @@ -65,11 +66,11 @@ def test_repr(self) -> None:
history=(),
status=400,
message="Something wrong",
headers={},
headers=CIMultiDict(),
)
assert repr(err) == (
"ClientResponseError(%r, (), status=400, "
"message='Something wrong', headers={})" % (self.request_info,)
"message='Something wrong', headers=<CIMultiDict()>)" % (self.request_info,)
)

def test_str(self) -> None:
Expand All @@ -78,7 +79,7 @@ def test_str(self) -> None:
history=(),
status=400,
message="Something wrong",
headers={},
headers=CIMultiDict(),
)
assert str(err) == ("400, message='Something wrong', url='http://example.com'")

Expand Down

0 comments on commit 11171b8

Please sign in to comment.