Skip to content

Commit

Permalink
Make Registry.contents raise NoSuchResource when needed.
Browse files Browse the repository at this point in the history
Previously it was propagating the KeyError (which we already don't do
elsewhere on other registry methods).
  • Loading branch information
Julian committed Apr 24, 2024
1 parent 1782bd5 commit 4b51260
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
5 changes: 5 additions & 0 deletions docs/changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
Changelog
=========

v0.35.0
-------

* Ensure that ``Registry.contents()`` also raises ``NoSuchResource`` exceptions for nonexistent resources, not ``KeyError`` (which is an implementation detail).

v0.34.0
-------

Expand Down
4 changes: 1 addition & 3 deletions referencing/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -477,9 +477,7 @@ def contents(self, uri: URI) -> D:
"""
Retrieve the (already crawled) contents identified by the given URI.
"""
# Empty fragment URIs are equivalent to URIs without the fragment.
# TODO: Is this true for non JSON Schema resources? Probably not.
return self._resources[uri.rstrip("#")].contents
return self[uri].contents

def crawl(self) -> Registry[D]:
"""
Expand Down
6 changes: 6 additions & 0 deletions referencing/tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,12 @@ def test_contents_strips_empty_fragments(self):
== {"ID": uri + "#"}
)

def test_contents_nonexistent_resource(self):
registry = Registry()
with pytest.raises(exceptions.NoSuchResource) as e:
registry.contents("urn:example")
assert e.value == exceptions.NoSuchResource(ref="urn:example")

def test_crawled_anchor(self):
resource = ID_AND_CHILDREN.create_resource({"anchors": {"foo": "bar"}})
registry = Registry().with_resource("urn:example", resource)
Expand Down

0 comments on commit 4b51260

Please sign in to comment.