stouputils.typing.extension_points module#

INHERITABLE_ATTRIBUTE: str = '__is_inheritable__'[source]#

Attribute set to True on a class decorated with inheritable().

OVERRIDABLE_ATTRIBUTE: str = '__is_overridable__'[source]#

Attribute set to True on the function behind a member decorated with overridable().

HOOK_ATTRIBUTE: str = '__is_hook__'[source]#

Attribute set to True on the function behind a member decorated with hook().

ClassMember = ClassMember[source]#

A type alias for anything defined in a class body that can be decorated

inheritable(cls: T) T[source]#

Mark a class as meant to be subclassed, sets __is_inheritable__ to True.

Nothing is enforced at runtime: it only tells the reader that subclassing is part of the class contract.

Examples

>>> @inheritable
... class Base:
...     pass
>>> Base.__is_inheritable__
True
overridable(member: T) T[source]#

Mark a member as a default implementation that subclasses may replace, sets __is_overridable__ to True.

Nothing is enforced at runtime and the member is not wrapped. Stack it above @property, @classmethod or @staticmethod: the flag lands on the underlying function.

Examples

>>> class Base:
...     @overridable
...     def run(self) -> None: ...
...     @overridable
...     @property
...     def name(self) -> str: return "base"
...     @overridable
...     @classmethod
...     def create(cls) -> None: ...
>>> Base.run.__is_overridable__, Base.name.fget.__is_overridable__, Base.create.__is_overridable__
(True, True, True)
hook(member: T) T[source]#

Mark a method as a hook the base class calls at a fixed point of its flow, sets __is_hook__ to True.

Unlike overridable(), the base implementation is usually empty: subclasses fill it in rather than replace it. Nothing is enforced at runtime and the member is not wrapped.

Examples

>>> class Runner:
...     @hook
...     def before_run(self) -> None: ...
>>> Runner.before_run.__is_hook__
True
set_member_flag(
member: ClassMember,
attribute: str,
) None[source]#

Set attribute to True on the function behind a class member.

Parameters:

member – Function, property (its getter is flagged), classmethod or staticmethod (their __func__ is flagged)