main.py 1.0 KB

123456789101112131415161718192021222324252627282930313233343536
  1. class Doc:
  2. """Define the documentation of a type annotation using `Annotated`, to be
  3. used in class attributes, function and method parameters, return values,
  4. and variables.
  5. The value should be a positional-only string literal to allow static tools
  6. like editors and documentation generators to use it.
  7. This complements docstrings.
  8. The string value passed is available in the attribute `documentation`.
  9. Example:
  10. ```Python
  11. from typing import Annotated
  12. from annotated_doc import Doc
  13. def hi(name: Annotated[str, Doc("Who to say hi to")]) -> None:
  14. print(f"Hi, {name}!")
  15. ```
  16. """
  17. def __init__(self, documentation: str, /) -> None:
  18. self.documentation = documentation
  19. def __repr__(self) -> str:
  20. return f"Doc({self.documentation!r})"
  21. def __hash__(self) -> int:
  22. return hash(self.documentation)
  23. def __eq__(self, other: object) -> bool:
  24. if not isinstance(other, Doc):
  25. return NotImplemented
  26. return self.documentation == other.documentation