Skip to content

Commit 7f28317

Browse files
committed
Dedupe by having one concrete implementation of Specification.
1 parent 4dc3668 commit 7f28317

File tree

6 files changed

+244
-292
lines changed

6 files changed

+244
-292
lines changed

docs/api.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ API Reference
1010
:members:
1111
:undoc-members:
1212

13+
.. autoclass:: referencing._core.Specification
14+
:members:
15+
:undoc-members:
16+
1317

1418
Submodules
1519
----------

docs/conf.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,11 @@
4242
html_static_path = []
4343

4444
# See sphinx-doc/sphinx#10785
45-
_TYPE_ALIASES = {
46-
"Schema",
47-
}
45+
_TYPE_ALIASES = dict(
46+
AnchorType=("class", "Anchor"),
47+
ObjectSchema=("data", "ObjectSchema"),
48+
Schema=("data", "Schema"),
49+
)
4850

4951

5052
def _resolve_broken_refs(app, env, node, contnode):
@@ -62,13 +64,15 @@ def _resolve_broken_refs(app, env, node, contnode):
6264
return resolve_reference_in_inventory(
6365
env, "pyrsistent", node, contnode
6466
)
65-
elif node["reftarget"] in _TYPE_ALIASES:
67+
68+
kind, target = _TYPE_ALIASES.get(node["reftarget"], (None, None))
69+
if kind is not None:
6670
return app.env.get_domain("py").resolve_xref(
6771
env,
6872
node["refdoc"],
6973
app.builder,
70-
"data",
71-
node["reftarget"],
74+
kind,
75+
target,
7276
node,
7377
contnode,
7478
)

referencing/__init__.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
from referencing._core import ( # noqa: F401
2+
OPAQUE_SPECIFICATION,
23
IdentifiedResource,
3-
OpaqueSpecification as _Opaque,
44
Registry,
55
)
6-
7-
OPAQUE_SPECIFICATION = _Opaque()

referencing/_core.py

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
from __future__ import annotations
22

3-
from collections.abc import Iterable, Sequence
3+
from collections.abc import Callable, Iterable, Sequence
44
from urllib.parse import unquote, urldefrag, urljoin
55

66
from attrs import evolve, field
77
from pyrsistent import m, plist, s
88
from pyrsistent.typing import PList, PMap, PSet
99

1010
from referencing._attrs import define, frozen
11-
from referencing.typing import Anchor as AnchorType, Schema, Specification
11+
from referencing.typing import Anchor as AnchorType, ObjectSchema, Schema
1212

1313

1414
class UnidentifiedResource(Exception):
@@ -67,21 +67,29 @@ def resolve(self, resolver, uri):
6767
return self.resource, uri
6868

6969

70-
class OpaqueSpecification:
70+
@frozen
71+
class Specification:
7172
"""
72-
A non-specification `Specification` which treats resources opaquely.
73+
A referencing-defining specification.
7374
74-
In particular, they have no subresources.
75+
See `referencing.jsonschema` for JSON Schema-specific instances.
7576
"""
7677

77-
def id_of(self, resource):
78-
return
78+
id_of: Callable[[Schema], str | None]
79+
subresources_of: Callable[[ObjectSchema], Iterable[Schema]]
80+
_anchors_in: Callable[[ObjectSchema, Specification], Iterable[AnchorType]]
81+
82+
def anchors_in(self, resource: ObjectSchema) -> Iterable[AnchorType]:
83+
return self._anchors_in(resource, self)
7984

80-
def anchors_in(self, resource):
81-
return ()
8285

83-
def subresources_of(self, resource):
84-
return ()
86+
#: A 'null' specification, where resources are opaque
87+
#: (e.g. have no subresources or IDs).
88+
OPAQUE_SPECIFICATION = Specification(
89+
id_of=lambda resource: None,
90+
anchors_in=lambda resource, specification: (),
91+
subresources_of=lambda resource: (),
92+
)
8593

8694

8795
@frozen

0 commit comments

Comments
 (0)