Skip to content

Commit e4b215c

Browse files
committed
Specify the argument name for retrieval callbacks via a protocol.
1 parent a83ec83 commit e4b215c

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

referencing/_core.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
from referencing import exceptions
2121
from referencing._attrs import frozen
22-
from referencing.typing import URI, Anchor as AnchorType, D, Mapping
22+
from referencing.typing import URI, Anchor as AnchorType, D, Mapping, Retrieve
2323

2424
EMPTY_RESOURCES: PMap[URI, Resource[Any]] = pmap({}, pre_size=64)
2525
EMPTY_ANCHORS = cast(PMap[Tuple[URI, str], AnchorType[Any]], EMPTY_RESOURCES)
@@ -253,7 +253,7 @@ class Registry(Mapping[URI, Resource[D]]):
253253
default=EMPTY_ANCHORS,
254254
)
255255
_uncrawled: PSet[URI] = field(default=EMPTY_UNCRAWLED)
256-
_retrieve: Callable[[URI], Resource[D]] = field(default=_fail_to_retrieve)
256+
_retrieve: Retrieve[D] = field(default=_fail_to_retrieve)
257257

258258
def __getitem__(self, uri: URI) -> Resource[D]:
259259
"""
@@ -427,8 +427,8 @@ def combine(self, *registries: Registry[D]) -> Registry[D]:
427427
anchors = anchors.update(registry._anchors) # type: ignore[reportUnknownMemberType] # noqa: E501
428428
uncrawled = uncrawled.update(registry._uncrawled)
429429

430-
if registry._retrieve != _fail_to_retrieve:
431-
if registry._retrieve != retrieve != _fail_to_retrieve:
430+
if registry._retrieve is not _fail_to_retrieve:
431+
if registry._retrieve is not retrieve is not _fail_to_retrieve:
432432
raise ValueError(
433433
"Cannot combine registries with conflicting retrieval "
434434
"functions.",

referencing/typing.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515

1616
if TYPE_CHECKING:
17-
from referencing._core import Resolved, Resolver
17+
from referencing._core import Resolved, Resolver, Resource
1818

1919
#: A URI which identifies a `Resource`.
2020
URI = str
@@ -23,6 +23,23 @@
2323
D = TypeVar("D")
2424

2525

26+
class Retrieve(Protocol[D]):
27+
"""
28+
A retrieval callable, usable within a `Registry` for resource retrieval.
29+
30+
Does not make assumptions about where the resource might be coming from.
31+
"""
32+
33+
def __call__(self, uri: URI) -> Resource[D]:
34+
"""
35+
Retrieve the resource with the given URI.
36+
37+
Raise `referencing.exceptions.NoSuchResource` if you wish to indicate
38+
the retriever cannot lookup the given URI.
39+
"""
40+
...
41+
42+
2643
class Anchor(Protocol[D]):
2744
"""
2845
An anchor within a `Resource`.

0 commit comments

Comments
 (0)