typing.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. """
  2. Type-annotation related support for the referencing library.
  3. """
  4. from __future__ import annotations
  5. from collections.abc import Mapping as Mapping
  6. from typing import TYPE_CHECKING, Any, Protocol
  7. try:
  8. from typing_extensions import TypeVar
  9. except ImportError: # pragma: no cover
  10. from typing import TypeVar
  11. if TYPE_CHECKING:
  12. from referencing._core import Resolved, Resolver, Resource
  13. #: A URI which identifies a `Resource`.
  14. URI = str
  15. #: The type of documents within a registry.
  16. D = TypeVar("D", default=Any)
  17. class Retrieve(Protocol[D]):
  18. """
  19. A retrieval callable, usable within a `Registry` for resource retrieval.
  20. Does not make assumptions about where the resource might be coming from.
  21. """
  22. def __call__(self, uri: URI) -> Resource[D]:
  23. """
  24. Retrieve the resource with the given URI.
  25. Raise `referencing.exceptions.NoSuchResource` if you wish to indicate
  26. the retriever cannot lookup the given URI.
  27. """
  28. ...
  29. class Anchor(Protocol[D]):
  30. """
  31. An anchor within a `Resource`.
  32. Beyond "simple" anchors, some specifications like JSON Schema's 2020
  33. version have dynamic anchors.
  34. """
  35. @property
  36. def name(self) -> str:
  37. """
  38. Return the name of this anchor.
  39. """
  40. ...
  41. def resolve(self, resolver: Resolver[D]) -> Resolved[D]:
  42. """
  43. Return the resource for this anchor.
  44. """
  45. ...