_abc.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import abc
  2. from collections.abc import Iterable, Mapping, MutableMapping
  3. from typing import TYPE_CHECKING, Protocol, TypeVar, Union, overload
  4. if TYPE_CHECKING:
  5. from ._multidict_py import istr
  6. else:
  7. istr = str
  8. _V = TypeVar("_V")
  9. _V_co = TypeVar("_V_co", covariant=True)
  10. _T = TypeVar("_T")
  11. class SupportsKeys(Protocol[_V_co]):
  12. def keys(self) -> Iterable[str]: ...
  13. def __getitem__(self, key: str, /) -> _V_co: ...
  14. class SupportsIKeys(Protocol[_V_co]):
  15. def keys(self) -> Iterable[istr]: ...
  16. def __getitem__(self, key: istr, /) -> _V_co: ...
  17. MDArg = Union[SupportsKeys[_V], SupportsIKeys[_V], Iterable[tuple[str, _V]], None]
  18. class MultiMapping(Mapping[str, _V_co]):
  19. @overload
  20. def getall(self, key: str) -> list[_V_co]: ...
  21. @overload
  22. def getall(self, key: str, default: _T) -> Union[list[_V_co], _T]: ...
  23. @abc.abstractmethod
  24. def getall(self, key: str, default: _T = ...) -> Union[list[_V_co], _T]:
  25. """Return all values for key."""
  26. @overload
  27. def getone(self, key: str) -> _V_co: ...
  28. @overload
  29. def getone(self, key: str, default: _T) -> Union[_V_co, _T]: ...
  30. @abc.abstractmethod
  31. def getone(self, key: str, default: _T = ...) -> Union[_V_co, _T]:
  32. """Return first value for key."""
  33. class MutableMultiMapping(MultiMapping[_V], MutableMapping[str, _V]):
  34. @abc.abstractmethod
  35. def add(self, key: str, value: _V) -> None:
  36. """Add value to list."""
  37. @abc.abstractmethod
  38. def extend(self, arg: MDArg[_V] = None, /, **kwargs: _V) -> None:
  39. """Add everything from arg and kwargs to the mapping."""
  40. @overload
  41. def popone(self, key: str) -> _V: ...
  42. @overload
  43. def popone(self, key: str, default: _T) -> Union[_V, _T]: ...
  44. @abc.abstractmethod
  45. def popone(self, key: str, default: _T = ...) -> Union[_V, _T]:
  46. """Remove specified key and return the corresponding value."""
  47. @overload
  48. def popall(self, key: str) -> list[_V]: ...
  49. @overload
  50. def popall(self, key: str, default: _T) -> Union[list[_V], _T]: ...
  51. @abc.abstractmethod
  52. def popall(self, key: str, default: _T = ...) -> Union[list[_V], _T]:
  53. """Remove all occurrences of key and return the list of corresponding values."""