Skip to content

Commit

Permalink
[PR #8776/11171b8d backport][3.11] Use more precise headers type (#8778)
Browse files Browse the repository at this point in the history
**This is a backport of PR #8776 as merged into master
(11171b8).**

---------

Co-authored-by: Sam Bull <git@sambull.org>
  • Loading branch information
patchback[bot] and Dreamsorcerer authored Aug 20, 2024
1 parent 351e07b commit 5ee29e7
Show file tree
Hide file tree
Showing 3 changed files with 12 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 @@ -4,8 +4,10 @@
import warnings
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 @@ -71,7 +73,7 @@ def __init__(
code: Optional[int] = None,
status: Optional[int] = None,
message: str = "",
headers: Optional[LooseHeaders] = None,
headers: Optional[MultiMapping[str]] = None,
) -> None:
self.request_info = request_info
if code is not None:
Expand Down
12 changes: 7 additions & 5 deletions tests/test_client_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from unittest import mock

import pytest
from multidict import CIMultiDict
from yarl import URL

from aiohttp import client, client_reqrep
Expand Down Expand Up @@ -44,7 +45,7 @@ def test_pickle(self) -> None:
history=(),
status=400,
message="Something wrong",
headers={},
headers=CIMultiDict(foo="bar"),
)
err.foo = "bar"
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
Expand All @@ -54,7 +55,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 @@ -66,11 +68,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 @@ -79,7 +81,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 5ee29e7

Please sign in to comment.