Skip to main content

Interface in POO with Python

Project description

pyinterfaces

A lightweight Python compiler extension that brings clean, Java-style interface syntax to Python using native braces {}.

PyPI version Python Versions


📐 Why pyinterfaces? (Software Engineering Perspective)

In modern Software Engineering, separating definition from implementation is a core architectural principle. Following SOLID principles—specifically the Interface Segregation Principle (ISP) and the Dependency Inversion Principle (DIP)—software components should depend on abstractions (interfaces), not on concrete logic.

While standard Python uses abc.ABC and @abstractmethod to enforce contracts, the syntax can often look cluttered, repetitive, and conceptually muddy (as Python abstract classes can accidentally mix actual logic with abstract models).

pyinterfaces bridges this gap by enforcing Pure Interfaces. It allows software engineers to declare strict structural contracts using the familiar, elegant, and universally understood Java/C# layout, keeping your architecture clean and highly readable.


✨ Features

  • Java-Style Syntax: Declare structures using Interface Name { ... }.
  • Pure Abstraction: No mixed implementation code allowed inside the contract block.
  • Runtime Enforcement: Automatically triggers native Python abc validations under the hood.
  • Zero Overhead: Translated seamlessly during file tokenization using custom stream decoding.

🚀 Installation

You can install pyinterfaces via pip or add it to your project using poetry:

pip install pyinterfaces

Or with Poetry:

poetry add pyinterfaces

💻 Usage

To enable the custom Java-like syntax parsing, you must include the magic encoding comment # -*- coding: java_interface -*- at the very first line of your Python script.

Here is a standard example of designing a decoupled Payment System:

# -*- coding: java_interface -*-
import pyinterfaces

# 1. Define the pure architectural contract
Interface PaymentProcessor {
    process_payment(self, amount: float) -> bool
    refund_payment(self, transaction_id: str) -> None
}

# 2. Implement the contract in standard Python classes
class PixProcessor(PaymentProcessor):
    def process_payment(self, amount: float) -> bool:
        print(f"Processing R\${amount} instantly via Pix.")
        return True

    def refund_payment(self, transaction_id: str) -> None:
        print(f"Refunding transaction {transaction_id}.")

# 3. Compile-time / Runtime validation
class BrokenProcessor(PaymentProcessor):
    def process_payment(self, amount: float) -> bool:
        return True
    # ERROR! Missing 'refund_payment' method implementation.

If a developer attempts to instantiate BrokenProcessor, Python will instantly raise a TypeError, stating that the class failed to implement the strict contract requirements defined by your interface.


🛠️ How it works under the hood

pyinterfaces hooks directly into Python's native codecs registry. When the interpretator reads the # -*- coding: java_interface -*- header, it streams your file through our custom pre-processor, safely transforming the custom block syntax into fully valid standard Python abc.ABC structures before the code even executes.


👤 Author

🤝 Acknowledgments

Special thanks to my friend Leandro Reginaldo for collaborating on the original concept, brainstorming the architectural ideas, and helping to shape the vision of pyinterfaces. This project wouldn't be the same without your insights!

📄 License

This project is licensed under the 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

pyinterfaces-0.2.0.tar.gz (4.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyinterfaces-0.2.0-py3-none-any.whl (5.6 kB view details)

Uploaded Python 3

File details

Details for the file pyinterfaces-0.2.0.tar.gz.

File metadata

  • Download URL: pyinterfaces-0.2.0.tar.gz
  • Upload date:
  • Size: 4.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.4.1 CPython/3.14.6 Windows/11

File hashes

Hashes for pyinterfaces-0.2.0.tar.gz
Algorithm Hash digest
SHA256 fbedd69ebea73fc6632aaa038fa7ad3f64a458ad7868b0cbc69497f8eb2211e8
MD5 5dba41e1846d05d0d83c01d585f7e9ad
BLAKE2b-256 3ad33d0f584d5c7486a2f2fdfc3455a8c1b5e291dcf6d4dd48b81b1de4e1e2b6

See more details on using hashes here.

File details

Details for the file pyinterfaces-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: pyinterfaces-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 5.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.4.1 CPython/3.14.6 Windows/11

File hashes

Hashes for pyinterfaces-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7c964cc4527d1d214756ef2074486139455aa4208cae6c06449f510f7e7b750f
MD5 48333d7a43a17231cd34c4a54b5f760f
BLAKE2b-256 2dab6975ea5d03136d17374565237bc081131b79ea8f9828f2e2a3365e2c5246

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