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

A Python base class that provides various decorators for specifying promises relating to inheritance. It provides three inheritance patterns:

  • implementing,

  • augmenting, and

  • overriding.

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

Augmenting

Augmenting is the pattern whereby an inheriting class’s method calls a base class method. This pattern is typical in multiple inheritance whereby mixins provide additional behavior. It 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 AlsoAugmentsMethod(HasMustAugmentMethod):

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

Overriding

Overriding is the pattern whereby an inheriting class’s method calls a base class method. This pattern indicates that the base class method is hidden (at least in some cases). It is declared using the decorator overrides, which indicates that this is an overriding method. Such a 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

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.1.linux-x86_64.tar.gz (9.4 kB view details)

Uploaded Source

Built Distribution

ipromise-1.1-py3-none-any.whl (8.7 kB view details)

Uploaded Python 3

File details

Details for the file ipromise-1.1.linux-x86_64.tar.gz.

File metadata

  • Download URL: ipromise-1.1.linux-x86_64.tar.gz
  • Upload date:
  • Size: 9.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.33.0 CPython/3.7.0

File hashes

Hashes for ipromise-1.1.linux-x86_64.tar.gz
Algorithm Hash digest
SHA256 8f066e978dbc6f35ab6845f2829cb660b7b66b30ccbee1fa5a81c37a06c08206
MD5 0e3eda94cb18a640942392aa0d9d3769
BLAKE2b-256 476239e775cf883b6aff09f566fe5b5e78764f97b35d48a0ec1adf62fbe6e1f6

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ipromise-1.1-py3-none-any.whl
  • Upload date:
  • Size: 8.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.33.0 CPython/3.7.0

File hashes

Hashes for ipromise-1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 6da9148196c5b0bf82107da9f0c339a9d81c4074aaf8010dabd0a6388adc8afe
MD5 e25c1b9e60ffb4563fdb35831ae4a34c
BLAKE2b-256 b6db59bc861d9090bba8449c3332e8330440a6537c6d501ab01805b05b2e4626

See more details on using hashes here.

Supported by

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