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
Type inference incorrectly instantiates a type variable to <nothing> in the following example. It was run with mypy --python-version=3.7. Mypy version 0.720.
Observed behavior: In the function example, mypy reports an error in the call dict_value_substituter(str_id_substituter)(m, d) (that is, the second function call). It expects d to have type Dict[<nothing>, str] and its actual type is Dict[int, str]. Apparently, the type parameter of dict_value_substituter was instantiated to <nothing>.
Expected behavior: I expected the parameter to be instantiated to int. In the modified function example_fixed, the expected type is given explicitly and mypy accepts it.
fromtypingimportCallable, TypeVar, Dict, List, Mappingfromtyping_extensionsimportProtocolB=TypeVar('B')
C=TypeVar('C')
# Protocol for a substituter that finds and replaces strings in a `B`.# Given `s: Substituter[B]`, the call `s(m, x)` creates a modified copy# of `x` where each `a: str` is replaced by `m[a]`.classSubstituter(Protocol[B]):
def__call__(self, mapping: Mapping[str, str], target: B) ->B: ...
# Substituter for a single stringdefstr_id_substituter(mapping: Mapping[str, str], target: str) ->str:
returnmapping[target]
# Substituter for all values in a dictionarydefdict_value_substituter(s: Substituter[B]) ->Substituter[Dict[C, B]]:
defapply(mapping: Mapping[str, str], target: Dict[C, B]) ->Dict[C, B]:
returndict((k, s(mapping, v)) fork, vintarget.items())
returnapply# Inputs for exampled: Dict[int, str] = {1: 'a'}
m: Mapping[str, str] = {'a': 'b'}
defexample() ->Dict[int, str]:
# Wrong type inferred for return value of dict_value_substituter(...)returndict_value_substituter(str_id_substituter)(m, d)
defexample_fixed() ->Dict[int, str]:
# With explicit type annotation, it workss: Substituter[Dict[int, str]] =dict_value_substituter(str_id_substituter)
returns(m, d)
print(example())
print(example_fixed())
The text was updated successfully, but these errors were encountered:
Mypy is actually correct here. The type for dict_value_substituter() is bad (underspecified), because it has a type variable that appears only in the return type. We already have an issue for having a better error message for such cases, see #2885
Type inference incorrectly instantiates a type variable to
<nothing>
in the following example. It was run withmypy --python-version=3.7
. Mypy version 0.720.Observed behavior: In the function
example
, mypy reports an error in the calldict_value_substituter(str_id_substituter)(m, d)
(that is, the second function call). It expects d to have typeDict[<nothing>, str]
and its actual type isDict[int, str]
. Apparently, the type parameter ofdict_value_substituter
was instantiated to<nothing>
.Expected behavior: I expected the parameter to be instantiated to
int
. In the modified functionexample_fixed
, the expected type is given explicitly and mypy accepts it.The text was updated successfully, but these errors were encountered: