Skip to content

Commit af68565

Browse files
committed
More minor tweaks that shouldn't change functionality.
1 parent a7ef637 commit af68565

File tree

2 files changed

+47
-47
lines changed

2 files changed

+47
-47
lines changed

referencing/_core.py

Lines changed: 46 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,21 @@ def from_resource(
4646
def id(self):
4747
return self._specification.id_of(self.resource)
4848

49-
def anchors(self):
49+
def anchors(self) -> list[AnchorType]:
50+
if isinstance(self.resource, bool):
51+
return []
5052
return self._specification.anchors_in(self.resource)
5153

52-
def subresources(self):
53-
for each in self._specification.subresources_of(self.resource):
54-
yield IdentifiedResource.from_resource(
54+
def subresources(self) -> Iterable[IdentifiedResource]:
55+
subresources = self._specification.subresources_of(self.resource) # type: ignore # FIXME: missing test # noqa: E501
56+
return (
57+
IdentifiedResource.from_resource(
5558
resource=each,
5659
default_specification=self._specification,
5760
)
61+
for each in subresources
62+
if each is not True and each is not False
63+
)
5864

5965

6066
@frozen
@@ -77,17 +83,17 @@ class Specification:
7783

7884
id_of: Callable[[Schema], str | None]
7985
subresources_of: Callable[[ObjectSchema], Iterable[Schema]]
80-
_anchors_in: Callable[[ObjectSchema, Specification], Iterable[AnchorType]]
86+
_anchors_in: Callable[[ObjectSchema, Specification], list[AnchorType]]
8187

82-
def anchors_in(self, resource: ObjectSchema) -> Iterable[AnchorType]:
88+
def anchors_in(self, resource: ObjectSchema) -> list[AnchorType]:
8389
return self._anchors_in(resource, self)
8490

8591

8692
#: A 'null' specification, where resources are opaque
8793
#: (e.g. have no subresources or IDs).
8894
OPAQUE_SPECIFICATION = Specification(
8995
id_of=lambda resource: None,
90-
anchors_in=lambda resource, specification: (),
96+
anchors_in=lambda resource, specification: [],
9197
subresources_of=lambda resource: (),
9298
)
9399

@@ -141,57 +147,54 @@ def with_identified_resources(
141147
self,
142148
pairs: Iterable[tuple[str, IdentifiedResource]],
143149
) -> Registry:
144-
uncrawled = self._uncrawled
145-
contents = self._contents
150+
uncrawled = self._uncrawled.evolver()
151+
contents = self._contents.evolver()
146152
for uri, resource in pairs:
147-
anchors: PMap[str, AnchorType] = m()
148-
contents = contents.set(uri, (resource, anchors))
149-
id = resource.id()
150-
if id is not None:
151-
contents = contents.set(id, (resource, anchors))
152-
153-
uncrawled = uncrawled.add(uri)
154-
return evolve(self, contents=contents, uncrawled=uncrawled)
153+
contents[uri] = resource, m()
154+
uncrawled.add(uri)
155+
return evolve(
156+
self,
157+
contents=contents.persistent(),
158+
uncrawled=uncrawled.persistent(),
159+
)
155160

156-
def with_anchors(
161+
def _with_anchors(
157162
self,
158163
uri: str,
159164
anchors: Iterable[AnchorType],
160165
) -> Registry:
161166
assert uri.endswith("#") or "#" not in uri, uri
162167
resource, old = self._contents[uri]
163168
new = old.update({anchor.name: anchor for anchor in anchors})
164-
contents = self._contents.set(uri, (resource, new))
165-
return evolve(self, contents=contents)
169+
return evolve(self, contents=self._contents.set(uri, (resource, new)))
166170

167-
def resource_at(self, uri: str) -> tuple[IdentifiedResource, Registry]:
171+
def resource_at(
172+
self, uri: str
173+
) -> tuple[IdentifiedResource, PMap[str, AnchorType], Registry]:
168174
at_uri = self._contents.get(uri)
169-
if at_uri is not None and at_uri[1]:
170-
registry = self
171-
else:
175+
if at_uri is None or uri in self._uncrawled:
172176
registry = self._crawl()
173-
return registry._contents[uri][0], registry
174-
175-
def anchors_at(self, uri: str) -> PMap[str, AnchorType]:
176-
return self._contents[uri][1]
177+
return *registry._contents[uri], registry
178+
return *at_uri, self
177179

178180
def _crawl(self) -> Registry:
179181
registry = self
180182
resources = [(uri, self._contents[uri][0]) for uri in self._uncrawled]
181183
while resources:
182184
base_uri, resource = resources.pop()
183-
if resource.resource is True or resource.resource is False:
184-
continue
185-
186-
uri = urljoin(base_uri, resource.id() or "")
187-
if uri != base_uri:
185+
id = resource.id()
186+
if id is None:
187+
uri = base_uri
188+
else:
189+
uri = urljoin(base_uri, id)
188190
registry = registry.with_identified_resource(
189191
uri=uri,
190192
resource=resource,
191193
)
192194

193195
anchors = resource.anchors()
194-
registry = registry.with_anchors(uri=uri, anchors=anchors)
196+
if anchors:
197+
registry = registry._with_anchors(uri, anchors)
195198

196199
resources.extend((uri, each) for each in resource.subresources())
197200
return evolve(registry, uncrawled=s())
@@ -221,7 +224,7 @@ def lookup(self, ref: str) -> tuple[Schema, Resolver]:
221224
else:
222225
uri, fragment = urldefrag(urljoin(self._base_uri, ref))
223226

224-
resource, registry = self._registry.resource_at(uri)
227+
resource, anchors, registry = self._registry.resource_at(uri)
225228
base_uri = uri
226229
target = resource.resource
227230

@@ -240,19 +243,17 @@ def lookup(self, ref: str) -> tuple[Schema, Resolver]:
240243
if id is not None:
241244
base_uri = urljoin(base_uri, id).rstrip("#")
242245
elif fragment:
243-
anchor = registry.anchors_at(uri=uri)[fragment]
244-
resource, uri = anchor.resolve(resolver=self, uri=uri)
246+
resource, uri = anchors[fragment].resolve(resolver=self, uri=uri)
245247
target = resource.resource
246248

247249
id = resource.id()
248250
if id is not None:
249251
base_uri = urljoin(self._base_uri, id).rstrip("#")
250252
else:
251-
target = resource.resource
252253
id = resource.id()
253254
if id is not None:
254255
base_uri = urljoin(self._base_uri, id).rstrip("#")
255-
return target, self.evolve(base_uri=base_uri, registry=registry)
256+
return target, self._evolve(base_uri=base_uri, registry=registry)
256257

257258
def with_root(
258259
self,
@@ -271,14 +272,13 @@ def with_root(
271272
specification=specification,
272273
),
273274
)
274-
return self.evolve(base_uri=uri, registry=registry)
275-
276-
def evolve(self, **kwargs):
277-
previous = self._previous.cons(self._base_uri)
278-
return evolve(self, previous=previous, **kwargs)
275+
return self._evolve(base_uri=uri, registry=registry)
279276

280277
def dynamic_scope(self):
281278
for uri in self._previous:
282-
resource, _ = self._registry.resource_at(uri)
283-
anchors = self._registry.anchors_at(uri)
279+
resource, anchors, _ = self._registry.resource_at(uri)
284280
yield uri, resource, anchors
281+
282+
def _evolve(self, **kwargs):
283+
previous = self._previous.cons(self._base_uri)
284+
return evolve(self, previous=previous, **kwargs)

referencing/jsonschema.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ def lookup_recursive_ref(
254254
return subschema, resolver
255255

256256

257-
BY_ID: dict[str, Specification] = {
257+
BY_ID = {
258258
"https://json-schema.org/draft/2020-12/schema": DRAFT202012,
259259
"https://json-schema.org/draft/2019-09/schema": DRAFT201909,
260260
"http://json-schema.org/draft-07/schema#": DRAFT7,

0 commit comments

Comments
 (0)