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