_utils.py 943 B

123456789101112131415161718192021222324252627282930313233343536
  1. from __future__ import annotations
  2. import enum
  3. import typing as t
  4. class Sentinel(enum.Enum):
  5. """Enum used to define sentinel values.
  6. .. seealso::
  7. `PEP 661 - Sentinel Values <https://peps.python.org/pep-0661/>`_.
  8. """
  9. UNSET = object()
  10. FLAG_NEEDS_VALUE = object()
  11. def __repr__(self) -> str:
  12. return f"{self.__class__.__name__}.{self.name}"
  13. UNSET = Sentinel.UNSET
  14. """Sentinel used to indicate that a value is not set."""
  15. FLAG_NEEDS_VALUE = Sentinel.FLAG_NEEDS_VALUE
  16. """Sentinel used to indicate an option was passed as a flag without a
  17. value but is not a flag option.
  18. ``Option.consume_value`` uses this to prompt or use the ``flag_value``.
  19. """
  20. T_UNSET = t.Literal[UNSET] # type: ignore[valid-type]
  21. """Type hint for the :data:`UNSET` sentinel value."""
  22. T_FLAG_NEEDS_VALUE = t.Literal[FLAG_NEEDS_VALUE] # type: ignore[valid-type]
  23. """Type hint for the :data:`FLAG_NEEDS_VALUE` sentinel value."""