2017-09-25 09:05:09 -07:00
|
|
|
"""Decorator utility functions."""
|
2024-03-08 16:36:11 +01:00
|
|
|
|
2021-09-29 16:32:11 +02:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
from collections.abc import Callable, Hashable
|
2022-02-23 20:58:42 +01:00
|
|
|
from typing import Any, TypeVar
|
2018-07-26 09:55:42 +03:00
|
|
|
|
2022-02-23 20:58:42 +01:00
|
|
|
_KT = TypeVar("_KT", bound=Hashable)
|
|
|
|
_VT = TypeVar("_VT", bound=Callable[..., Any])
|
2017-09-25 09:05:09 -07:00
|
|
|
|
|
|
|
|
2022-02-23 20:58:42 +01:00
|
|
|
class Registry(dict[_KT, _VT]):
|
2017-09-25 09:05:09 -07:00
|
|
|
"""Registry of items."""
|
|
|
|
|
2022-02-23 20:58:42 +01:00
|
|
|
def register(self, name: _KT) -> Callable[[_VT], _VT]:
|
2017-09-25 09:05:09 -07:00
|
|
|
"""Return decorator to register item with a specific name."""
|
2019-07-31 12:25:30 -07:00
|
|
|
|
2022-02-23 20:58:42 +01:00
|
|
|
def decorator(func: _VT) -> _VT:
|
2017-09-25 09:05:09 -07:00
|
|
|
"""Register decorated function."""
|
|
|
|
self[name] = func
|
|
|
|
return func
|
|
|
|
|
|
|
|
return decorator
|