blob: 3c552cebb4226c02bd6f741c5446623a0cd6a7ed [file] [log] [blame]
Guido van Rossum8518bdc2007-06-14 00:03:37 +00001# Copyright 2007 Google, Inc. All Rights Reserved.
2# Licensed to PSF under a Contributor Agreement.
3
4"""Abstract Base Classes (ABCs) according to PEP 3119."""
5
Łukasz Langaeadd8cf2013-05-25 18:41:50 +02006
Guido van Rossum8518bdc2007-06-14 00:03:37 +00007def abstractmethod(funcobj):
8 """A decorator indicating abstract methods.
9
10 Requires that the metaclass is ABCMeta or derived from it. A
11 class that has a metaclass derived from ABCMeta cannot be
12 instantiated unless all of its abstract methods are overridden.
Walter Dörwald6e359bd2009-05-04 16:10:10 +000013 The abstract methods can be called using any of the normal
Serhiy Storchaka5c117dd2018-12-31 09:56:21 +020014 'super' call mechanisms. abstractmethod() may be used to declare
15 abstract methods for properties and descriptors.
Guido van Rossum8518bdc2007-06-14 00:03:37 +000016
17 Usage:
18
19 class C(metaclass=ABCMeta):
20 @abstractmethod
21 def my_abstract_method(self, ...):
22 ...
23 """
24 funcobj.__isabstractmethod__ = True
25 return funcobj
26
27
Benjamin Peterson45c257f2010-08-17 00:52:52 +000028class abstractclassmethod(classmethod):
Ivan Levkivskyi03e3c342018-02-18 12:41:58 +000029 """A decorator indicating abstract classmethods.
Benjamin Peterson45c257f2010-08-17 00:52:52 +000030
Miss Islington (bot)c95cdf22021-06-27 11:50:38 -070031 Deprecated, use 'classmethod' with 'abstractmethod' instead:
32
33 class C(ABC):
34 @classmethod
35 @abstractmethod
36 def my_abstract_classmethod(cls, ...):
37 ...
38
Benjamin Peterson45c257f2010-08-17 00:52:52 +000039 """
40
41 __isabstractmethod__ = True
42
43 def __init__(self, callable):
44 callable.__isabstractmethod__ = True
45 super().__init__(callable)
46
47
48class abstractstaticmethod(staticmethod):
Ivan Levkivskyi03e3c342018-02-18 12:41:58 +000049 """A decorator indicating abstract staticmethods.
Benjamin Peterson45c257f2010-08-17 00:52:52 +000050
Miss Islington (bot)c95cdf22021-06-27 11:50:38 -070051 Deprecated, use 'staticmethod' with 'abstractmethod' instead:
52
53 class C(ABC):
54 @staticmethod
55 @abstractmethod
56 def my_abstract_staticmethod(...):
57 ...
58
Benjamin Peterson45c257f2010-08-17 00:52:52 +000059 """
60
61 __isabstractmethod__ = True
62
63 def __init__(self, callable):
64 callable.__isabstractmethod__ = True
65 super().__init__(callable)
66
67
Guido van Rossumb31339f2007-08-01 17:32:28 +000068class abstractproperty(property):
Ivan Levkivskyi03e3c342018-02-18 12:41:58 +000069 """A decorator indicating abstract properties.
Guido van Rossumb31339f2007-08-01 17:32:28 +000070
Miss Islington (bot)c95cdf22021-06-27 11:50:38 -070071 Deprecated, use 'property' with 'abstractmethod' instead:
72
73 class C(ABC):
74 @property
75 @abstractmethod
76 def my_abstract_property(self):
77 ...
78
Guido van Rossumb31339f2007-08-01 17:32:28 +000079 """
Benjamin Petersonbfebb7b2011-12-15 15:34:02 -050080
Guido van Rossumb31339f2007-08-01 17:32:28 +000081 __isabstractmethod__ = True
82
83
Ivan Levkivskyi03e3c342018-02-18 12:41:58 +000084try:
85 from _abc import (get_cache_token, _abc_init, _abc_register,
86 _abc_instancecheck, _abc_subclasscheck, _get_dump,
87 _reset_registry, _reset_caches)
88except ImportError:
89 from _py_abc import ABCMeta, get_cache_token
90 ABCMeta.__module__ = 'abc'
91else:
92 class ABCMeta(type):
93 """Metaclass for defining Abstract Base Classes (ABCs).
Guido van Rossum8518bdc2007-06-14 00:03:37 +000094
Ivan Levkivskyi03e3c342018-02-18 12:41:58 +000095 Use this metaclass to create an ABC. An ABC can be subclassed
96 directly, and then acts as a mix-in class. You can also register
97 unrelated concrete classes (even built-in classes) and unrelated
98 ABCs as 'virtual subclasses' -- these and their descendants will
99 be considered subclasses of the registering ABC by the built-in
100 issubclass() function, but the registering ABC won't show up in
101 their MRO (Method Resolution Order) nor will method
102 implementations defined by the registering ABC be callable (not
103 even via super()).
Éric Araujo6c3787c2011-02-24 18:03:10 +0000104 """
Ivan Levkivskyi03e3c342018-02-18 12:41:58 +0000105 def __new__(mcls, name, bases, namespace, **kwargs):
106 cls = super().__new__(mcls, name, bases, namespace, **kwargs)
107 _abc_init(cls)
108 return cls
Guido van Rossum8518bdc2007-06-14 00:03:37 +0000109
Ivan Levkivskyi03e3c342018-02-18 12:41:58 +0000110 def register(cls, subclass):
111 """Register a virtual subclass of an ABC.
Guido van Rossum8518bdc2007-06-14 00:03:37 +0000112
Ivan Levkivskyi03e3c342018-02-18 12:41:58 +0000113 Returns the subclass, to allow usage as a class decorator.
114 """
115 return _abc_register(cls, subclass)
Guido van Rossum8518bdc2007-06-14 00:03:37 +0000116
Ivan Levkivskyi03e3c342018-02-18 12:41:58 +0000117 def __instancecheck__(cls, instance):
118 """Override for isinstance(instance, cls)."""
119 return _abc_instancecheck(cls, instance)
120
121 def __subclasscheck__(cls, subclass):
122 """Override for issubclass(subclass, cls)."""
123 return _abc_subclasscheck(cls, subclass)
124
125 def _dump_registry(cls, file=None):
126 """Debug helper to print the ABC registry."""
127 print(f"Class: {cls.__module__}.{cls.__qualname__}", file=file)
128 print(f"Inv. counter: {get_cache_token()}", file=file)
129 (_abc_registry, _abc_cache, _abc_negative_cache,
130 _abc_negative_cache_version) = _get_dump(cls)
131 print(f"_abc_registry: {_abc_registry!r}", file=file)
132 print(f"_abc_cache: {_abc_cache!r}", file=file)
133 print(f"_abc_negative_cache: {_abc_negative_cache!r}", file=file)
134 print(f"_abc_negative_cache_version: {_abc_negative_cache_version!r}",
135 file=file)
136
137 def _abc_registry_clear(cls):
138 """Clear the registry (for debugging or testing)."""
139 _reset_registry(cls)
140
141 def _abc_caches_clear(cls):
142 """Clear the caches (for debugging or testing)."""
143 _reset_caches(cls)
Andrew Svetlovb67596d2012-12-13 19:09:33 +0200144
Łukasz Langaeadd8cf2013-05-25 18:41:50 +0200145
Ben Avrahamibef7d292020-10-06 20:40:50 +0300146def update_abstractmethods(cls):
147 """Recalculate the set of abstract methods of an abstract class.
148
149 If a class has had one of its abstract methods implemented after the
150 class was created, the method will not be considered implemented until
151 this function is called. Alternatively, if a new abstract method has been
152 added to the class, it will only be considered an abstract method of the
153 class after this function is called.
154
155 This function should be called before any use is made of the class,
156 usually in class decorators that add methods to the subject class.
157
158 Returns cls, to allow usage as a class decorator.
159
160 If cls is not an instance of ABCMeta, does nothing.
161 """
162 if not hasattr(cls, '__abstractmethods__'):
163 # We check for __abstractmethods__ here because cls might by a C
164 # implementation or a python implementation (especially during
165 # testing), and we want to handle both cases.
166 return cls
167
168 abstracts = set()
169 # Check the existing abstract methods of the parents, keep only the ones
170 # that are not implemented.
171 for scls in cls.__bases__:
172 for name in getattr(scls, '__abstractmethods__', ()):
173 value = getattr(cls, name, None)
174 if getattr(value, "__isabstractmethod__", False):
175 abstracts.add(name)
176 # Also add any other newly added abstract methods.
177 for name, value in cls.__dict__.items():
178 if getattr(value, "__isabstractmethod__", False):
179 abstracts.add(name)
180 cls.__abstractmethods__ = frozenset(abstracts)
181 return cls
182
183
Andrew Svetlovb67596d2012-12-13 19:09:33 +0200184class ABC(metaclass=ABCMeta):
185 """Helper class that provides a standard way to create an ABC using
186 inheritance.
187 """
Aaron Hall, MBAff487392017-06-06 15:34:57 -0400188 __slots__ = ()