enum.py 689 B

1234567891011121314151617181920212223
  1. """Enum backports from standard lib."""
  2. from __future__ import annotations
  3. from enum import Enum
  4. from typing import Any, TypeVar
  5. _StrEnumT = TypeVar("_StrEnumT", bound="StrEnum")
  6. class StrEnum(str, Enum):
  7. """Partial backport of Python 3.11's StrEnum for our basic use cases."""
  8. def __new__(
  9. cls: type[_StrEnumT], value: str, *args: Any, **kwargs: Any
  10. ) -> _StrEnumT:
  11. """Create a new StrEnum instance."""
  12. if not isinstance(value, str):
  13. raise TypeError(f"{value!r} is not a string")
  14. return super().__new__(cls, value, *args, **kwargs)
  15. def __str__(self) -> str:
  16. """Return self.value."""
  17. return str(self.value)