2007 google inc licensed to psf under a contributor agreement abstract base classes abcs according to pep 3119 def abstractmethodfuncobj funcobj isabstractmethod true return funcobj class abstractclassmethodclassmethod isabstractmethod true def initself callable callable isabstractmethod true super initcallable class abstractstaticmethodstaticmethod isabstractmethod true def initself callable callable isabstractmethod true super initcallable class abstractpropertyproperty isabstractmethod true try from abc import getcachetoken abcinit abcregister abcinstancecheck abcsubclasscheck getdump resetregistry resetcaches except importerror from pyabc import abcmeta getcachetoken abcmeta module abc else class abcmetatype def newmcls name bases namespace kwargs cls super newmcls name bases namespace kwargs abcinitcls return cls def registercls subclass return abcregistercls subclass def instancecheckcls instance override for issubclasssubclass cls return abcsubclasscheckcls subclass def dumpregistrycls filenone clear the registry for debugging or testing resetregistrycls def abccachesclearcls 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 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 check the existing abstract methods of the parents keep only the ones that are not implemented also add any other newly added abstract methods helper class that provides a standard way to create an abc using inheritance 2007 google inc licensed to psf under a contributor agreement abstract base classes abcs according to pep 3119 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 a decorator indicating abstract classmethods deprecated use classmethod with abstractmethod instead class c abc classmethod abstractmethod def my_abstract_classmethod cls a decorator indicating abstract staticmethods deprecated use staticmethod with abstractmethod instead class c abc staticmethod abstractmethod def my_abstract_staticmethod a decorator indicating abstract properties deprecated use property with abstractmethod instead class c abc property abstractmethod def my_abstract_property self metaclass for defining abstract base classes abcs use this metaclass to create an abc an abc can be subclassed directly and then acts as a mix in class you can also register unrelated concrete classes even built in classes and unrelated abcs as virtual subclasses these and their descendants will be considered subclasses of the registering abc by the built in issubclass function but the registering abc won t show up in their mro method resolution order nor will method implementations defined by the registering abc be callable not even via super register a virtual subclass of an abc returns the subclass to allow usage as a class decorator override for isinstance instance cls override for issubclass subclass cls debug helper to print the abc registry clear the registry for debugging or testing clear the caches for debugging or testing 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 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 check the existing abstract methods of the parents keep only the ones that are not implemented also add any other newly added abstract methods helper class that provides a standard way to create an abc using inheritance
def abstractmethod(funcobj):
    
    funcobj.__isabstractmethod__ = True
    return funcobj


class abstractclassmethod(classmethod):
    

    __isabstractmethod__ = True

    def __init__(self, callable):
        callable.__isabstractmethod__ = True
        super().__init__(callable)


class abstractstaticmethod(staticmethod):
    

    __isabstractmethod__ = True

    def __init__(self, callable):
        callable.__isabstractmethod__ = True
        super().__init__(callable)


class abstractproperty(property):
    

    __isabstractmethod__ = True


try:
    from _abc import (get_cache_token, _abc_init, _abc_register,
                      _abc_instancecheck, _abc_subclasscheck, _get_dump,
                      _reset_registry, _reset_caches)
except ImportError:
    from _py_abc import ABCMeta, get_cache_token
    ABCMeta.__module__ = 'abc'
else:
    class ABCMeta(type):
        
        def __new__(mcls, name, bases, namespace, /, **kwargs):
            cls = super().__new__(mcls, name, bases, namespace, **kwargs)
            _abc_init(cls)
            return cls

        def register(cls, subclass):
            
            return _abc_register(cls, subclass)

        def __instancecheck__(cls, instance):
            
            return _abc_instancecheck(cls, instance)

        def __subclasscheck__(cls, subclass):
            
            return _abc_subclasscheck(cls, subclass)

        def _dump_registry(cls, file=None):
            
            print(f"Class: {cls.__module__}.{cls.__qualname__}", file=file)
            print(f"Inv. counter: {get_cache_token()}", file=file)
            (_abc_registry, _abc_cache, _abc_negative_cache,
             _abc_negative_cache_version) = _get_dump(cls)
            print(f"_abc_registry: {_abc_registry!r}", file=file)
            print(f"_abc_cache: {_abc_cache!r}", file=file)
            print(f"_abc_negative_cache: {_abc_negative_cache!r}", file=file)
            print(f"_abc_negative_cache_version: {_abc_negative_cache_version!r}",
                  file=file)

        def _abc_registry_clear(cls):
            
            _reset_registry(cls)

        def _abc_caches_clear(cls):
            
            _reset_caches(cls)


def update_abstractmethods(cls):
    
    if not hasattr(cls, '__abstractmethods__'):
        
        
        
        return cls

    abstracts = set()
    
    
    for scls in cls.__bases__:
        for name in getattr(scls, '__abstractmethods__', ()):
            value = getattr(cls, name, None)
            if getattr(value, "__isabstractmethod__", False):
                abstracts.add(name)
    
    for name, value in cls.__dict__.items():
        if getattr(value, "__isabstractmethod__", False):
            abstracts.add(name)
    cls.__abstractmethods__ = frozenset(abstracts)
    return cls


class ABC(metaclass=ABCMeta):
    
    __slots__ = ()