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.1.0.tar.gz (4.6 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.1.0-py3-none-any.whl (4.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: interface_py-1.1.0.tar.gz
  • Upload date:
  • Size: 4.6 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.1.0.tar.gz
Algorithm Hash digest
SHA256 298a0b2cb9b8574f36968f4844e584e09f16133af572f8ef57213c6b8403cbdc
MD5 46a5c462362aa4eabc3d5a5e7fcc8dc0
BLAKE2b-256 1445d9bd43e096c0a61807dc7f3d5301da8824f0e932df8543ae18f4a40192bc

See more details on using hashes here.

File details

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

File metadata

  • Download URL: interface_py-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 4.9 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.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 971f1fdd5f75a61276cd09cdc6d76e7d869913c5c8bb54f0f30e54e7ab87b31b
MD5 b33e3760104ad571c19f0bb6b79cc076
BLAKE2b-256 380333c0a6113f9ea7495ee06b1d127b193347419b6e4273e61b2cf09455c06d

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