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