abc.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. """
  24. __isabstractmethod__ = True
  25. def __init__(self, callable):
  26. callable.__isabstractmethod__ = True
  27. super().__init__(callable)
  28. class abstractstaticmethod(staticmethod):
  29. """A decorator indicating abstract staticmethods.
  30. Deprecated, use 'staticmethod' with 'abstractmethod' instead.
  31. """
  32. __isabstractmethod__ = True
  33. def __init__(self, callable):
  34. callable.__isabstractmethod__ = True
  35. super().__init__(callable)
  36. class abstractproperty(property):
  37. """A decorator indicating abstract properties.
  38. Deprecated, use 'property' with 'abstractmethod' instead.
  39. """
  40. __isabstractmethod__ = True
  41. try:
  42. from _abc import (get_cache_token, _abc_init, _abc_register,
  43. _abc_instancecheck, _abc_subclasscheck, _get_dump,
  44. _reset_registry, _reset_caches)
  45. except ImportError:
  46. from _py_abc import ABCMeta, get_cache_token
  47. ABCMeta.__module__ = 'abc'
  48. else:
  49. class ABCMeta(type):
  50. """Metaclass for defining Abstract Base Classes (ABCs).
  51. Use this metaclass to create an ABC. An ABC can be subclassed
  52. directly, and then acts as a mix-in class. You can also register
  53. unrelated concrete classes (even built-in classes) and unrelated
  54. ABCs as 'virtual subclasses' -- these and their descendants will
  55. be considered subclasses of the registering ABC by the built-in
  56. issubclass() function, but the registering ABC won't show up in
  57. their MRO (Method Resolution Order) nor will method
  58. implementations defined by the registering ABC be callable (not
  59. even via super()).
  60. """
  61. def __new__(mcls, name, bases, namespace, **kwargs):
  62. cls = super().__new__(mcls, name, bases, namespace, **kwargs)
  63. _abc_init(cls)
  64. return cls
  65. def register(cls, subclass):
  66. """Register a virtual subclass of an ABC.
  67. Returns the subclass, to allow usage as a class decorator.
  68. """
  69. return _abc_register(cls, subclass)
  70. def __instancecheck__(cls, instance):
  71. """Override for isinstance(instance, cls)."""
  72. return _abc_instancecheck(cls, instance)
  73. def __subclasscheck__(cls, subclass):
  74. """Override for issubclass(subclass, cls)."""
  75. return _abc_subclasscheck(cls, subclass)
  76. def _dump_registry(cls, file=None):
  77. """Debug helper to print the ABC registry."""
  78. print(f"Class: {cls.__module__}.{cls.__qualname__}", file=file)
  79. print(f"Inv. counter: {get_cache_token()}", file=file)
  80. (_abc_registry, _abc_cache, _abc_negative_cache,
  81. _abc_negative_cache_version) = _get_dump(cls)
  82. print(f"_abc_registry: {_abc_registry!r}", file=file)
  83. print(f"_abc_cache: {_abc_cache!r}", file=file)
  84. print(f"_abc_negative_cache: {_abc_negative_cache!r}", file=file)
  85. print(f"_abc_negative_cache_version: {_abc_negative_cache_version!r}",
  86. file=file)
  87. def _abc_registry_clear(cls):
  88. """Clear the registry (for debugging or testing)."""
  89. _reset_registry(cls)
  90. def _abc_caches_clear(cls):
  91. """Clear the caches (for debugging or testing)."""
  92. _reset_caches(cls)
  93. class ABC(metaclass=ABCMeta):
  94. """Helper class that provides a standard way to create an ABC using
  95. inheritance.
  96. """
  97. __slots__ = ()