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

Union as type of a Dict fails, when coming from a variable #8984

Closed
ltworf opened this issue Jun 10, 2020 · 3 comments
Closed

Union as type of a Dict fails, when coming from a variable #8984

ltworf opened this issue Jun 10, 2020 · 3 comments

Comments

@ltworf
Copy link

ltworf commented Jun 10, 2020

This possibly relates to this #6463

from typing import *

def f(arg: Sequence[Dict[Union[int,str], str]]) -> None:
    ...

def q(arg: Sequence[Union[int,str]]) -> None:
    ...

q([1,2,3,'a']) # This works
f([{'a':'a'}]) # This works

l =  [{'a':'a'}]
f(l) # This does not

It seems that assigning the same value to a variable or passing it directly gets typed differently for some reason.

@JelleZijlstra
Copy link
Member

@ltworf
Copy link
Author

ltworf commented Jun 10, 2020

The type of the same thing is being inferred differently depending if it's assigned to a variable or passed as a parameter. That is what I'm reporting here.

@JelleZijlstra
Copy link
Member

That's also expected behavior. If you assign it to a variable, the type of the variable is fixed at the definition site, but if you use a literal as an argument to a function mypy uses type context to infer a different type.

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