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

Bug?: Passing unpacked Dict fails unless all func args support all dict types? #5869

Closed
neiljp opened this issue Nov 4, 2018 · 2 comments
Closed

Comments

@neiljp
Copy link

neiljp commented Nov 4, 2018

I have the following test-case inspired by real code similar to x and func.

from typing import Optional
x = dict(a=5, b=None)
y = dict(a=5, b=6)

def func(*, a: int, b: Optional[int]) -> None:
    print(a, b)
 def func2(*, a: Optional[int], b: Optional[int]) -> None:
    print(a, b)

func(**x)  # Only call to fail mypy; expect this to pass, or is extra annotation needed?
func(**y)
func2(**x)
func2(**y)

Output:

error: Argument 1 to "func" has incompatible type "**Dict[str, Optional[int]]"; expected "int"

Version 0.641, extensions 0.4.1. No explicit flags.

@ilevkivskyi
Copy link
Member

Yes, you need to annotate this as a typed dict. By default mypy assumes all keys have the same types, like Dict[str, int].

But beware there is another issue #5198 that may give you false negatives. Anyway, this is essentially a duplicate of #5382, so closing.

@neiljp
Copy link
Author

neiljp commented Nov 4, 2018

@ilevkivskyi Thanks for the pointer, using a TypedDict enables the code to pass mypy here.

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