-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Closed
Labels
Description
The new signature for dict.get introduced in python/typeshed@6008b9d causes wrong types to be inferred for some arguments. Here is an example that demonstrates the issue and doesn't rely on the typeshed commit:
from typing import Any, Generic, overload, Optional, TypeVar, Union
_KT = TypeVar('_KT')
_VT_co = TypeVar('_VT_co', covariant=True)
_T = TypeVar('_T')
class D(Generic[_KT, _VT_co]): # Mimics Dict
@overload
def get(self, k: _KT) -> Optional[_VT_co]: ...
@overload
def get(self, k: _KT, default: _T) -> Union[_VT_co, _T]: ...
def f(x: Any, y: D[int, D[int, Any]]) -> None:
a = y.get(x, D())
reveal_type(a) # Union[t.D[builtins.int, Any], t.D*[builtins.None, builtins.None]]
The expected type is t.D[builtins.int, Any].
Similar example using typeshed (note that the commit that introduced the problem is not yet used in mypy master):
from typing import Any, Dict
def f(x: Any, y: Dict[int, Dict[int, Any]]) -> None:
a = y.get(x, {})
reveal_type(a) # Union[builtins.dict[builtins.int, Any], builtins.dict*[builtins.None, builtins.None]]
The reason for the behavior is that mypy doesn't realize that type context should propagate from the return type to the argument somehow. Actually, it's unclear whether type context should generally work like this -- perhaps we should modify the signature of get instead.