stouputils.decorators.abstract module#

abstract(
func: Callable[..., T],
*,
error_log: LogLevels = LogLevels.RAISE_EXCEPTION,
) Callable[..., T][source]#
abstract(
func: None = None,
*,
error_log: LogLevels = LogLevels.RAISE_EXCEPTION,
) Callable[[Callable[..., T]], Callable[..., T]]

Decorator that marks a function as abstract.

Contrary to the abc.abstractmethod() decorator that raises a TypeError when you try to instantiate a class that has abstract methods, this decorator raises a NotImplementedError ONLY when the decorated function is called, indicating that the function must be implemented by a subclass.

Parameters:
  • func (Callable[..., T] | None) – The function to mark as abstract

  • error_log (LogLevels) –

    Log level for the error handling:

    • LogLevels.NONE - None

    • LogLevels.WARNING - Show as warning

    • LogLevels.WARNING_TRACEBACK - Show as warning with traceback

    • LogLevels.ERROR_TRACEBACK - Show as error with traceback

    • LogLevels.RAISE_EXCEPTION - Raise exception

Examples

>>> class Base:
...     @abstract
...     def method(self):
...         pass
>>> Base().method()
Traceback (most recent call last):
        ...
NotImplementedError: Function 'method()' is abstract and must be implemented by a subclass