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

Parameter expansion with ** rejected when calling aiohttp.web.Request.clone #8809

Closed
rra opened this issue May 12, 2020 · 1 comment
Closed

Comments

@rra
Copy link
Contributor

rra commented May 12, 2020

I'm getting a strange mypy error when calling aiohttp.web.Request.clone with a dict of parameters expanded with **. Is there some trick or missing feature to calling functions with ** expansion of parameters that I'm missing?

~ ❯ cat foo.py 
from aiohttp import web

def foo(request: web.Request) -> web.Request:
    overrides = {"remote": "1.1.1.1", "scheme": "https"}
    return request.clone(**overrides)
~ ❯ mypy foo.py 
foo.py:5: error: Argument 1 to "clone" of "Request" has incompatible type "**Dict[str, str]"; expected "Union[Mapping[Union[str, istr], str], CIMultiDict[str], CIMultiDictProxy[str]]"
Found 1 error in 1 file (checked 1 source file)

The relevant prototype is:

    def clone(self, *, method: str=sentinel, rel_url:
              StrOrURL=sentinel, headers: LooseHeaders=sentinel,
              scheme: str=sentinel, host: str=sentinel, remote:
              str=sentinel) -> 'Request':

It looks like the messy type is coming from LooseHeaders, and indeed str is not a valid type for LooseHeaders. Is that what mypy is complaining about? If so, is there a way to write this code that mypy will be happy with? (In my actual code, the overrides are dynamically determined.)

This is with Python 3.8.3rc1 and mypy 0.770, and aiohttp 3.6.2.

@JelleZijlstra
Copy link
Member

You can do better with a TypedDict believe. Essentially the same issue was reported recently in #8772.

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

No branches or pull requests

2 participants