Skip to content

Commit 71e9d6a

Browse files
committed
Minor convenience -- Resource.opaque.
1 parent ea910ee commit 71e9d6a

File tree

4 files changed

+35
-49
lines changed

4 files changed

+35
-49
lines changed

.flake8

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ ignore =
1616
D200,
1717
# This rule misses sassy docstrings ending with ! or ?.
1818
D400,
19+
# Plz spaces after section headers
20+
D412,
1921
# This conflicts with the hilarious way one tells mypy you mean to
2022
# re-export an imported object
2123
I250,

referencing/_core.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,15 @@ def from_contents(
7676
raise CannotDetermineSpecification(contents)
7777
return cls(contents=contents, specification=specification) # type: ignore[reportUnknownArgumentType] # noqa: E501
7878

79+
@classmethod
80+
def opaque(cls, contents: D) -> Resource[D]:
81+
"""
82+
Create an opaque `Resource` -- i.e. one with opaque specification.
83+
84+
See `Specification.OPAQUE` for details.
85+
"""
86+
return cls(contents=contents, specification=Specification.OPAQUE)
87+
7988
def id(self) -> URI | None:
8089
"""
8190
Retrieve this resource's (specification-specific) identifier.
@@ -221,7 +230,6 @@ def lookup(self, ref: URI) -> Resolved[D]:
221230
"""
222231
Resolve the given reference to the resource it points to.
223232
224-
225233
Raises:
226234
227235
`Unresolvable`

referencing/tests/test_core.py

Lines changed: 22 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,7 @@ def test_with_resource(self):
1010
Adding a resource to the registry then allows re-retrieving it.
1111
"""
1212

13-
resource = Resource(
14-
contents={"foo": "bar"},
15-
specification=Specification.OPAQUE,
16-
)
13+
resource = Resource.opaque(contents={"foo": "bar"})
1714
uri = "urn:example"
1815
registry = Registry().with_resource(uri=uri, resource=resource)
1916
assert registry[uri] is resource
@@ -23,7 +20,7 @@ def test_with_resources(self):
2320
Adding multiple resources to the registry is like adding each one.
2421
"""
2522

26-
one = Resource(contents={}, specification=Specification.OPAQUE)
23+
one = Resource.opaque(contents={})
2724
two = Resource(contents={"foo": "bar"}, specification=DRAFT202012)
2825
registry = Registry().with_resources(
2926
[
@@ -53,11 +50,7 @@ def test_with_contents_and_default_specification(self):
5350
[(uri, {"foo": "bar"})],
5451
default_specification=Specification.OPAQUE,
5552
)
56-
expected = Resource(
57-
contents={"foo": "bar"},
58-
specification=Specification.OPAQUE,
59-
)
60-
assert registry[uri] == expected
53+
assert registry[uri] == Resource.opaque({"foo": "bar"})
6154

6255
def test_len(self):
6356
total = 5
@@ -75,25 +68,19 @@ def test_iter(self):
7568
assert set(registry) == {str(i) for i in range(8)}
7669

7770
def test_crawl_still_has_top_level_resource(self):
78-
resource = Resource(
79-
contents={"foo": "bar"},
80-
specification=Specification.OPAQUE,
81-
)
71+
resource = Resource.opaque({"foo": "bar"})
8272
uri = "urn:example"
8373
registry = Registry().with_resources([(uri, resource)]).crawl()
8474
assert registry[uri] is resource
8575

8676
def test_contents(self):
87-
resource = Resource(
88-
contents={"foo": "bar"},
89-
specification=Specification.OPAQUE,
90-
)
77+
resource = Resource.opaque({"foo": "bar"})
9178
uri = "urn:example"
9279
registry = Registry().with_resources([(uri, resource)])
9380
assert registry.contents(uri) == {"foo": "bar"}
9481

9582
def test_init(self):
96-
one = Resource(contents={}, specification=Specification.OPAQUE)
83+
one = Resource.opaque(contents={})
9784
two = Resource(contents={"foo": "bar"}, specification=DRAFT202012)
9885
registry = Registry(
9986
{
@@ -115,7 +102,7 @@ def test_dict_conversion(self):
115102
So continuing to use the registry works.
116103
"""
117104

118-
one = Resource(contents={}, specification=Specification.OPAQUE)
105+
one = Resource.opaque(contents={})
119106
two = Resource(contents={"foo": "bar"}, specification=DRAFT202012)
120107
registry = Registry(
121108
{"http://example.com/1": one},
@@ -128,7 +115,7 @@ def test_dict_conversion(self):
128115
)
129116

130117
def test_combine(self):
131-
one = Resource(contents={}, specification=Specification.OPAQUE)
118+
one = Resource.opaque(contents={})
132119
two = Resource(contents={"foo": "bar"}, specification=DRAFT202012)
133120
three = Resource(contents={"baz": "quux"}, specification=DRAFT202012)
134121

@@ -170,10 +157,7 @@ def test_from_contents_with_no_discernible_information_and_default(self):
170157
{"foo": "bar"},
171158
default_specification=Specification.OPAQUE,
172159
)
173-
assert resource == Resource(
174-
contents={"foo": "bar"},
175-
specification=Specification.OPAQUE,
176-
)
160+
assert resource == Resource.opaque(contents={"foo": "bar"})
177161

178162
def test_from_contents_unneeded_default(self):
179163
schema = {"$schema": "https://json-schema.org/draft/2020-12/schema"}
@@ -189,22 +173,16 @@ def test_from_contents_unneeded_default(self):
189173
def test_non_mapping_from_contents(self):
190174
resource = Resource.from_contents(
191175
True,
192-
default_specification=Specification.OPAQUE,
193-
)
194-
assert resource == Resource(
195-
contents=True,
196-
specification=Specification.OPAQUE,
176+
default_specification=DRAFT202012,
197177
)
178+
assert resource == Resource(contents=True, specification=DRAFT202012)
198179

199180
def test_from_contents_with_fallback(self):
200181
resource = Resource.from_contents(
201182
{"foo": "bar"},
202183
default_specification=Specification.OPAQUE,
203184
)
204-
assert resource == Resource(
205-
contents={"foo": "bar"},
206-
specification=Specification.OPAQUE,
207-
)
185+
assert resource == Resource.opaque(contents={"foo": "bar"})
208186

209187
def test_id_delegates_to_specification(self):
210188
specification = Specification(id_of=lambda contents: "urn:fixedID")
@@ -215,28 +193,26 @@ def test_id_delegates_to_specification(self):
215193
assert resource.id() == "urn:fixedID"
216194

217195
def test_pointer_to_mapping(self):
218-
resource = Resource(
219-
contents={"foo": "baz"},
220-
specification=Specification.OPAQUE,
221-
)
196+
resource = Resource.opaque(contents={"foo": "baz"})
222197
resolver = Registry().resolver()
223198
assert resource.pointer("/foo", resolver=resolver).contents == "baz"
224199

225200
def test_pointer_to_array(self):
226-
resource = Resource(
227-
contents={"foo": {"bar": [3]}},
228-
specification=Specification.OPAQUE,
229-
)
201+
resource = Resource.opaque(contents={"foo": {"bar": [3]}})
230202
resolver = Registry().resolver()
231203
assert resource.pointer("/foo/bar/0", resolver=resolver).contents == 3
232204

205+
def test_opaque(self):
206+
contents = {"foo": "bar"}
207+
assert Resource.opaque(contents) == Resource(
208+
contents=contents,
209+
specification=Specification.OPAQUE,
210+
)
211+
233212

234213
class TestResolver:
235214
def test_lookup_exact_uri(self):
236-
resource = Resource(
237-
contents={"foo": "baz"},
238-
specification=Specification.OPAQUE,
239-
)
215+
resource = Resource.opaque(contents={"foo": "baz"})
240216
resolver = Registry({"http://example.com/1": resource}).resolver()
241217
resolved = resolver.lookup("http://example.com/1")
242218
assert resolved.contents == resource.contents

referencing/tests/test_referencing_suite.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import pytest
1010

11-
from referencing import Registry, Resource, Specification
11+
from referencing import Registry, Resource
1212

1313

1414
class SuiteNotFound(Exception):
@@ -34,7 +34,7 @@ def test_referencing_suite(test_path):
3434
loaded = toml.loads(test_path.read_text())
3535
registry = loaded["registry"]
3636
registry = Registry().with_resources(
37-
(uri, Resource(contents=contents, specification=Specification.OPAQUE))
37+
(uri, Resource.opaque(contents=contents))
3838
for uri, contents in loaded["registry"].items()
3939
)
4040
for test in loaded["tests"]:

0 commit comments

Comments
 (0)