Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[typing] mypy errors when trying to extract headers when type is LooseHeaders #8768

Closed
1 task done
rahuliyer95 opened this issue Aug 19, 2024 · 1 comment · Fixed by #8776
Closed
1 task done

[typing] mypy errors when trying to extract headers when type is LooseHeaders #8768

rahuliyer95 opened this issue Aug 19, 2024 · 1 comment · Fixed by #8776
Labels

Comments

@rahuliyer95
Copy link

rahuliyer95 commented Aug 19, 2024

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/A

Python Version

$ python --version
Python 3.10.8

aiohttp 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, s3fs

multidict 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, yarl

yarl 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: aiohttp

OS

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 file

Code of Conduct

  • I agree to follow the aio-libs Code of Conduct
@Dreamsorcerer
Copy link
Member

LooseHeaders represents the acceptable inputs to Multidict, it shouldn't have been used for the exception.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants