Skip to content

WIP: Mapping.{get,pop} can return default type #223

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 30, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions stdlib/2.7/__builtin__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ from typing import (
Sequence, Mapping, Tuple, List, Any, Dict, Callable, Generic, Set,
AbstractSet, Sized, Reversible, SupportsInt, SupportsFloat, SupportsAbs,
SupportsRound, IO, BinaryIO, Union, AnyStr, MutableSequence, MutableMapping,
MutableSet
MutableSet, Optional,
)
from abc import abstractmethod, ABCMeta

Expand Down Expand Up @@ -534,8 +534,14 @@ 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: ...
def pop(self, k: _KT, default: _VT = ...) -> _VT: ...
@overload
def get(self, k: _KT) -> Optional[_VT]: ...
@overload
def get(self, k: _KT, default: _T) -> Union[_VT, _T]: ...
@overload
def pop(self, k: _KT) -> _VT: ...
@overload
def pop(self, k: _KT, default: _T) -> Union[_VT, _T]: ...
def popitem(self) -> Tuple[_KT, _VT]: ...
def setdefault(self, k: _KT, default: _VT = ...) -> _VT: ...
@overload
Expand Down
22 changes: 14 additions & 8 deletions stdlib/2.7/typing.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -157,17 +157,20 @@ class MutableSet(AbstractSet[_T], Generic[_T]):
def __ixor__(self, s: AbstractSet[_S]) -> MutableSet[Union[_T, _S]]: ...
def __isub__(self, s: AbstractSet[Any]) -> MutableSet[_T]: ...

class Mapping(Sized, Iterable[_KT], Container[_KT], Generic[_KT, _VT]):
class Mapping(Sized, Iterable[_KT], Container[_KT], Generic[_KT, _VT_co]):
@abstractmethod
def __getitem__(self, k: _KT) -> _VT: ...
def __getitem__(self, k: _KT) -> _VT_co: ...
# Mixin methods
def get(self, k: _KT, default: _VT = ...) -> _VT: ...
@overload
def get(self, k: _KT) -> Optional[_VT_co]: ...
@overload
def get(self, k: _KT, default: _T) -> Union[_VT_co, _T]: ...
def keys(self) -> list[_KT]: ...
def values(self) -> list[_VT]: ...
def items(self) -> list[Tuple[_KT, _VT]]: ...
def values(self) -> list[_VT_co]: ...
def items(self) -> list[Tuple[_KT, _VT_co]]: ...
def iterkeys(self) -> Iterator[_KT]: ...
def itervalues(self) -> Iterator[_VT]: ...
def iteritems(self) -> Iterator[Tuple[_KT, _VT]]: ...
def itervalues(self) -> Iterator[_VT_co]: ...
def iteritems(self) -> Iterator[Tuple[_KT, _VT_co]]: ...
def __contains__(self, o: object) -> bool: ...

class MutableMapping(Mapping[_KT, _VT], Generic[_KT, _VT]):
Expand All @@ -177,7 +180,10 @@ class MutableMapping(Mapping[_KT, _VT], Generic[_KT, _VT]):
def __delitem__(self, v: _KT) -> None: ...

def clear(self) -> None: ...
def pop(self, k: _KT, default: _VT = ...) -> _VT: ...
@overload
def pop(self, k: _KT) -> _VT: ...
@overload
def pop(self, k: _KT, default: _T) -> Union[_VT, _T]: ...
def popitem(self) -> Tuple[_KT, _VT]: ...
def setdefault(self, k: _KT, default: _VT = ...) -> _VT: ...
@overload
Expand Down
12 changes: 9 additions & 3 deletions stdlib/3/builtins.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ from typing import (
TypeVar, Iterator, Iterable, overload,
Sequence, MutableSequence, Mapping, MutableMapping, Tuple, List, Any, Dict, Callable, Generic,
Set, AbstractSet, MutableSet, Sized, Reversible, SupportsInt, SupportsFloat, SupportsBytes,
SupportsAbs, SupportsRound, IO, Union, ItemsView, KeysView, ValuesView, ByteString
SupportsAbs, SupportsRound, IO, Union, ItemsView, KeysView, ValuesView, ByteString, Optional,
)
from abc import abstractmethod, ABCMeta

Expand Down Expand Up @@ -507,8 +507,14 @@ 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: ...
def pop(self, k: _KT, default: _VT = None) -> _VT: ...
@overload
def get(self, k: _KT) -> Optional[_VT]: ...
@overload
def get(self, k: _KT, default: _T) -> Union[_VT, _T]: ...
@overload
def pop(self, k: _KT) -> _VT: ...
@overload
def pop(self, k: _KT, default: _T) -> Union[_VT, _T]: ...
def popitem(self) -> Tuple[_KT, _VT]: ...
def setdefault(self, k: _KT, default: _VT = None) -> _VT: ...
@overload
Expand Down
18 changes: 12 additions & 6 deletions stdlib/3/typing.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -215,16 +215,19 @@ class ValuesView(MappingView, Iterable[_VT_co], Generic[_VT_co]):

# TODO: ContextManager (only if contextlib.AbstractContextManager exists)

class Mapping(Iterable[_KT], Container[_KT], Sized, Generic[_KT, _VT]):
class Mapping(Iterable[_KT], Container[_KT], Sized, Generic[_KT, _VT_co]):
# TODO: Value type should be covariant, but currently we can't give a good signature for
# get if this is the case.
@abstractmethod
def __getitem__(self, k: _KT) -> _VT: ...
def __getitem__(self, k: _KT) -> _VT_co: ...
# Mixin methods
def get(self, k: _KT, default: _VT = ...) -> _VT: ...
def items(self) -> AbstractSet[Tuple[_KT, _VT]]: ...
@overload
def get(self, k: _KT) -> Optional[_VT_co]: ...
@overload
def get(self, k: _KT, default: _T) -> Union[_VT_co, _T]: ...
def items(self) -> AbstractSet[Tuple[_KT, _VT_co]]: ...
def keys(self) -> AbstractSet[_KT]: ...
def values(self) -> ValuesView[_VT]: ...
def values(self) -> ValuesView[_VT_co]: ...
def __contains__(self, o: object) -> bool: ...

class MutableMapping(Mapping[_KT, _VT], Generic[_KT, _VT]):
Expand All @@ -234,7 +237,10 @@ class MutableMapping(Mapping[_KT, _VT], Generic[_KT, _VT]):
def __delitem__(self, v: _KT) -> None: ...

def clear(self) -> None: ...
def pop(self, k: _KT, default: _VT = ...) -> _VT: ...
@overload
def pop(self, k: _KT) -> _VT: ...
@overload
def pop(self, k: _KT, default: _T) -> Union[_VT, _T]: ...
def popitem(self) -> Tuple[_KT, _VT]: ...
def setdefault(self, k: _KT, default: _VT = ...) -> _VT: ...
# 'update' used to take a Union, but using overloading is better.
Expand Down