1
0

utils.py 759 B

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