utils.py 780 B

12345678910111213141516171819202122232425262728293031
  1. """Utility functions for constants."""
  2. from collections.abc import Callable
  3. from typing import Any, Generic, TypeVar
  4. T = TypeVar("T")
  5. V = TypeVar("V")
  6. class classproperty(Generic[T, V]):
  7. """A class property decorator."""
  8. def __init__(self, getter: Callable[[type[T]], V]) -> None:
  9. """Initialize the class property.
  10. Args:
  11. getter: The getter function.
  12. """
  13. self.getter = getattr(getter, "__func__", getter)
  14. def __get__(self, instance: Any, owner: type[T]) -> V:
  15. """Get the value of the class property.
  16. Args:
  17. instance: The instance of the class.
  18. owner: The class itself.
  19. Returns:
  20. The value of the class property.
  21. """
  22. return self.getter(owner)