11from __future__ import annotations
22
33from collections .abc import Sequence
4- from typing import TYPE_CHECKING
54from urllib .parse import unquote , urldefrag , urljoin
65
6+ from attrs import evolve , field
77from pyrsistent import m , plist , s
88from pyrsistent .typing import PList , PMap , PSet
99
10+ from referencing ._attrs import define , frozen
11+ from referencing .jsonschema import DynamicAnchor , id_of
1012from referencing .typing import Anchor as AnchorType , Schema , Specification
1113
1214
13- class UnsupportedSubclassing (Exception ):
14- @classmethod
15- def complain (this ):
16- raise UnsupportedSubclassing (
17- "Subclassing is not part of referencing's public API. "
18- "If no other suitable API exists for what you're trying to do, "
19- "feel free to file an issue asking for one." ,
20- )
21-
22-
2315class UnidentifiedResource (Exception ):
2416 pass
2517
2618
27- if TYPE_CHECKING :
28- from attrs import define , evolve , field , frozen
29- else :
30- from attrs import define as _define , evolve , field , frozen as _frozen
31-
32- def define (cls ):
33- cls .__init_subclass__ = UnsupportedSubclassing .complain
34- return _define (cls )
35-
36- def frozen (cls ):
37- cls .__init_subclass__ = UnsupportedSubclassing .complain
38- return _frozen (cls )
39-
40-
4119@frozen
4220class Anchor :
4321
@@ -49,24 +27,6 @@ def resolve(self, dynamic_scope, uri) -> tuple[Schema, str]:
4927 return self .resource , uri
5028
5129
52- @frozen
53- class DynamicAnchor :
54-
55- uri : str
56- name : str
57- resource : Schema
58-
59- def resolve (self , dynamic_scope , uri ):
60- last = self .resource
61- for resource , anchors in dynamic_scope :
62- anchor = anchors .get (self .name )
63- if isinstance (anchor , DynamicAnchor ):
64- last = anchor .resource
65- elif "$ref" not in resource :
66- break
67- return last , id_of (last ) or "" # FIXME: consider when this can be None
68-
69-
7030class OpaqueSpecification :
7131 """
7232 A non-specification `Specification` which treats resources opaquely.
@@ -234,9 +194,3 @@ def dynamic_scope(self):
234194 for uri in self ._previous :
235195 resource , _ = self ._registry .resource_at (uri )
236196 yield resource , self ._registry .anchors_at (uri )
237-
238-
239- def id_of (resource ) -> str | None :
240- if resource is True or resource is False :
241- return None
242- return resource .get ("$id" )
0 commit comments