Skip to content

CGuichard/ispec

Repository files navigation

ISpec - Interface Specification

License Language Documentation Style Lint Security Stability Contributions

Pull Request · Bug Report · Feature Request

Warning

ISpec is an experimental project, and is not aimed to be used in real projects. You can successfully create interfaces with the help of abc (on which ispec is based), zope.interface, and last but not least, the best solution, the Protocol from typing.

ISpec is a project aimed to create a simple way of defining interfaces. What it does is use the abc module, make your class inherit abc.ABC, apply the abc.abstractmethod decorator, and ensure the function signature uses type hints for parameters and returns. With this, a class that implements an ISpec interface can't be instantiated if it doesn't define every function of the interface, and the interface must declare type hints to improve readability.

Table of Contents

Getting started

Installation

pip install git+https://github.com/CGuichard/ispec.git
# pip install git+https://github.com/CGuichard/ispec.git@<tag>

Usage

Simple example:

from ispec import ispec


@ispec
class MyInterface:
    def method_dummy(self, a: str) -> str:
        ...

    @staticmethod
    def static_method_dummy(b: str) -> str:
        ...

    @classmethod
    def class_method_dummy(cls, c: str) -> str:
        ...


class A(MyInterface):
    def method_dummy(self, a: str) -> str:
        print(f"method {a=}")

    @staticmethod
    def static_method_dummy(b: str) -> str:
        print(f"static method {b=}")

    @classmethod
    def class_method_dummy(cls, c: str) -> str:
        print(f"class method {c=}")


mi: MyInterface = A()

Contributing

If you want to contribute to this project or understand how it works, please check CONTRIBUTING.rst.

Any contribution is greatly appreciated.

License

Distributed under the MIT License. See LICENSE for more information.