_abc.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import abc
  2. import sys
  3. import types
  4. from collections.abc import Mapping, MutableMapping
  5. class _TypingMeta(abc.ABCMeta):
  6. # A fake metaclass to satisfy typing deps in runtime
  7. # basically MultiMapping[str] and other generic-like type instantiations
  8. # are emulated.
  9. # Note: real type hints are provided by __init__.pyi stub file
  10. if sys.version_info >= (3, 9):
  11. def __getitem__(self, key):
  12. return types.GenericAlias(self, key)
  13. else:
  14. def __getitem__(self, key):
  15. return self
  16. class MultiMapping(Mapping, metaclass=_TypingMeta):
  17. @abc.abstractmethod
  18. def getall(self, key, default=None):
  19. raise KeyError
  20. @abc.abstractmethod
  21. def getone(self, key, default=None):
  22. raise KeyError
  23. class MutableMultiMapping(MultiMapping, MutableMapping):
  24. @abc.abstractmethod
  25. def add(self, key, value):
  26. raise NotImplementedError
  27. @abc.abstractmethod
  28. def extend(self, *args, **kwargs):
  29. raise NotImplementedError
  30. @abc.abstractmethod
  31. def popone(self, key, default=None):
  32. raise KeyError
  33. @abc.abstractmethod
  34. def popall(self, key, default=None):
  35. raise KeyError