Skip to content

Type context not working for second argument of dict.get #2703

@JukkaL

Description

@JukkaL

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.

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions