Skip to main content

A package to define interface in python

Project description

interface-py

interface-py is a lightweight Python package for defining interfaces and concrete implementations with enforced contracts. It ensures that concrete classes implement all required methods and properties, including optional enforcement of getter/setter properties.


Features

  • Define interfaces using a decorator or base class.
  • Enforce that concrete classes implement all interface methods.
  • Enforce getter and setter implementation for properties.
  • Supports multi-level interface hierarchies.
  • Prevents runtime errors from missing implementations.
  • Works alongside Python's built-in ABCs.

Installation

pip install interface-py

Usage

Defining an Interface

from interface_py import interface

@interface
class HumanInterface:
    def speak(self):
        ...
    
    @property
    def name(self):
        ...
    
    @name.setter
    def name(self, value):
        ...

Multi-level Interface Example

@interface
class MilitaryHumanInterface(HumanInterface):
    def march(self):
        ...

@concrete
class Soldier(MilitaryHumanInterface):
    def speak(self):
        print("Reporting for duty!")

    def march(self):
        print("Marching!")

    @property
    def name(self):
        return self._name
    
    @name.setter
    def name(self, value):
        self._name = value
  • MilitaryHumanInterface extends HumanInterface.
  • Soldier implements all required methods and properties from both HumanInterface and MilitaryHumanInterface.

Validation

  • Trying to instantiate a concrete class that does not implement all interface methods/properties raises a TypeError.
  • Ensures consistent interface contracts across your project.

Why Use interface-py?

  • Provides contract enforcement in dynamically typed Python.
  • Helps structure large codebases with clear interface and implementation separation.
  • Avoids runtime errors from missing methods or properties.
  • Enhances code readability, maintainability, and Pythonic design.

License

MIT License

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

interface_py-1.0.0.tar.gz (3.9 kB view details)

Uploaded Source

Built Distribution

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

interface_py-1.0.0-py3-none-any.whl (4.5 kB view details)

Uploaded Python 3

File details

Details for the file interface_py-1.0.0.tar.gz.

File metadata

  • Download URL: interface_py-1.0.0.tar.gz
  • Upload date:
  • Size: 3.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for interface_py-1.0.0.tar.gz
Algorithm Hash digest
SHA256 6e78052333366892955406147074101a53f480dbb2091c3306058d7e900df192
MD5 df91ea9958f77876f326e45a89b3becc
BLAKE2b-256 ed28671d6a0f934ceb6101aea6543f49d2e2a92aed2166139ab7ef17f479e502

See more details on using hashes here.

File details

Details for the file interface_py-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: interface_py-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 4.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for interface_py-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 104832ee86787791f00b73cef20c566bc7233a54f26ce63e938d6a397f0584fd
MD5 adc6da22fb9efa34bd139b1e3a96a877
BLAKE2b-256 8fd1b4daf6edd38bca2f646c5a3a7f58142a7e5984c6c7a3b563e2bc60707afa

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