typing.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. """
  2. Type-annotation related support for the referencing library.
  3. """
  4. from __future__ import annotations
  5. from typing import TYPE_CHECKING, Protocol, TypeVar
  6. try:
  7. from collections.abc import Mapping as Mapping
  8. Mapping[str, str]
  9. except TypeError: # pragma: no cover
  10. from typing import Mapping as Mapping
  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")
  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. ...