hbutils.design.final

Overview:

Final class implementation module providing metaclass to prevent class inheritance.

This module implements a metaclass that makes classes final (unable to be extended by other classes). It’s not a custom design pattern, but a useful utility for designing immutable class hierarchies.

Note

This is particularly useful when you want to prevent further inheritance of a class to maintain its integrity and prevent unintended modifications.

FinalMeta

class hbutils.design.final.FinalMeta(name, bases, attrs)[source]

A metaclass for making a class final (unable to be extended by other classes).

This metaclass prevents any class from inheriting from classes that use it as their metaclass. When a class attempts to inherit from a final class, a TypeError will be raised at class definition time.

Example::
>>> class FinalClass(metaclass=FinalMeta):  # this is a final class
...     pass
...
>>> class TryToExtendFinalClass(FinalClass):  # TypeError will be raised at compile time
...     pass
Traceback (most recent call last):
    ...
TypeError: Type 'FinalClass' is a final class, which is not an acceptable common type.
__init__(*args, **kwargs)
static __new__(mcs, name, bases, attrs)[source]

Create a new finalized class and validate that it doesn’t inherit from any final classes.

This method is called when a new class is being created. It checks all base classes to ensure none of them are final classes. If any base class is final, it raises a TypeError to prevent the inheritance.

Parameters:
  • mcs (type) – The metaclass itself (FinalMeta).

  • name (str) – Name of the new class being created.

  • bases (Tuple[type, ...]) – Tuple of base classes for the new class.

  • attrs (Dict[str, Any]) – Dictionary of attributes (methods and fields) for the new class.

Returns:

The newly created class object.

Return type:

type

Raises:

TypeError – If any base class is a final class (uses FinalMeta as metaclass).

Example::
>>> class FinalClass(metaclass=FinalMeta):  # this is a final class
...     pass
...
>>> class TryToExtendFinalClass(FinalClass):  # TypeError will be raised at compile time
...     pass
Traceback (most recent call last):
    ...
TypeError: Type 'FinalClass' is a final class, which is not an acceptable common type.