Skip to content

Commit 4b51260

Browse files
committed
Make Registry.contents raise NoSuchResource when needed.
Previously it was propagating the KeyError (which we already don't do elsewhere on other registry methods).
1 parent 1782bd5 commit 4b51260

File tree

3 files changed

+12
-3
lines changed

3 files changed

+12
-3
lines changed

docs/changes.rst

+5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
Changelog
33
=========
44

5+
v0.35.0
6+
-------
7+
8+
* Ensure that ``Registry.contents()`` also raises ``NoSuchResource`` exceptions for nonexistent resources, not ``KeyError`` (which is an implementation detail).
9+
510
v0.34.0
611
-------
712

referencing/_core.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -477,9 +477,7 @@ def contents(self, uri: URI) -> D:
477477
"""
478478
Retrieve the (already crawled) contents identified by the given URI.
479479
"""
480-
# Empty fragment URIs are equivalent to URIs without the fragment.
481-
# TODO: Is this true for non JSON Schema resources? Probably not.
482-
return self._resources[uri.rstrip("#")].contents
480+
return self[uri].contents
483481

484482
def crawl(self) -> Registry[D]:
485483
"""

referencing/tests/test_core.py

+6
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,12 @@ def test_contents_strips_empty_fragments(self):
188188
== {"ID": uri + "#"}
189189
)
190190

191+
def test_contents_nonexistent_resource(self):
192+
registry = Registry()
193+
with pytest.raises(exceptions.NoSuchResource) as e:
194+
registry.contents("urn:example")
195+
assert e.value == exceptions.NoSuchResource(ref="urn:example")
196+
191197
def test_crawled_anchor(self):
192198
resource = ID_AND_CHILDREN.create_resource({"anchors": {"foo": "bar"}})
193199
registry = Registry().with_resource("urn:example", resource)

0 commit comments

Comments
 (0)