Skip to main content

Pythonic Interface definitions

Project description

build status

interface provides facilities for declaring interfaces and for statically asserting that classes implement those interfaces.

interface improves on Python’s abc module in two ways:

  1. Interface requirements are checked at class creation time, rather than at instance creation time. This means that interface can tell you if a class fails to meet the requirements of an interface even if you never create any instances of that class.

  2. interface requires that method signatures of interface implementations are compatible with the signatures declared in the interface. For example, the following code using abc does not produce an error:

    >>> from abc import ABCMeta, abstractmethod
    >>> class Base(metaclass=ABCMeta):
    ...     @abstractmethod
    ...     def method(self, a, b):
    ...         pass
    ...
    >>> class Implementation(MyABC):
    ...     def method(self):
    ...         return "This shouldn't work."
    ...
    >>> impl = Implementation()
    >>>

    The equivalent code using interface produces an error indicating that the signature of our implementation method is incompatible with the signature of our interface declaration:

    >>> from interface import implements, Interface
    >>> class I(Interface):
    ...     def method(self, a, b):
    ...         pass
    ...
    >>> class C(implements(I)):
    ...     def method(self):
    ...         return "This shouldn't work"
    ...
    TypeError:
    class C failed to implement interface I:
    
    The following methods were implemented but had invalid signatures:
      - method(self) != method(self, a, b)

Defining an Interface

To define an interface, simply subclass from interface.Interface and define method stubs in your class body.

from interface import Interface

class MyInterface(Interface):

    def method1(self):
        pass

    def method2(self, arg1, arg2):
        pass

Implementing an Interface

To declare that a particular class implements an interface I, pass implements(I) as a base class for your class.

from interface import implements

class MyClass(implements(MyInterface)):

    def method1(self):
        return "method1"

    def method2(self, arg1, arg2):
        return "method2"

Installation

$ pip install python-interface

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

python-interface-1.0.0.tar.gz (9.4 kB view details)

Uploaded Source

File details

Details for the file python-interface-1.0.0.tar.gz.

File metadata

File hashes

Hashes for python-interface-1.0.0.tar.gz
Algorithm Hash digest
SHA256 918decf193c1bf2c382af23d0eaa3d7a5d424d3cb4ce521fe9ab34bfd9a0f8ea
MD5 2b7964187a2bcf74eec6b38cfdde1dc8
BLAKE2b-256 3dd5d0f45a4d8b524281b3bf9094f2c4eeb57093aad4fbae45a4fcc2ffac0953

See more details on using hashes here.

Provenance

Supported by

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