abc.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. # Copyright 2007 Google, Inc. All Rights Reserved.
  2. # Licensed to PSF under a Contributor Agreement.
  3. """Abstract Base Classes (ABCs) according to PEP 3119."""
  4. def abstractmethod(funcobj):
  5. """A decorator indicating abstract methods.
  6. Requires that the metaclass is ABCMeta or derived from it. A
  7. class that has a metaclass derived from ABCMeta cannot be
  8. instantiated unless all of its abstract methods are overridden.
  9. The abstract methods can be called using any of the normal
  10. 'super' call mechanisms. abstractmethod() may be used to declare
  11. abstract methods for properties and descriptors.
  12. Usage:
  13. class C(metaclass=ABCMeta):
  14. @abstractmethod
  15. def my_abstract_method(self, ...):
  16. ...
  17. """
  18. funcobj.__isabstractmethod__ = True
  19. return funcobj
  20. class abstractclassmethod(classmethod):
  21. """A decorator indicating abstract classmethods.
  22. Deprecated, use 'classmethod' with 'abstractmethod' instead:
  23. class C(ABC):
  24. @classmethod
  25. @abstractmethod
  26. def my_abstract_classmethod(cls, ...):
  27. ...
  28. """
  29. __isabstractmethod__ = True
  30. def __init__(self, callable):
  31. callable.__isabstractmethod__ = True
  32. super().__init__(callable)
  33. class abstractstaticmethod(staticmethod):
  34. """A decorator indicating abstract staticmethods.
  35. Deprecated, use 'staticmethod' with 'abstractmethod' instead:
  36. class C(ABC):
  37. @staticmethod
  38. @abstractmethod
  39. def my_abstract_staticmethod(...):
  40. ...
  41. """
  42. __isabstractmethod__ = True
  43. def __init__(self, callable):
  44. callable.__isabstractmethod__ = True
  45. super().__init__(callable)
  46. class abstractproperty(property):
  47. """A decorator indicating abstract properties.
  48. Deprecated, use 'property' with 'abstractmethod' instead:
  49. class C(ABC):
  50. @property
  51. @abstractmethod
  52. def my_abstract_property(self):
  53. ...
  54. """
  55. __isabstractmethod__ = True
  56. try:
  57. from _abc import (get_cache_token, _abc_init, _abc_register,
  58. _abc_instancecheck, _abc_subclasscheck, _get_dump,
  59. _reset_registry, _reset_caches)
  60. except ImportError:
  61. from _py_abc import ABCMeta, get_cache_token
  62. ABCMeta.__module__ = 'abc'
  63. else:
  64. class ABCMeta(type):
  65. """Metaclass for defining Abstract Base Classes (ABCs).
  66. Use this metaclass to create an ABC. An ABC can be subclassed
  67. directly, and then acts as a mix-in class. You can also register
  68. unrelated concrete classes (even built-in classes) and unrelated
  69. ABCs as 'virtual subclasses' -- these and their descendants will
  70. be considered subclasses of the registering ABC by the built-in
  71. issubclass() function, but the registering ABC won't show up in
  72. their MRO (Method Resolution Order) nor will method
  73. implementations defined by the registering ABC be callable (not
  74. even via super()).
  75. """
  76. def __new__(mcls, name, bases, namespace, **kwargs):
  77. cls = super().__new__(mcls, name, bases, namespace, **kwargs)
  78. _abc_init(cls)
  79. return cls
  80. def register(cls, subclass):
  81. """Register a virtual subclass of an ABC.
  82. Returns the subclass, to allow usage as a class decorator.
  83. """
  84. return _abc_register(cls, subclass)
  85. def __instancecheck__(cls, instance):
  86. """Override for isinstance(instance, cls)."""
  87. return _abc_instancecheck(cls, instance)
  88. def __subclasscheck__(cls, subclass):
  89. """Override for issubclass(subclass, cls)."""
  90. return _abc_subclasscheck(cls, subclass)
  91. def _dump_registry(cls, file=None):
  92. """Debug helper to print the ABC registry."""
  93. print(f"Class: {cls.__module__}.{cls.__qualname__}", file=file)
  94. print(f"Inv. counter: {get_cache_token()}", file=file)
  95. (_abc_registry, _abc_cache, _abc_negative_cache,
  96. _abc_negative_cache_version) = _get_dump(cls)
  97. print(f"_abc_registry: {_abc_registry!r}", file=file)
  98. print(f"_abc_cache: {_abc_cache!r}", file=file)
  99. print(f"_abc_negative_cache: {_abc_negative_cache!r}", file=file)
  100. print(f"_abc_negative_cache_version: {_abc_negative_cache_version!r}",
  101. file=file)
  102. def _abc_registry_clear(cls):
  103. """Clear the registry (for debugging or testing)."""
  104. _reset_registry(cls)
  105. def _abc_caches_clear(cls):
  106. """Clear the caches (for debugging or testing)."""
  107. _reset_caches(cls)
  108. class ABC(metaclass=ABCMeta):
  109. """Helper class that provides a standard way to create an ABC using
  110. inheritance.
  111. """
  112. __slots__ = ()