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

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions Lib/abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

"""Abstract Base Classes (ABCs) according to PEP 3119."""

import inspect

def abstractmethod(funcobj):
"""A decorator indicating abstract methods.
Expand Down Expand Up @@ -186,3 +187,35 @@ class ABC(metaclass=ABCMeta):
inheritance.
"""
__slots__ = ()


class InterfaceMeta(ABCMeta):
"""Metaclass to force implementation of methods in derived classes with the same signature."""

def __new__(mcs, name, bases, namespace):
# If it is not a base class (Interface), check implementation
if bases:
for base in bases:
if isinstance(base, InterfaceMeta):
for attr_name, attr_value in base.__dict__.items():
# Checks if the base attribute is a method and not a variable
if callable(attr_value) and not attr_name.startswith('__'):
if attr_name not in namespace:
raise TypeError(f"Class '{name}' must implement method '{attr_name}' of interface '{base.__name__}'")

# Compare method signatures
base_signature = inspect.signature(attr_value)
derived_signature = inspect.signature(namespace[attr_name])

if base_signature != derived_signature:
raise TypeError(
f"Method '{attr_name}' in class '{name}' does not match the interface signature.\n"
f"Expected: {base_signature}\n"
f"Got: {derived_signature}"
)
return super().__new__(mcs, name, bases, namespace)


class Interface(metaclass=InterfaceMeta):
"""Base for all interfaces."""
pass
Loading