1
1
from __future__ import annotations
2
2
3
3
from collections .abc import Sequence
4
- from typing import TYPE_CHECKING
5
4
from urllib .parse import unquote , urldefrag , urljoin
6
5
6
+ from attrs import evolve , field
7
7
from pyrsistent import m , plist , s
8
8
from pyrsistent .typing import PList , PMap , PSet
9
9
10
+ from referencing ._attrs import define , frozen
11
+ from referencing .jsonschema import DynamicAnchor , id_of
10
12
from referencing .typing import Anchor as AnchorType , Schema , Specification
11
13
12
14
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
-
23
15
class UnidentifiedResource (Exception ):
24
16
pass
25
17
26
18
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
-
41
19
@frozen
42
20
class Anchor :
43
21
@@ -49,24 +27,6 @@ def resolve(self, dynamic_scope, uri) -> tuple[Schema, str]:
49
27
return self .resource , uri
50
28
51
29
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
-
70
30
class OpaqueSpecification :
71
31
"""
72
32
A non-specification `Specification` which treats resources opaquely.
@@ -234,9 +194,3 @@ def dynamic_scope(self):
234
194
for uri in self ._previous :
235
195
resource , _ = self ._registry .resource_at (uri )
236
196
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