Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-130992: Add Interface Enforcement to abc Module #130993

Conversation

programadorLhama
Copy link

Description
This PR introduces a new InterfaceMeta metaclass in the abc module, enabling strict enforcement of interface method signatures in Python.

Motivation
Python's abc module allows for defining Abstract Base Classes (ABCs) but does not enforce method signatures. This can lead to subtle runtime errors if a derived class implements a method with an incorrect signature.

This PR adds an InterfaceMeta metaclass that ensures:

  • All required methods in an interface are implemented by derived classes.
  • Method signatures match exactly (parameters and return type annotations).
  • A TypeError is raised at class definition time if any method is missing or incorrect.

Changes Introduced

  • New Metaclass: InterfaceMeta (extends ABCMeta).
  • New Base Class: Interface, allowing strict interface enforcement.

Example Usage

from abc import Interface

class MyInterface(Interface):
    def method(self, x: int) -> str:
        pass

class ValidImplementation(MyInterface):
    def method(self, x: int) -> str:
        return str(x)

class InvalidImplementation(MyInterface):
    def method(self, x):  # Missing return annotation
        return str(x)

# Raises TypeError: "Method 'method' in class 'InvalidImplementation' does not match the interface signature."
# Expected: (self, x: int) -> str
# Got: (self, x)

Issue Number
Closes gh-130992

Performance Impact
Minimal, as the verification occurs only at class definition time.

Backward Compatibility

  • Does not modify existing ABCMeta behavior.
  • Codebases using ABC remain unchanged.

Alternative Approaches Considered

  • Extending ABCMeta with optional signature enforcement.
  • Using static type checkers (mypy), which do not enforce runtime correctness.

Open Questions

  • Should this be integrated directly into ABCMeta, or remain a separate base class (Interface)?
  • Should return type enforcement be optional?

Documentation Updates

  • The abc module documentation should include an example of InterfaceMeta.

Tests Added

  • Unit tests verifying:
    • Correct enforcement of method names and signatures.
    • Raising TypeError for incorrect implementations.

Next Steps

  • Review and discussion on whether this should be included in the standard library or proposed as a PEP.

🚀 This PR enhances Python’s OOP capabilities by introducing strict interface enforcement, improving maintainability and reducing runtime errors.

Copy link

The following commit authors need to sign the Contributor License Agreement:

Click the button to sign:
CLA not signed

@bedevere-app
Copy link

bedevere-app bot commented Mar 9, 2025

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

Copy link
Member

@ZeroIntensity ZeroIntensity left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See my comment in the (now closed) issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Proposal: OOP Interface Support in Python
2 participants