Skip to content

Commit

Permalink
Overload signature of get to return an Optional value and to allow …
Browse files Browse the repository at this point in the history
…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*]'
```
  • Loading branch information
Roy Williams committed Jan 12, 2017
1 parent 05c6c66 commit ccdd564
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 7 deletions.
5 changes: 4 additions & 1 deletion stdlib/2/__builtin__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,10 @@ class dict(MutableMapping[_KT, _VT], Generic[_KT, _VT]):
def has_key(self, k: _KT) -> bool: ...
def clear(self) -> None: ...
def copy(self) -> Dict[_KT, _VT]: ...
def get(self, k: _KT, default: _VT = None) -> _VT: ...
@overload
def get(self, k: _KT) -> Optional[_VT]: ...
@overload
def get(self, k: _KT, default: _T2) -> Union[_VT, _T2]: ...
def pop(self, k: _KT, default: _VT = ...) -> _VT: ...
def popitem(self) -> Tuple[_KT, _VT]: ...
def setdefault(self, k: _KT, default: _VT = ...) -> _VT: ...
Expand Down
8 changes: 5 additions & 3 deletions stdlib/2/typing.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -174,15 +174,17 @@ class ValuesView(MappingView, Iterable[_VT_co], Generic[_VT_co]):
def __contains__(self, o: object) -> bool: ...
def __iter__(self) -> Iterator[_VT_co]: ...

class Mapping(Sized, Iterable[_KT], Container[_KT], Generic[_KT, _VT_co]):
class Mapping(Iterable[_KT], Container[_KT], Sized, Generic[_KT, _VT_co]):
# TODO: We wish the key type could also be covariant, but that doesn't work,
# see discussion in https: //github.com/python/typing/pull/273.
@abstractmethod
def __getitem__(self, k: _KT) -> _VT_co:
...
# Mixin methods
def get(self, k: _KT, default: _VT_co = ...) -> _VT_co: # type: ignore
...
@overload # type: ignore
def get(self, k: _KT) -> Optional[_VT_co]: ...
@overload # type: ignore
def get(self, k: _KT, default: _VT) -> Union[_VT_co, _VT]: ...
def keys(self) -> list[_KT]: ...
def values(self) -> list[_VT_co]: ...
def items(self) -> list[Tuple[_KT, _VT_co]]: ...
Expand Down
5 changes: 4 additions & 1 deletion stdlib/3/builtins.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,10 @@ class dict(MutableMapping[_KT, _VT], Generic[_KT, _VT]):
def __init__(self, iterable: Iterable[Tuple[_KT, _VT]], **kwargs: _VT) -> None: ...
def clear(self) -> None: ...
def copy(self) -> Dict[_KT, _VT]: ...
def get(self, k: _KT, default: _VT = None) -> _VT: ...
@overload
def get(self, k: _KT) -> Optional[_VT]: ...
@overload
def get(self, k: _KT, default: _T2) -> Union[_VT, _T2]: ...
def pop(self, k: _KT, default: _VT = None) -> _VT: ...
def popitem(self) -> Tuple[_KT, _VT]: ...
def setdefault(self, k: _KT, default: _VT = None) -> _VT: ...
Expand Down
6 changes: 4 additions & 2 deletions stdlib/3/typing.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,10 @@ class Mapping(Iterable[_KT], Container[_KT], Sized, Generic[_KT, _VT_co]):
def __getitem__(self, k: _KT) -> _VT_co:
...
# Mixin methods
def get(self, k: _KT, default: _VT_co = ...) -> _VT_co: # type: ignore
...
@overload # type: ignore
def get(self, k: _KT) -> Optional[_VT_co]: ...
@overload # type: ignore
def get(self, k: _KT, default: _VT) -> Union[_VT_co, _VT]: ...
def items(self) -> AbstractSet[Tuple[_KT, _VT_co]]: ...
def keys(self) -> AbstractSet[_KT]: ...
def values(self) -> ValuesView[_VT_co]: ...
Expand Down

0 comments on commit ccdd564

Please sign in to comment.