Giter VIP home page Giter VIP logo

ipromise's Introduction

I Promise

ipromise badge

I Promise provides a Python base class, and decorators for specifying promises relating to inheritance.

It provides three inheritance patterns:

  • implementing,
  • overriding, and
  • augmenting.

Using the inheritance patterns can ensure an inheritance hierarchy is used as intended by forcing a run-time failure when not.



pip install ipromise

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

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.

From samples/implements.py :

from ipromise import AbstractBaseClass, implements
import abc

class MyInterface(AbstractBaseClass):
    @abc.abstractmethod
    def f(self):
        raise NotImplementedError("You forgot to implement f()")

class MyImplementation(MyInterface):
    @implements(MyInterface)
    def f(self):
        print("MyImplementation().f()")
        return 0

MyImplementation().f()

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 it is not required.

From samples/overrides.py :

from ipromise import AbstractBaseClass, overrides

class MyClass(AbstractBaseClass):
    def f(self):
        print("MyClass().f()")
        return 1

class MyClassButBetter(MyClass):
    @overrides(MyClass)
    def f(self):
        print("MyClassButBetter().f()")
        return 2

MyClass().f()
MyClassButBetter().f()

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_augment indicates that child classes that define this method must decorate their method overriddes with augments.

From samples/augments.py :

from ipromise import AbstractBaseClass, must_augment, augments
import abc

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

class MyClassAgumentedOnce(MyClass):
    @augments(MyClass)
    def f(self, extra=0, **kwargs):
        print("MyClassAgumentedOnce().f()")
        return super().f(**kwargs) + extra

class MyClassAgumentedOnceAgain(MyClassAgumentedOnce):
    @augments(MyClass)
    def f(self, **kwargs):
        print("MyClassAgumentedOnceAgain().f()")
        return super().f(**kwargs)

MyClassAgumentedOnce().f()
MyClassAgumentedOnceAgain().f()

Pull Requests can be submitted on github.

The poetry development environment can be started with the typical poetry commands:

poetry install
poetry shell

Tool commands should be run at the project top-level directory.

mypy ipromise test
pflake8 ipromise test
pytest -c pyproject.toml test

Only necessary for README.rst changes.

rst-lint --level info README.rst

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.