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, properties, and fields, including optional enforcement of getter/setter properties.


Features

  • Define interfaces using a decorator or base class.
  • Enforce that concrete classes implement all interface methods, fields, and properties.
  • Support for fields with three declaration styles:
    1. With annotation only → x: int
    2. With annotation + ...y: float = ...
    3. Without annotation + ...z = ...
  • 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, InterfaceBase

@interface
class HumanInterface(InterfaceBase):
    # field definitions
    name: str
    age: int = ...
    nickname = ...
    
    def speak(self): ...
    
    @property
    def rank(self): ...
    
    @rank.setter
    def rank(self, value): ...

Multi-level Interface Example

from interface_py import interface, concrete

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

@concrete
class Soldier(MilitaryHumanInterface):
    name: str = "John"
    age: int = 25
    nickname = "Eagle"
    
    def speak(self):
        print("Reporting for duty!")

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

    @property
    def rank(self):
        return self._rank
    
    @rank.setter
    def rank(self, value):
        self._rank = value

Field Enforcement Examples

from interface_py import interface, concrete, InterfaceBase

@interface
class ExampleInterface(InterfaceBase):
    x: int              # only annotation
    y: float = ...      # annotation with ellipsis
    z = ...             # plain ellipsis


# ✅ Correct implementation
@concrete
class GoodImpl(ExampleInterface):
    x: int = 10
    y: float = 3.14
    z = "hello"


# ❌ Incorrect implementation
@concrete
class BadImpl(ExampleInterface):
    x: str = "oops"   # wrong type (expected int)
    # y missing → TypeError
    z = ...           # not allowed to keep ellipsis

Validation

  • Instantiating a concrete class that does not implement all interface methods/fields/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, fields, 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.2.1.tar.gz (5.0 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.2.1-py3-none-any.whl (5.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: interface_py-1.2.1.tar.gz
  • Upload date:
  • Size: 5.0 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.2.1.tar.gz
Algorithm Hash digest
SHA256 6b798e1c168ab763ef86321c7ab1f33f61997040af2ca0839f18ea0a4ff60628
MD5 2ff4965d114911e3a11daba67e947b61
BLAKE2b-256 3d894fb6f8b0987f7643a654990782ad4e498a69a057e2e69394046cd3835691

See more details on using hashes here.

File details

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

File metadata

  • Download URL: interface_py-1.2.1-py3-none-any.whl
  • Upload date:
  • Size: 5.6 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.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 9a41bb7479918312548b592caab9fa05ccfb052adce92691bd32b06dc7ba62f7
MD5 ec27b66068aa45299dc43d7db9eb75dd
BLAKE2b-256 2757f25ea689981901b893c23a6f508f00a14ed9823b0542a4fdcc5fe886af31

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