hbutils.design.final

Final class enforcement utilities.

This module provides the FinalMeta metaclass, which can be used to define classes that cannot be subclassed. It is designed for situations where you want to explicitly prevent inheritance to preserve a class’s integrity or guarantee certain behaviors remain unchanged.

The module contains the following main components:

  • FinalMeta - Metaclass that makes classes final (non-inheritable)

Note

The inheritance check is performed at class definition time. Any attempt to subclass a final class raises a TypeError.

Example:

>>> from hbutils.design.final import FinalMeta
>>> class FinalClass(metaclass=FinalMeta):
...     pass
...
>>> class TryToExtendFinalClass(FinalClass):
...     pass
Traceback (most recent call last):
    ...
TypeError: Type 'FinalClass' is a final class, which is not an acceptable common type.

__all__

hbutils.design.final.__all__ = ['FinalMeta']

Built-in mutable sequence.

If no argument is given, the constructor creates a new empty list. The argument must be an iterable if specified.

FinalMeta

class hbutils.design.final.FinalMeta(name: str, bases: Tuple[type, ...], attrs: Dict[str, Any])[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.
static __new__(mcs: type, name: str, bases: Tuple[type, ...], attrs: Dict[str, Any]) type[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.