[typing] mypy errors when trying to extract headers when type is LooseHeaders #8768
Closed
Description
Describe the bug
When trying to read the headers from aiohttp.ClientResponseError mypy complains,
error: No overload variant of "get" of "Mapping" matches argument type "str" [call-overload]
note: Possible overload variants:
note: def get(self, istr, /) -> str | None
note: def [_T] get(self, istr, /, default: str | _T) -> str | _T
Found 1 error in 1 file (checked 1 source file)To Reproduce
test.py
from collections.abc import Iterable, Mapping
from typing import Optional, Tuple, Union
from aiohttp.typedefs import LooseHeaders
from multidict import CIMultiDict, CIMultiDictProxy, istr
def get_my_header(headers: LooseHeaders) -> Optional[str]:
if isinstance(headers, Mapping):
return headers.get("foo")
return next((v for k, v in headers if k == "foo"), None)❯ mypy test.py
test.py:17: error: No overload variant of "get" of "Mapping" matches argument type "str" [call-overload]
test.py:17: note: Possible overload variants:
test.py:17: note: def get(self, istr, /) -> str | None
test.py:17: note: def [_T] get(self, istr, /, default: str | _T) -> str | _T
Found 1 error in 1 file (checked 1 source file)
Expected behavior
headers.get() should not this throw type error.
Logs/tracebacks
N/APython Version
$ python --version
Python 3.10.8aiohttp Version
$ python -m pip show aiohttp
Name: aiohttp
Version: 3.10.3
Summary: Async http client/server framework (asyncio)
Home-page: https://github.com/aio-libs/aiohttp
Author:
Author-email:
License: Apache 2
Location: .venv/lib/python3.10/site-packages
Requires: aiohappyeyeballs, aiosignal, async-timeout, attrs, frozenlist, multidict, yarl
Required-by: aiobotocore, s3fsmultidict Version
$ python -m pip show multidict
Name: multidict
Version: 6.0.4
Summary: multidict implementation
Home-page: https://github.com/aio-libs/multidict
Author: Andrew Svetlov
Author-email: andrew.svetlov@gmail.com
License: Apache 2
Location: .venv/lib/python3.10/site-packages
Requires:
Required-by: aiohttp, yarlyarl Version
$ python -m pip show yarl
Name: yarl
Version: 1.9.3
Summary: Yet another URL library
Home-page: https://github.com/aio-libs/yarl
Author: Andrew Svetlov
Author-email: andrew.svetlov@gmail.com
License: Apache-2.0
Location: .venv/lib/python3.10/site-packages
Requires: idna, multidict
Required-by: aiohttpOS
macOS
Related component
Client
Additional context
I believe this got introduced as a part of #8620 when we split Mapping[Union[str, istr], str] into Mapping[str, str] and Mapping[istr, str]. Reverting it to the older form works fine,
test.py
from collections.abc import Iterable, Mapping
from typing import Optional, Tuple, Union
from aiohttp.typedefs import LooseHeaders
from multidict import CIMultiDict, CIMultiDictProxy, istr
LooseHeadersFixed = Union[
Mapping[Union[str, istr], str],
CIMultiDict[str],
CIMultiDictProxy[str],
Iterable[Tuple[Union[str, istr], str]],
]
def get_my_header(headers: LooseHeadersFixed) -> Optional[str]:
if isinstance(headers, Mapping):
return headers.get("foo")
return next((v for k, v in headers if k == "foo"), None)❯ mypy test.py
Success: no issues found in 1 source fileCode of Conduct
- I agree to follow the aio-libs Code of Conduct