Skip to content

Commit 1917638

Browse files
author
Roy Williams
committed
Overload signature of get to return an Optional value and to allow default to take any type to match runtime behavior.
This chage more closely matches the behavior of `get` at runtime. Users can pass whatever they want in to the default parameter and it will be returned if the key is absent. Additionally, `get` should return an `Optional` if called with only one parameter. ```python z = {'a': 22} reveal_type(z.get('b')) reveal_type(z.get('b', 22)) reveal_type(z.get('b', 'hello')) ``` Before: ```shell test_get_default.py:2: error: Revealed type is 'builtins.int*' test_get_default.py:3: error: Revealed type is 'builtins.int*' test_get_default.py:4: error: Revealed type is 'builtins.int*' test_get_default.py:4: error: Argument 2 to "get" of "dict" has incompatible type "str"; expected "int" ``` After: ```shell test_get_default.py:2: error: Revealed type is 'Union[builtins.int*, builtins.None]' test_get_default.py:3: error: Revealed type is 'builtins.int' test_get_default.py:4: error: Revealed type is 'Union[builtins.int, builtins.str*]' ```
1 parent 05c6c66 commit 1917638

File tree

4 files changed

+17
-7
lines changed

4 files changed

+17
-7
lines changed

stdlib/2/__builtin__.pyi

+4-1
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,10 @@ class dict(MutableMapping[_KT, _VT], Generic[_KT, _VT]):
538538
def has_key(self, k: _KT) -> bool: ...
539539
def clear(self) -> None: ...
540540
def copy(self) -> Dict[_KT, _VT]: ...
541-
def get(self, k: _KT, default: _VT = None) -> _VT: ...
541+
@overload
542+
def get(self, k: _KT) -> Optional[_VT]: ...
543+
@overload
544+
def get(self, k: _KT, default: _T) -> Union[_VT, _T]: ...
542545
def pop(self, k: _KT, default: _VT = ...) -> _VT: ...
543546
def popitem(self) -> Tuple[_KT, _VT]: ...
544547
def setdefault(self, k: _KT, default: _VT = ...) -> _VT: ...

stdlib/2/typing.pyi

+5-3
Original file line numberDiff line numberDiff line change
@@ -174,15 +174,17 @@ class ValuesView(MappingView, Iterable[_VT_co], Generic[_VT_co]):
174174
def __contains__(self, o: object) -> bool: ...
175175
def __iter__(self) -> Iterator[_VT_co]: ...
176176

177-
class Mapping(Sized, Iterable[_KT], Container[_KT], Generic[_KT, _VT_co]):
177+
class Mapping(Iterable[_KT], Container[_KT], Sized, Generic[_KT, _VT_co]):
178178
# TODO: We wish the key type could also be covariant, but that doesn't work,
179179
# see discussion in https: //github.com/python/typing/pull/273.
180180
@abstractmethod
181181
def __getitem__(self, k: _KT) -> _VT_co:
182182
...
183183
# Mixin methods
184-
def get(self, k: _KT, default: _VT_co = ...) -> _VT_co: # type: ignore
185-
...
184+
@overload # type: ignore
185+
def get(self, k: _KT) -> Optional[_VT_co]: ...
186+
@overload # type: ignore
187+
def get(self, k: _KT, default: _T) -> Union[_VT_co, _T]: ...
186188
def keys(self) -> list[_KT]: ...
187189
def values(self) -> list[_VT_co]: ...
188190
def items(self) -> list[Tuple[_KT, _VT_co]]: ...

stdlib/3/builtins.pyi

+4-1
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,10 @@ class dict(MutableMapping[_KT, _VT], Generic[_KT, _VT]):
556556
def __init__(self, iterable: Iterable[Tuple[_KT, _VT]], **kwargs: _VT) -> None: ...
557557
def clear(self) -> None: ...
558558
def copy(self) -> Dict[_KT, _VT]: ...
559-
def get(self, k: _KT, default: _VT = None) -> _VT: ...
559+
@overload
560+
def get(self, k: _KT) -> Optional[_VT]: ...
561+
@overload
562+
def get(self, k: _KT, default: _T) -> Union[_VT, _T]: ...
560563
def pop(self, k: _KT, default: _VT = None) -> _VT: ...
561564
def popitem(self) -> Tuple[_KT, _VT]: ...
562565
def setdefault(self, k: _KT, default: _VT = None) -> _VT: ...

stdlib/3/typing.pyi

+4-2
Original file line numberDiff line numberDiff line change
@@ -250,8 +250,10 @@ class Mapping(Iterable[_KT], Container[_KT], Sized, Generic[_KT, _VT_co]):
250250
def __getitem__(self, k: _KT) -> _VT_co:
251251
...
252252
# Mixin methods
253-
def get(self, k: _KT, default: _VT_co = ...) -> _VT_co: # type: ignore
254-
...
253+
@overload # type: ignore
254+
def get(self, k: _KT) -> Optional[_VT_co]: ...
255+
@overload # type: ignore
256+
def get(self, k: _KT, default: _T) -> Union[_VT_co, _T]: ...
255257
def items(self) -> AbstractSet[Tuple[_KT, _VT_co]]: ...
256258
def keys(self) -> AbstractSet[_KT]: ...
257259
def values(self) -> ValuesView[_VT_co]: ...

0 commit comments

Comments
 (0)