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

add Sequence[str | int] typing in _RequestOptions.params #9144

Closed
1 task done
kiraware opened this issue Sep 16, 2024 · 2 comments
Closed
1 task done

add Sequence[str | int] typing in _RequestOptions.params #9144

kiraware opened this issue Sep 16, 2024 · 2 comments
Labels

Comments

@kiraware
Copy link

kiraware commented Sep 16, 2024

Describe the bug

I want to send query params with session.get(). with one of the param is a list. Based on the documentation here,

the library support nested lists ({'key': ['value1', 'value2']}) alternative as well.

So i try to sent with that. And it works. The problem is with the typing, mypy complaining about incompatible type

image

Output

{
  "args": {
    "key1": "value1",
    "key2": [
      "value2",
      "22"
    ]
  },
  "headers": {
    "Accept": "*/*",
    "Accept-Encoding": "gzip, deflate",
    "Host": "httpbin.org",
    "User-Agent": "Python/3.12 aiohttp/3.10.5",
    "X-Amzn-Trace-Id": "Root=1-66e7b68c-13c127ab188b8f274cca5ede"
  },
  "origin": "110.110.110.110",
  "url": "http://httpbin.org/get?key1=value1&key2=value2&key2=22"
}

I try to find the code that may cause the problem. And i found that params typing is not supporting sequence like str or int. params: Union[Mapping[str, Union[str, int]], str, None]. I think this is easy to fix, because it's only the problem with the params type forgot to add Sequence[str | int]

To Reproduce

Use mypy to analyze this code

import asyncio
from typing import Sequence

from aiohttp import ClientSession


async def main() -> None:
    async with ClientSession("http://httpbin.org") as session:
        params: dict[str, str | Sequence[str | int]] = {
            "key1": "value1",
            "key2": ["value2", 22],
        }

        async with session.get("/get", params=params) as resp:
            expect = "http://httpbin.org/get?key1=value1&key2=value2&key2=22"
            assert str(resp.url) == expect
            print(await resp.text())


asyncio.run(main())

Expected behavior

mypy is not complaining about incompatible type

Logs/tracebacks

Argument "params" to "get" of "ClientSession" has incompatible type "dict[str, str | Sequence[str | int]]"; expected "Mapping[str, str | int] | str | None"Mypyarg-type
(variable) params: dict[str, str | Sequence[str | int]]

Python Version

Python 3.12.1

aiohttp Version

name         : aiohttp
version      : 3.10.5
description  : Async http client/server framework (asyncio)

multidict Version

name         : multidict
version      : 6.0.5
description  : multidict implementation

yarl Version

name         : yarl
version      : 1.9.11
description  : Yet another URL library

OS

Windows

Related component

Client

Additional context

No response

Code of Conduct

  • I agree to follow the aio-libs Code of Conduct
@kiraware kiraware added the bug label Sep 16, 2024
@Dreamsorcerer
Copy link
Member

#8564

@kiraware
Copy link
Author

Ah sorry, i dont find it. Hope it released soon. Thanks for your response

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

No branches or pull requests

2 participants