abc.py 5.4 KB

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