Python Metaclass Architecture Pattern
Implements advanced Python metaclasses for structural class validation.
Works with:
all
Prompt
Act as a senior Python software architect. Write an advanced metaclass implementation in Python for enforcing mandatory attribute validation across all subclass definitions of [BASE_CLASS].
Example Output
python
class ValidatorMeta(type):
def __new__(cls, name, bases, attrs):
if 'required_field' not in attrs:
raise TypeError(f"Class {name} missing required attribute 'required_field'")
return super().__new__(cls, name, bases, attrs)
class Base(metaclass=ValidatorMeta):
pass
class ValidatorMeta(type):
def __new__(cls, name, bases, attrs):
if 'required_field' not in attrs:
raise TypeError(f"Class {name} missing required attribute 'required_field'")
return super().__new__(cls, name, bases, attrs)
class Base(metaclass=ValidatorMeta):
pass