8000 Passing on Kwargs to Functions with Complex Types · Issue #6552 · python/mypy · GitHub
[go: up one dir, main page]

Skip to content

Passing on Kwargs to Functions with Complex Types #6552

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

Closed
rmorshea opened this issue Mar 15, 2019 · 2 comments
Closed

Passing on Kwargs to Functions with Complex Types #6552

rmorshea opened this issue Mar 15, 2019 · 2 comments

Comments

@rmorshea
Copy link
rmorshea commented Mar 15, 2019

Consider the following attempt at adding type hints to the functions downstream and upstream:

def downstream(*, a: Type1, b: Type2):
    ...

def upstream(*, c: Type3, d: Type4, **kwargs):
    downstream(**kwargs)
    ...

MyPy complains that kwargs has the type Dict[str, Any] but that the arguments a and b of downstream require Type1 and Type2 respectively.

I understand that the solution to this error is to rewrite upstream in the following way:

def upstream(*, a: Type1, b: Type2, c: Type3, d: Type4, **kwargs):
    parent(a=a, b=b)
    ...

However what if the argument list of parent is much longer, or there is function further_upstream which has its own argument list and must call upstream? Are you required to enumerate the arguments and their types from all the downstream functions? Or is there a graceful way to handle the "per-key" typing of **kwargs that I'm not seeing?

This problem is further compounded when dealing with overwritten methods in subclasses because MyPy will complain that the signature of the overwritten method is incompatible with the signature from the supertype.

@ilevkivskyi
Copy link
Member

This is essentially a duplicate of #4441 (which likely will be implemented in near future), see also python/typing#270

@rmorshea
Copy link
Author
rmorshea commented Mar 15, 2019

@ilevkivskyi using TypedDict was actually my first guess at how to solve this, however being able to specify the functions this "wraps" via decorator would be much preferred.

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
0