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.1.1.tar.gz (4.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.1.1-py3-none-any.whl (5.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: interface_py-1.1.1.tar.gz
  • Upload date:
  • Size: 4.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.1.1.tar.gz
Algorithm Hash digest
SHA256 225391ab9621f0e7fdf5d664de66a003cfe2587269d55466cc7828755b3b330f
MD5 7d7aa70579adfc439195ec49dc8c6563
BLAKE2b-256 32efc05f4c5ffbf68a3875315da7fe3b7c2451eba4b55b7f5bef4deac98d89c3

See more details on using hashes here.

File details

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

File metadata

  • Download URL: interface_py-1.1.1-py3-none-any.whl
  • Upload date:
  • Size: 5.2 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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 004a1dda20e5b599e0ee143c875f80bd7f8358c76ec5e5980af88c0d2d3d2e5c
MD5 d248de7401a5075d9b20eeb754bd13d8
BLAKE2b-256 95404508cd05ad36dfa36a0db782243f10f387cbc9e7603466f38e42e5adbf3d

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