Skip to main content
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)

Release files for ipromise 1.9

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ipromise 1.9
File Size Uploaded
ipromise-1.9.tar.gz 7.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for ipromise 1.9
File Interpreter ABI Platform
ipromise-1.9-py3-none-any.whl Python 3 none any Details

Total release size: 18.0 kB

Release files / ipromise-1.9.tar.gz

Download URL ipromise-1.9.tar.gz
Size 7.7 kB
Tags Source
SHA-256 checksum
How to use checksums
7a58426ad1c5bdab781dbe862aec657c1088e057ee16045b9bf13b94667ad39f
BLAKE2b-256 checksum
How to use checksums
13108002c2ab201fe5c25b7586c3bb6a7313e58495c93c34173120b6326f0423
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.0.5 CPython/3.8.1 Darwin/19.4.0

Release files / ipromise-1.9-py3-none-any.whl

Download URL ipromise-1.9-py3-none-any.whl
Size 10.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
96392914b439b3155072f02167dd7a29f6c00b286316d098c6c4c3cb5fea686a
BLAKE2b-256 checksum
How to use checksums
96500442965650596f308498da1e14ebcf53e0ebebcb4fbc02159ba9f48cc703
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.0.5 CPython/3.8.1 Darwin/19.4.0

Release history Release notifications | RSS feed

This release

1.9 This release

2 release files

1.8

2 release files

1.7

2 release files

1.6

1 release file

1.5

2 release files

1.4

1 release file

1.3

1 release file

1.2

2 release files

1.1

2 release files

1.0

2 release files

0.75

1 release file

0.65

1 release file

0.9

3 release files

0.7

1 release file

0.6

1 release file

0.5

1 release file

0.4

1 release file

0.3

1 release file

0.2

1 release file

0.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page