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
@interfacedecorator. - Enforce that concrete classes implement all interface methods, fields, and properties.
- Support for fields with three declaration styles:
- With annotation only →
x: int - With annotation +
...→y: float = ... - Without annotation +
...→z = ...
- With annotation only →
- 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
MilitaryHumanInterfaceextendsHumanInterface.Soldierimplements 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
@interfaceautomatically 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file interface_py-1.2.2.tar.gz.
File metadata
- Download URL: interface_py-1.2.2.tar.gz
- Upload date:
- Size: 5.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
200438906d11c170c3d18c54814c5125c31958a5174459fd589ce28cf19b061d
|
|
| MD5 |
96ad67238c942044fe0713828e474322
|
|
| BLAKE2b-256 |
7c3b0fa066057b32c088202f52979f1f6b01bd4572f3cccfab1d54f0414aff5a
|
File details
Details for the file interface_py-1.2.2-py3-none-any.whl.
File metadata
- Download URL: interface_py-1.2.2-py3-none-any.whl
- Upload date:
- Size: 5.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
aa3c301d82be2f172443bf3af4e2f9437c1426f7cf3d5ee5a9a90b2ac861854c
|
|
| MD5 |
c6de9f9b1e6a1b23d85be54b7651f9c9
|
|
| BLAKE2b-256 |
6c94f0e82325f7c15a53cb3f53f750600f02d29dd163baa036d8cf8a883ae9b5
|