Closed
Description
So everybody knows not to write
def foo(arg={'bar': 'baz'}):
So instead one usually writes something like:
def foo(arg=None):
if arg is None:
arg = {'bar', 'baz'}
Now add some types:
def func(arg=None):
# type: (Dict[str, str]) -> Dict[str,str]
reveal_type(arg) # Revealed type is 'Union[builtins.dict[builtins.str, builtins.str], builtins.None]'
if arg is None:
arg = {'foo': 'bar'}
reveal_type(arg) # Revealed type is 'builtins.dict[builtins.str, builtins.str]'
return arg
So all is good, unless you try to create an inner method:
def func(arg=None):
# type: (Dict[str, str]) -> Dict[str,str]
reveal_type(arg) # Revealed type is 'Union[builtins.dict[builtins.str, builtins.str], builtins.None]'
if arg is None:
arg = {'foo': 'bar'}
reveal_type(arg) # Revealed type is 'builtins.dict[builtins.str, builtins.str]'
def inner():
# type: () -> Dict[str, str]
reveal_type(arg) # Revealed type is 'Union[builtins.dict[builtins.str, builtins.str], builtins.None]'
return arg # Incompatible return value type (got "Optional[Dict[str, str]]", expected Dict[str, str])
return inner()
I guess there are lots of workarounds (assign to temporary variable, pull out the inner method, pass arguments into the inner) but they all require editing existing code which is always scary. Hmm. I guess the easiest thing is to just add an assert arg is not None
in the inner. That's not too intrusive, anyway thought you might want to know.