mirror of
https://github.com/davidhalter/typeshed.git
synced 2025-12-08 21:14:48 +08:00
Without this change, mypy can't infer proper types for cases like `d.get(k, [])` where it needs type context to infer the type of `[]`. We add the value type to the second argument to `get` using union types, and this provides the context. This doesn't affect the effective signature of `get`, other than providing the type context for mypy. Also removed some related redundant method definitions where we can just inherit the base class definition. This makes it easier to keep the method signatures consistent. Note that this requires a few mypy PRs before mypy will be able to use this effectively: * https://github.com/python/mypy/pull/2718 * https://github.com/python/mypy/pull/2715
43 lines
1.5 KiB
Python
43 lines
1.5 KiB
Python
from typing import (Any, Container, Dict, Generic, Iterable, Iterator, List,
|
|
Mapping, Optional, Sized, Tuple, TypeVar, Union, overload)
|
|
|
|
_KT = TypeVar('_KT')
|
|
_VT = TypeVar('_VT')
|
|
_T = TypeVar('_T')
|
|
|
|
class UserDict(Dict[_KT, _VT], Generic[_KT, _VT]):
|
|
data = ... # type: Mapping[_KT, _VT]
|
|
|
|
def __init__(self, initialdata: Mapping[_KT, _VT] = ...) -> None: ...
|
|
|
|
# TODO: __iter__ is not available for UserDict
|
|
|
|
class IterableUserDict(UserDict[_KT, _VT], Generic[_KT, _VT]):
|
|
...
|
|
|
|
class DictMixin(Iterable[_KT], Container[_KT], Sized, Generic[_KT, _VT]):
|
|
def has_key(self, key: _KT) -> bool: ...
|
|
|
|
# From typing.Mapping[_KT, _VT]
|
|
# (can't inherit because of keys())
|
|
@overload
|
|
def get(self, k: _KT) -> Optional[_VT]: ...
|
|
@overload
|
|
def get(self, k: _KT, default: Union[_VT, _T]) -> Union[_VT, _T]: ...
|
|
def values(self) -> List[_VT]: ...
|
|
def items(self) -> List[Tuple[_KT, _VT]]: ...
|
|
def iterkeys(self) -> Iterator[_KT]: ...
|
|
def itervalues(self) -> Iterator[_VT]: ...
|
|
def iteritems(self) -> Iterator[Tuple[_KT, _VT]]: ...
|
|
def __contains__(self, o: Any) -> bool: ...
|
|
|
|
# From typing.MutableMapping[_KT, _VT]
|
|
def clear(self) -> None: ...
|
|
def pop(self, k: _KT, default: _VT = ...) -> _VT: ...
|
|
def popitem(self) -> Tuple[_KT, _VT]: ...
|
|
def setdefault(self, k: _KT, default: _VT = ...) -> _VT: ...
|
|
@overload
|
|
def update(self, m: Mapping[_KT, _VT], **kwargs: _VT) -> None: ...
|
|
@overload
|
|
def update(self, m: Iterable[Tuple[_KT, _VT]], **kwargs: _VT) -> None: ...
|