Skip to content

abc

Abstract Base Classes (ABCs) according to PEP 3119.

Classes:

Functions:

ABC

Helper class that provides a standard way to create an ABC using inheritance.

abstractclassmethod

abstractclassmethod(callable)

Bases: classmethod

A decorator indicating abstract classmethods.

Deprecated, use 'classmethod' with 'abstractmethod' instead:

class C(ABC):
    @classmethod
    @abstractmethod
    def my_abstract_classmethod(cls, ...):
        ...
Source code in src/sake/abc.py
46
47
48
def __init__(self, callable):
    callable.__isabstractmethod__ = True
    super().__init__(callable)

abstractproperty

Bases: property

A decorator indicating abstract properties.

Deprecated, use 'property' with 'abstractmethod' instead:

class C(ABC):
    @property
    @abstractmethod
    def my_abstract_property(self):
        ...

abstractstaticmethod

abstractstaticmethod(callable)

Bases: staticmethod

A decorator indicating abstract staticmethods.

Deprecated, use 'staticmethod' with 'abstractmethod' instead:

class C(ABC):
    @staticmethod
    @abstractmethod
    def my_abstract_staticmethod(...):
        ...
Source code in src/sake/abc.py
66
67
68
def __init__(self, callable):
    callable.__isabstractmethod__ = True
    super().__init__(callable)

abstractmethod

abstractmethod(funcobj)

A decorator indicating abstract methods.

Requires that the metaclass is ABCMeta or derived from it. A class that has a metaclass derived from ABCMeta cannot be instantiated unless all of its abstract methods are overridden. The abstract methods can be called using any of the normal 'super' call mechanisms. abstractmethod() may be used to declare abstract methods for properties and descriptors.

Usage:

class C(metaclass=ABCMeta):
    @abstractmethod
    def my_abstract_method(self, arg1, arg2, argN):
        ...
Source code in src/sake/abc.py
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
def abstractmethod(funcobj):
    """A decorator indicating abstract methods.

    Requires that the metaclass is ABCMeta or derived from it.  A
    class that has a metaclass derived from ABCMeta cannot be
    instantiated unless all of its abstract methods are overridden.
    The abstract methods can be called using any of the normal
    'super' call mechanisms.  abstractmethod() may be used to declare
    abstract methods for properties and descriptors.

    Usage:

        class C(metaclass=ABCMeta):
            @abstractmethod
            def my_abstract_method(self, arg1, arg2, argN):
                ...
    """
    funcobj.__isabstractmethod__ = True
    return funcobj

update_abstractmethods

update_abstractmethods(cls)

Recalculate the set of abstract methods of an abstract class.

If a class has had one of its abstract methods implemented after the class was created, the method will not be considered implemented until this function is called. Alternatively, if a new abstract method has been added to the class, it will only be considered an abstract method of the class after this function is called.

This function should be called before any use is made of the class, usually in class decorators that add methods to the subject class.

Returns cls, to allow usage as a class decorator.

If cls is not an instance of ABCMeta, does nothing.

Source code in src/sake/abc.py
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
def update_abstractmethods(cls):
    """Recalculate the set of abstract methods of an abstract class.

    If a class has had one of its abstract methods implemented after the
    class was created, the method will not be considered implemented until
    this function is called. Alternatively, if a new abstract method has been
    added to the class, it will only be considered an abstract method of the
    class after this function is called.

    This function should be called before any use is made of the class,
    usually in class decorators that add methods to the subject class.

    Returns cls, to allow usage as a class decorator.

    If cls is not an instance of ABCMeta, does nothing.
    """
    if not hasattr(cls, "__abstractmethods__"):
        # We check for __abstractmethods__ here because cls might by a C
        # implementation or a python implementation (especially during
        # testing), and we want to handle both cases.
        return cls

    abstracts = set()
    # Check the existing abstract methods of the parents, keep only the ones
    # that are not implemented.
    for scls in cls.__bases__:
        for name in getattr(scls, "__abstractmethods__", ()):
            value = getattr(cls, name, None)
            if getattr(value, "__isabstractmethod__", False):
                abstracts.add(name)
    # Also add any other newly added abstract methods.
    for name, value in cls.__dict__.items():
        if getattr(value, "__isabstractmethod__", False):
            abstracts.add(name)
    cls.__abstractmethods__ = frozenset(abstracts)
    return cls