codekingpro/portable-devtools
114k
1import abc2from collections.abc import Iterable, Mapping, MutableMapping3from typing import TYPE_CHECKING, Protocol, TypeVar, Union, overload4 5if TYPE_CHECKING:6 from ._multidict_py import istr7else:8 istr = str9 10_V = TypeVar("_V")11_V_co = TypeVar("_V_co", covariant=True)12_T = TypeVar("_T")13 14 15class SupportsKeys(Protocol[_V_co]):16 def keys(self) -> Iterable[str]: ...17 def __getitem__(self, key: str, /) -> _V_co: ...18 19 20class SupportsIKeys(Protocol[_V_co]):21 def keys(self) -> Iterable[istr]: ...22 def __getitem__(self, key: istr, /) -> _V_co: ...23 24 25MDArg = Union[SupportsKeys[_V], SupportsIKeys[_V], Iterable[tuple[str, _V]], None]26 27 28class MultiMapping(Mapping[str, _V_co]):29 @overload30 def getall(self, key: str) -> list[_V_co]: ...31 @overload32 def getall(self, key: str, default: _T) -> Union[list[_V_co], _T]: ...33 @abc.abstractmethod34 def getall(self, key: str, default: _T = ...) -> Union[list[_V_co], _T]:35 """Return all values for key."""36 37 @overload38 def getone(self, key: str) -> _V_co: ...39 @overload40 def getone(self, key: str, default: _T) -> Union[_V_co, _T]: ...41 @abc.abstractmethod42 def getone(self, key: str, default: _T = ...) -> Union[_V_co, _T]:43 """Return first value for key."""44 45 46class MutableMultiMapping(MultiMapping[_V], MutableMapping[str, _V]):47 @abc.abstractmethod48 def add(self, key: str, value: _V) -> None:49 """Add value to list."""50 51 @abc.abstractmethod52 def extend(self, arg: MDArg[_V] = None, /, **kwargs: _V) -> None:53 """Add everything from arg and kwargs to the mapping."""54 55 @abc.abstractmethod56 def merge(self, arg: MDArg[_V] = None, /, **kwargs: _V) -> None:57 """Merge into the mapping, adding non-existing keys."""58 59 @overload60 def popone(self, key: str) -> _V: ...61 @overload62 def popone(self, key: str, default: _T) -> Union[_V, _T]: ...63 @abc.abstractmethod64 def popone(self, key: str, default: _T = ...) -> Union[_V, _T]:65 """Remove specified key and return the corresponding value."""66 67 @overload68 def popall(self, key: str) -> list[_V]: ...69 @overload70 def popall(self, key: str, default: _T) -> Union[list[_V], _T]: ...71 @abc.abstractmethod72 def popall(self, key: str, default: _T = ...) -> Union[list[_V], _T]:73 """Remove all occurrences of key and return the list of corresponding values."""74 