Skip to main content

A Python base class that provides various decorators for specifying promises relating to inheritance.

Project description

https://badge.fury.io/py/ipromise.svg

This repository provides a Python base class, and various decorators for specifying promises relating to inheritance. It provides three inheritance patterns:

  • implementing,

  • overriding, and

  • augmenting.

Base class

Checking promises depends on inheritance from the base class AbstractBaseClass. Unlike the standard library’s similar class abc.ABCMeta, AbstractBaseClass does not bring in any metaclasses. This is thanks to Python 3.6’s PEP 487, which added __init_subclass__.

Implementing

Implementing is the pattern whereby an inheriting class’s method implements an abstract method from a base class method. It is declared using the decorators:

  • abc.abstractmethod from the standard library, and

  • implements, which indicates that a method implements an abstract method in a base class

For example:

class HasAbstractMethod(AbstractBaseClass):

    @abstractmethod
    def f(self):
        raise NotImplementedError


class ImplementsAbstractMethod(HasAbstractMethod):

    @implements(HasAbstractMethod)
    def f(self):
        return 0

Overriding

Overriding is the pattern whereby an inheriting class’s method replaces the implementation of a base class method. It is declared using the decorator overrides, which marks the overriding method.

An overriding method could call super, but does not have to:

class HasRegularMethod(AbstractBaseClass):

    def f(self):
        return 1


class OverridesRegularMethod(HasRegularMethod):

    @overrides(HasRegularMethod)
    def f(self):
        return 2

Augmenting

Augmenting is a special case of overriding whereby the inheriting class’s method not only overrides the base class method, but extends its functionality. This means that it must delegate to super in all code paths. This pattern is typical in multiple inheritance.

We hope that Python linters will be able to check for the super call.

Augmenting is declared using two decorators:

  • augments indicates that this method must call super within its definition and thus augments the behavior of the base class method, and

  • must_agugment indicates that child classes that define this method must decorate their method overriddes with augments.

For example:

class HasMustAugmentMethod(AbstractBaseClass):

    @must_augment
    def f(self):
        # must_augment prevents this behavior from being lost.
        self.times_f_called += 1
        return 0


class AugmentsMethod(HasMustAugmentMethod):

    @augments(HasMustAugmentMethod)
    def f(self, extra=0, **kwargs):
        return super().f(**kwargs) + extra


class AugmentsMethodFurther(AugmentsMethod):

    @augments(HasMustAugmentMethod)
    def f(self, **kwargs):
        print("f has been called")
        return super().f(**kwargs)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ipromise-1.9.tar.gz (7.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ipromise-1.9-py3-none-any.whl (10.2 kB view details)

Uploaded Python 3

File details

Details for the file ipromise-1.9.tar.gz.

File metadata

  • Download URL: ipromise-1.9.tar.gz
  • Upload date:
  • Size: 7.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.5 CPython/3.8.1 Darwin/19.4.0

File hashes

Hashes for ipromise-1.9.tar.gz
Algorithm Hash digest
SHA256 7a58426ad1c5bdab781dbe862aec657c1088e057ee16045b9bf13b94667ad39f
MD5 df8894619ddcfda1708d8bd838d780c9
BLAKE2b-256 13108002c2ab201fe5c25b7586c3bb6a7313e58495c93c34173120b6326f0423

See more details on using hashes here.

File details

Details for the file ipromise-1.9-py3-none-any.whl.

File metadata

  • Download URL: ipromise-1.9-py3-none-any.whl
  • Upload date:
  • Size: 10.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.5 CPython/3.8.1 Darwin/19.4.0

File hashes

Hashes for ipromise-1.9-py3-none-any.whl
Algorithm Hash digest
SHA256 96392914b439b3155072f02167dd7a29f6c00b286316d098c6c4c3cb5fea686a
MD5 bac394964c09f8c7aebc7930edab2e7f
BLAKE2b-256 96500442965650596f308498da1e14ebcf53e0ebebcb4fbc02159ba9f48cc703

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page