Skip to main content

Create abstract function signatures that enforce consistent implementation signatures.

Project description

abstractsignature

This package provides a method for enforcing consistent function signatures between abstractmethod implementations. This hopes to make large libraries where many classes inherit a common interface from a single abstract definition easier to maintain.

Classes which inherit an @abstractmethod with an @abstractsignature will fail to instantiate if their signature does not equal the abstraction.

Two signatures are considered equal if all their parameters are equal (excluding the implicit self/cls parameter for instance and class methods), their positional and positional only parameters are in the same order, and they have equal return annotations. Parameters are considered equal if they have the same name, kind, default value, and annotation.

Install

Install from git:

uv add git+https://github.com/d-c-gray/abstractsignature.git

Usage

Create a class that inherits from ABCSignature, and define abstract methods. Add the @abstractsignature decorator above the @abstractmethod decorator.

from abstractsignature import abstractsignature, ABCSignature
from abc import abstractmethod

class MyInterface(ABCSignature):
    @abstractsignature
    @abstractmethod
    def my_method(self, a: int) -> int:
        return a + 1

class MyImplementation(MyInterface):
    def __init__(self):
        super().__init__()
    def my_method(self, number: int) -> int:
        return number + 1

MyImplementation()

If the implemented signature doesn't match the defined signature, you will get an error:

abstractsignature.abstractsignature.IncorrectSignatureError: implementation <bound method MyImplementation.my_method of <__main__.MyImplementation object at 0x00000135A7E1EE40>> does not match abstract definition of <class '__main__.MyInterface'>:
Implemented signature:
 (number: int) -> int
Abstract signature:
 (a: int) -> int

This pattern also works with @classmethod and @staticmethod

from abstractsignature import abstractsignature, ABCSignature
from abc import abstractmethod

class MyInterface(ABCSignature):
    @classmethod
    @abstractsignature
    @abstractmethod
    def my_class_method(cls, a: int) -> int:
        return a + 1

    @staticmethod
    @abstractsignature
    @abstractmethod
    def my_static_method(cls, a: int) -> int:
        return a + 1

This project was built from simple-modern-uv.

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

abstractsignature-0.1.0.tar.gz (31.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

abstractsignature-0.1.0-py3-none-any.whl (4.9 kB view details)

Uploaded Python 3

File details

Details for the file abstractsignature-0.1.0.tar.gz.

File metadata

  • Download URL: abstractsignature-0.1.0.tar.gz
  • Upload date:
  • Size: 31.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.9.5

File hashes

Hashes for abstractsignature-0.1.0.tar.gz
Algorithm Hash digest
SHA256 07631b6d4651e9bd637fc6d0e0e59a21a68859edbbfd6b7aa034d07efb8af0ad
MD5 f794aa2fa8ffe2d2ac722ade2b28b780
BLAKE2b-256 bd876d5130fcc4bd05a100836ace7d0b5db6146ed793d72221a60d01e48cfb9e

See more details on using hashes here.

File details

Details for the file abstractsignature-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for abstractsignature-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a12e280b8483e92cc569ffefee19db0aacaaa08bc96e72569a62f5ebd42d8ab2
MD5 295d6959095d285417c43e5234aa70f1
BLAKE2b-256 7f20df22947b4eb20c6c7f26986ec7e0a8c64a26b6e151f49031fb0dce26175f

See more details on using hashes here.

Supported by

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