13
13
load_schema_into_namespace(schema['meta']['context'], globals(), 'Context')
14
14
"""
15
15
16
+ from __future__ import annotations
17
+
16
18
import json
17
- from typing import Any , Union
18
19
19
20
import attrs
20
21
import bidsschematools as bst
21
22
import bidsschematools .schema
22
23
24
+ from . import _typings as t
25
+
23
26
LATEST_SCHEMA_URL = 'https://bids-specification.readthedocs.io/en/latest/schema.json'
24
27
STABLE_SCHEMA_URL = 'https://bids-specification.readthedocs.io/en/stable/schema.json'
25
28
26
29
27
- def get_schema (url : Union [ str , None ] = None ) -> bst .types .Namespace :
30
+ def get_schema (url : str | None = None ) -> bst .types .Namespace :
28
31
"""Load a BIDS schema from a URL or return the bundled schema if no URL is provided.
29
32
30
33
This function utilizes the ``universal_pathlib`` package to handle various URL schemes.
@@ -75,7 +78,7 @@ def snake_to_pascal(val: str) -> str:
75
78
return '' .join (sub .capitalize () for sub in val .split ('_' ))
76
79
77
80
78
- def typespec_to_type (name : str , typespec : dict [str , Any ]) -> tuple [type , dict [str , Any ]]:
81
+ def typespec_to_type (name : str , typespec : dict [str , t . Any ]) -> tuple [type , dict [str , t . Any ]]:
79
82
"""Convert JSON-schema style specification to type and metadata dictionary."""
80
83
tp = typespec .get ('type' )
81
84
if not tp :
@@ -86,12 +89,12 @@ def typespec_to_type(name: str, typespec: dict[str, Any]) -> tuple[type, dict[st
86
89
if properties :
87
90
type_ = create_attrs_class (name , properties = properties , metadata = metadata )
88
91
else :
89
- type_ = dict [str , Any ]
92
+ type_ = dict [str , t . Any ]
90
93
elif tp == 'array' :
91
94
if 'items' in typespec :
92
95
subtype , md = typespec_to_type (name , typespec ['items' ])
93
96
else :
94
- subtype = Any
97
+ subtype = t . Any
95
98
type_ = list [subtype ]
96
99
else :
97
100
type_ = {
@@ -111,8 +114,8 @@ def _type_name(tp: type) -> str:
111
114
112
115
def create_attrs_class (
113
116
class_name : str ,
114
- properties : dict [str , Any ],
115
- metadata : dict [str , Any ],
117
+ properties : dict [str , t . Any ],
118
+ metadata : dict [str , t . Any ],
116
119
) -> type :
117
120
"""Dynamically create an attrs class with the given properties.
118
121
@@ -160,7 +163,7 @@ def create_attrs_class(
160
163
161
164
162
165
def generate_attrs_classes_from_schema (
163
- schema : dict [str , Any ],
166
+ schema : dict [str , t . Any ],
164
167
root_class_name : str ,
165
168
) -> type :
166
169
"""Generate attrs classes from a JSON schema.
@@ -185,7 +188,7 @@ def generate_attrs_classes_from_schema(
185
188
return type_
186
189
187
190
188
- def populate_namespace (attrs_class : type , namespace : dict [str , Any ]) -> None :
191
+ def populate_namespace (attrs_class : type , namespace : dict [str , t . Any ]) -> None :
189
192
"""Populate a namespace with nested attrs classes.
190
193
191
194
Parameters
@@ -205,8 +208,8 @@ def populate_namespace(attrs_class: type, namespace: dict[str, Any]) -> None:
205
208
206
209
207
210
def load_schema_into_namespace (
208
- schema : dict [str , Any ],
209
- namespace : dict [str , Any ],
211
+ schema : dict [str , t . Any ],
212
+ namespace : dict [str , t . Any ],
210
213
root_class_name : str ,
211
214
) -> None :
212
215
"""Load a JSON schema into a namespace as attrs classes.
0 commit comments