Skip to main content

Go-style interfaces for Python

Project description

Go-style interfaces for Python

Status PyPI - Python Version PyPI - License Read the Docs Test Coverage Code Quality

Interfaces in the Go programming language are a bit different than those found in Java or C++, as they are implicit. This means that there is no explicit “implements” relationship between the interface definition and an implementation of the defined interface. A type implements an interface by implementing all of the methods defined. When we wish to define an interface in Python, we typically use abstract base classes to define them because we can enforce implementation of methods. This requires us to use inheritance, which couples the interface and the implementation.

This package emulates Go-style interfaces by creating an Interface metaclass that can be used to construct Python classes that override issubclass to test whether a class implements the methods of the interface class, rather than whether it inherits from the interface class.

This is a tiny package that emulates on of my favorite features of Go.

Installation

pip install pygopher-interfaces
# or:
# pipenv install pygopher-interfaces
# poetry add pygopher-interfaces

Usage

To create an interface class, use the Interface metaclass.

from pygopher.interfaces import Interface


class RepositoryInterface(metaclass=Interface):
    def get(account_id: int) -> Account:
        raise NotImplementedError

    def add(account: Account):
        raise NotImplementedError


class MysqlRepository:
    def get(account_id: int) -> Account:
        ...

    def add(account: Account):
        ...


>>> issubclass(MysqlRepository, RepositoryInterface)
True

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

pygopher_interfaces-0.1.3.tar.gz (8.5 kB view details)

Uploaded Source

Built Distribution

pygopher_interfaces-0.1.3-py3-none-any.whl (4.0 kB view details)

Uploaded Python 3

File details

Details for the file pygopher_interfaces-0.1.3.tar.gz.

File metadata

  • Download URL: pygopher_interfaces-0.1.3.tar.gz
  • Upload date:
  • Size: 8.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.11.1

File hashes

Hashes for pygopher_interfaces-0.1.3.tar.gz
Algorithm Hash digest
SHA256 868e1f5477765a22bf385327674fc9ebb5ac8d58cc3f812297f57e287f7c4114
MD5 0f50e8269997a434f7ad58b392a7e6ec
BLAKE2b-256 07be7eadfadcac104c4863f3749cb418ecf724261bc38432480e40bd7a2a400e

See more details on using hashes here.

File details

Details for the file pygopher_interfaces-0.1.3-py3-none-any.whl.

File metadata

File hashes

Hashes for pygopher_interfaces-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 31037b8b75196a8320c87b28227be5173a7dfa87668166e856e679cd6be84b7c
MD5 bebb999c1e8ba667071439e3e4527394
BLAKE2b-256 f638b2538b2dfff805e591060d6d2f025b0118953ae7f5dbd0b8e3390471b0fc

See more details on using hashes here.

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