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 the @interface decorator.
  • 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

@interface
class HumanInterface:
    # 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
  • MilitaryHumanInterface extends HumanInterface.
  • Soldier implements all required methods, fields, and properties from both interfaces automatically.

Field Enforcement Examples

from interface_py import interface, concrete

@interface
class ExampleInterface:
    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.
  • The decorator @interface automatically enforces the interface behavior without requiring any base class.

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.3.tar.gz (6.2 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.3-py3-none-any.whl (6.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: interface_py-1.2.3.tar.gz
  • Upload date:
  • Size: 6.2 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.3.tar.gz
Algorithm Hash digest
SHA256 e9965ec0225fc7523186e0b202516195f8fba30012c2f7397460f2291c5c48ba
MD5 21e45a357bbf34aad6a0c740da5b0a7b
BLAKE2b-256 8f6a4c08188fed407fb6f330a2fde2a16dad628790655b869e10724a024f4df9

See more details on using hashes here.

File details

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

File metadata

  • Download URL: interface_py-1.2.3-py3-none-any.whl
  • Upload date:
  • Size: 6.3 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.3-py3-none-any.whl
Algorithm Hash digest
SHA256 3ca24f6a730d187f0bddaa6b201120ce8301224bc2b4db7e6684ecc70edf0ec1
MD5 04015bfa30eccc85e2df92613a776575
BLAKE2b-256 80b45dfc730d37de7b44fba5d2c4fef3f1044a49c9cd3ed4081ea02a5f1f814d

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