Skip to content

Commit 90b53d3

Browse files
committed
Give specifications names to make their reprs easier to decipher.
1 parent 7beee92 commit 90b53d3

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

referencing/_core.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ class Specification(Generic[D]):
2222
behavior across JSON Schema specification versions, etc.
2323
"""
2424

25+
#: A short human-readable name for the specification, used for debugging.
26+
name: str
27+
2528
#: Find the ID of a given document.
2629
id_of: Callable[[D], URI | None]
2730

@@ -33,6 +36,9 @@ class Specification(Generic[D]):
3336
#: nor internal identifiers.
3437
OPAQUE: ClassVar[Specification[Any]]
3538

39+
def __repr__(self):
40+
return f"<Specification name={self.name!r}>"
41+
3642
def create_resource(self, contents: D) -> Resource[D]:
3743
"""
3844
Create a resource which is interpreted using this specification.
@@ -41,6 +47,7 @@ def create_resource(self, contents: D) -> Resource[D]:
4147

4248

4349
Specification.OPAQUE = Specification(
50+
name="opaque",
4451
id_of=lambda contents: None,
4552
subresources_of=lambda contents: [],
4653
)

referencing/jsonschema.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,26 +56,32 @@ def subresources_of(resource: ObjectSchema) -> Iterable[ObjectSchema]:
5656

5757

5858
DRAFT202012 = Specification(
59+
name="draft2020-12",
5960
id_of=_dollar_id,
6061
subresources_of=lambda contents: [],
6162
)
6263
DRAFT201909 = Specification(
64+
name="draft2019-09",
6365
id_of=_dollar_id,
6466
subresources_of=lambda contents: [],
6567
)
6668
DRAFT7 = Specification(
69+
name="draft-07",
6770
id_of=_dollar_id,
6871
subresources_of=_subresources_of(values={"if", "then", "else"}),
6972
)
7073
DRAFT6 = Specification(
74+
name="draft-06",
7175
id_of=_dollar_id,
7276
subresources_of=lambda contents: [],
7377
)
7478
DRAFT4 = Specification(
79+
name="draft-04",
7580
id_of=_legacy_id,
7681
subresources_of=lambda contents: [],
7782
)
7883
DRAFT3 = Specification(
84+
name="draft-03",
7985
id_of=_legacy_id,
8086
subresources_of=lambda contents: [],
8187
)

referencing/tests/test_core.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from referencing.jsonschema import DRAFT202012
55

66
ID_AND_CHILDREN = Specification(
7+
name="id-and-children",
78
id_of=lambda contents: contents.get("ID"),
89
subresources_of=lambda contents: contents.get("children", []),
910
)
@@ -274,6 +275,7 @@ def test_from_contents_with_fallback(self):
274275

275276
def test_id_delegates_to_specification(self):
276277
specification = Specification(
278+
name="",
277279
id_of=lambda contents: "urn:fixedID",
278280
subresources_of=lambda contents: [],
279281
)
@@ -367,6 +369,7 @@ def test_lookup_non_existent_pointer_to_array_index(self):
367369
class TestSpecification:
368370
def test_create_resource(self):
369371
specification = Specification(
372+
name="",
370373
id_of=lambda contents: "urn:fixedID",
371374
subresources_of=lambda contents: [],
372375
)
@@ -377,6 +380,11 @@ def test_create_resource(self):
377380
)
378381
assert resource.id() == "urn:fixedID"
379382

383+
def test_repr(self):
384+
assert (
385+
repr(ID_AND_CHILDREN) == "<Specification name='id-and-children'>"
386+
)
387+
380388

381389
class TestOpaqueSpecification:
382390

0 commit comments

Comments
 (0)