Skip to content

Commit 6008b9d

Browse files
Roy Williamsambv
authored andcommitted
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 6008b9d

4 files changed

Lines changed: 17 additions & 7 deletions

File tree

‎stdlib/2/__builtin__.pyi‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,10 @@ class dict(MutableMapping[_KT, _VT], Generic[_KT, _VT]):
538538
defhas_key(self, k: _KT) ->bool: ...
539539
defclear(self) ->None: ...
540540
defcopy(self) ->Dict[_KT, _VT]: ...
541-
defget(self, k: _KT, default: _VT=None) ->_VT: ...
541+
@overload
542+
defget(self, k: _KT) ->Optional[_VT]: ...
543+
@overload
544+
defget(self, k: _KT, default: _T) ->Union[_VT, _T]: ...
542545
defpop(self, k: _KT, default: _VT= ...) ->_VT: ...
543546
defpopitem(self) ->Tuple[_KT, _VT]: ...
544547
defsetdefault(self, k: _KT, default: _VT= ...) ->_VT: ...

‎stdlib/2/typing.pyi‎

Lines changed: 5 additions & 3 deletions
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-
classMapping(Sized, Iterable[_KT], Container[_KT], Generic[_KT, _VT_co]):
177+
classMapping(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-
defget(self, k: _KT, default: _VT_co= ...) ->_VT_co: # type: ignore
185-
...
184+
@overload# type: ignore
185+
defget(self, k: _KT) ->Optional[_VT_co]: ...
186+
@overload# type: ignore
187+
defget(self, k: _KT, default: _T) ->Union[_VT_co, _T]: ...
186188
defkeys(self) ->list[_KT]: ...
187189
defvalues(self) ->list[_VT_co]: ...
188190
defitems(self) ->list[Tuple[_KT, _VT_co]]: ...

‎stdlib/3/builtins.pyi‎

Lines changed: 4 additions & 1 deletion
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
defclear(self) ->None: ...
558558
defcopy(self) ->Dict[_KT, _VT]: ...
559-
defget(self, k: _KT, default: _VT=None) ->_VT: ...
559+
@overload
560+
defget(self, k: _KT) ->Optional[_VT]: ...
561+
@overload
562+
defget(self, k: _KT, default: _T) ->Union[_VT, _T]: ...
560563
defpop(self, k: _KT, default: _VT=None) ->_VT: ...
561564
defpopitem(self) ->Tuple[_KT, _VT]: ...
562565
defsetdefault(self, k: _KT, default: _VT=None) ->_VT: ...

‎stdlib/3/typing.pyi‎

Lines changed: 4 additions & 2 deletions
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-
defget(self, k: _KT, default: _VT_co= ...) ->_VT_co: # type: ignore
254-
...
253+
@overload# type: ignore
254+
defget(self, k: _KT) ->Optional[_VT_co]: ...
255+
@overload# type: ignore
256+
defget(self, k: _KT, default: _T) ->Union[_VT_co, _T]: ...
255257
defitems(self) ->AbstractSet[Tuple[_KT, _VT_co]]: ...
256258
defkeys(self) ->AbstractSet[_KT]: ...
257259
defvalues(self) ->ValuesView[_VT_co]: ...

0 commit comments

Comments
 (0)