You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This happens with other invariant containers as well, such as lists. I just encountered an instance where mypy couldn't infer the type of a {} function argument when calling string.Template.substitute, since the type of the argument in the signature is a union.
from typing import *
def get_empty_list_str() -> List[str]:
return []
def get_empty_list_int() -> List[int]:
return []
foo: Union[List[str], List[int]] = get_empty_list_str() # fine
bar: Union[List[str], List[int]] = get_empty_list_int() # fine
baz: Union[List[str], List[int]] = [] # error
i.e., create a function to create an empty container with a defined type.
You can solve the original issue like this (note, it doesn't matter what the type of your constructor function is as long as it is one of the Union types):
This false-positive is also there with unions of lists when assigning an empty list (e.g. var: Union[list[int], list[str]] = [].
The issue is a few years old at this point but the case is not obscure enough that it would only occur in very few codebases. What's the status on this?
Reacted by Govind Rai
Reacted by Kelly Brazil, nss-csis, Wannes Rombouts, James Lamb and Govind Rai
Thanks @JosuaKrause for the work around. This actually can be done very concisely in newer python versions by using list[str](), dict[str, int](), etc. as constructors for a typed empty iterators:
Activity
gvanrossum commentedon Feb 22, 2019
I’m sure the cause is the union, not the tuple.
[-]No type inference for dicts in tuples[/-][+]No type inference for dicts in union[/+]ikonst commentedon Feb 22, 2019
@gvanrossum thanks, you're right - corrected the issue title and the sample code
oraluben commentedon Feb 25, 2019
@ikonst you could try this to have a workaround, though the semantics has changed
ilevkivskyi commentedon Feb 25, 2019
This may be a duplicate of #2164
ikonst commentedon Feb 26, 2019
@oraluben - the actual code is more complex:
and then this works:
and this works:
but this doesn't:
JukkaL commentedon Mar 29, 2019
This happens with other invariant containers as well, such as lists. I just encountered an instance where mypy couldn't infer the type of a
{}function argument when callingstring.Template.substitute, since the type of the argument in the signature is a union.ruuda commentedon Jun 5, 2019
This also happens with dicts inside a tuple:
JosuaKrause commentedon Apr 2, 2021
You can work around this issue with:
i.e., create a function to create an empty container with a defined type.
You can solve the original issue like this (note, it doesn't matter what the type of your constructor function is as long as it is one of the Union types):
Workaround python/mypy#6463
berzi commentedon May 24, 2022
This false-positive is also there with unions of lists when assigning an empty list (e.g.
var: Union[list[int], list[str]] = [].The issue is a few years old at this point but the case is not obscure enough that it would only occur in very few codebases. What's the status on this?
klieret commentedon Oct 26, 2022
Thanks @JosuaKrause for the work around. This actually can be done very concisely in newer python versions by using
list[str](),dict[str, int](), etc. as constructors for a typed empty iterators:Special-case type inference of empty collections (#16122)