Skip to main content

pygopher-interfaces

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

Metadata

Release files for pygopher-interfaces 0.1.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pygopher-interfaces 0.1.3
File Size Uploaded
pygopher_interfaces-0.1.3.tar.gz 8.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pygopher-interfaces 0.1.3
File Interpreter ABI Platform
pygopher_interfaces-0.1.3-py3-none-any.whl Python 3 none any Details

Total release size: 12.5 kB

Release files / pygopher_interfaces-0.1.3.tar.gz

Download URL pygopher_interfaces-0.1.3.tar.gz
Size 8.5 kB
Tags Source
SHA-256 checksum
How to use checksums
868e1f5477765a22bf385327674fc9ebb5ac8d58cc3f812297f57e287f7c4114
BLAKE2b-256 checksum
How to use checksums
07be7eadfadcac104c4863f3749cb418ecf724261bc38432480e40bd7a2a400e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.11.1

Release files / pygopher_interfaces-0.1.3-py3-none-any.whl

Download URL pygopher_interfaces-0.1.3-py3-none-any.whl
Size 4.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
31037b8b75196a8320c87b28227be5173a7dfa87668166e856e679cd6be84b7c
BLAKE2b-256 checksum
How to use checksums
f638b2538b2dfff805e591060d6d2f025b0118953ae7f5dbd0b8e3390471b0fc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.11.1

Release history Release notifications | RSS feed

This release

0.1.3 This release

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page