-
Notifications
You must be signed in to change notification settings - Fork 19
/
schema_utils.py
336 lines (298 loc) · 12.7 KB
/
schema_utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
# Copyright 2022 The PyGlove Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Utilities for handling schema for symbolic classes."""
import types
from typing import Any, Dict, List, Optional, Sequence, Tuple, Union
from pyglove.core import object_utils
from pyglove.core import typing as pg_typing
from pyglove.core.symbolic import base
from pyglove.core.symbolic import flags
def augment_schema(
schema: pg_typing.Schema,
fields: List[
Union[
pg_typing.Field,
Tuple[Union[str, pg_typing.KeySpec], pg_typing.ValueSpec, str],
Tuple[Union[str, pg_typing.KeySpec], pg_typing.ValueSpec, str, Any],
]
],
extend: bool = True,
*,
init_arg_list: Optional[Sequence[str]] = None,
metadata: Optional[Dict[str, Any]] = None,
description: Optional[str] = None,
) -> pg_typing.Schema:
"""Gets the augmented schema from class with extra fields and metadata.
Args:
schema: The original schema.
fields: A list of `pg.typing.Field` or equivalent tuple representation as
(<key>, <value-spec>, [description], [metadata-objects]). `key` should be
a string. `value-spec` should be pg_typing.ValueSpec classes or
equivalent, e.g. primitive values which will be converted to ValueSpec
implementation according to its type and used as its default value.
`description` is optional only when field overrides a field from its
parent class. `metadata-objects` is an optional list of any type, which
can be used to generate code according to the schema.
extend: If True, extend existing schema using `fields`. Otherwise replace
existing schema with a new schema created from `fields`.
init_arg_list: An optional sequence of strings as the positional argument
list for `__init__`. This is helpful when symbolic attributes are
inherited from base classes or the user want to change its order. If not
provided, the `init_arg_list` will be automatically generated from
symbolic attributes defined from ``pg.members`` in their declaration
order, from the base classes to the subclass.
metadata: Optional dict of user objects as class-level metadata which will
be attached to class schema.
description: An optional description to set.
Returns:
The augmented schema (new copy).
"""
metadata = metadata or {}
if init_arg_list is None:
init_arg_list = metadata.get('init_arg_list', None)
metadata = object_utils.merge([schema.metadata, metadata])
# NOTE(daiyip): Consider to inherit `init_arg_list` from the parent when
# there is no new field.
metadata['init_arg_list'] = init_arg_list
return formalize_schema(
pg_typing.create_schema(
fields=fields,
name=schema.name,
base_schema_list=[schema] if extend else [],
description=description or schema.description,
allow_nonconst_keys=True,
metadata=metadata,
)
)
def update_schema(
cls,
fields: List[
Union[
pg_typing.Field,
Tuple[Union[str, pg_typing.KeySpec], pg_typing.ValueSpec, str],
Tuple[Union[str, pg_typing.KeySpec], pg_typing.ValueSpec, str, Any],
]
],
extend: bool = True,
*,
init_arg_list: Optional[Sequence[str]] = None,
metadata: Optional[Dict[str, Any]] = None,
description: Optional[str] = None,
serialization_key: Optional[str] = None,
additional_keys: Optional[List[str]] = None,
add_to_registry: bool = True,
) -> None:
"""Updates the schema for a ``pg.Object`` subclass.
This function allows the user to update the symbolic fields associated
with a symbolic class. It was intended to support meta-programming
scenarios in which symbolic fields are dynamically generated.
Example::
class A(pg.Object):
pass
# Add symbolic field 'x' to class A.
pg.symbolic.update_schema(A, [
('x', schema.Int())
])
# B inherits the symbolic field 'x' from A.
class B(A):
pass
# Wipe out the symbolic field 'x' from B.
pg.symbolic.update_schema(B, [], extend=False)
See also: :func:`pyglove.members`, :func:`pyglove.functor` and
:func:`pyglove.symbolize`.
Args:
cls: A symbolic Object subclass.
fields: A list of `pg.typing.Field` or equivalent tuple representation as
(<key>, <value-spec>, [description], [metadata-objects]). `key` should be
a string. `value-spec` should be pg_typing.ValueSpec classes or
equivalent, e.g. primitive values which will be converted to ValueSpec
implementation according to its type and used as its default value.
`description` is optional only when field overrides a field from its
parent class. `metadata-objects` is an optional list of any type, which
can be used to generate code according to the schema.
extend: If True, extend existing schema using `fields`. Otherwise replace
existing schema with a new schema created from `fields`.
init_arg_list: An optional sequence of strings as the positional argument
list for `__init__`. This is helpful when symbolic attributes are
inherited from base classes or the user want to change its order. If not
provided, the `init_arg_list` will be automatically generated from
symbolic attributes defined from ``pg.members`` in their declaration
order, from the base classes to the subclass.
metadata: Optional dict of user objects as class-level metadata which will
be attached to class schema.
description: An optional description to set.
serialization_key: An optional string to be used as the serialization key
for the class during `sym_jsonify`. If None, `cls.__type_name__` will be
used. This is introduced for scenarios when we want to relocate a class,
before the downstream can recognize the new location, we need the class to
serialize it using previous key.
additional_keys: An optional list of strings as additional keys to
deserialize an object of the registered class. This can be useful when we
need to relocate or rename the registered class while being able to load
existing serialized JSON values.
add_to_registry: If True, the newly created functor class will be added to
the registry for deserialization.
"""
cls.apply_schema(
augment_schema(
cls.__schema__,
fields=fields,
extend=extend,
init_arg_list=init_arg_list,
metadata=metadata,
description=description,
)
)
if add_to_registry:
cls.register_for_deserialization(serialization_key, additional_keys)
def callable_schema(
func: types.FunctionType,
args: Optional[
List[
Union[
Tuple[Tuple[str, pg_typing.KeySpec], pg_typing.ValueSpec, str],
Tuple[
Tuple[str, pg_typing.KeySpec], pg_typing.ValueSpec, str, Any
],
]
]
] = None, # pylint: disable=bad-continuation
returns: Optional[pg_typing.ValueSpec] = None,
*,
auto_typing: bool = True,
auto_doc: bool = True,
remove_self: bool = False,
) -> pg_typing.Schema:
"""Returns the schema from the signature of a callable."""
args_docstr = None
description = None
if auto_doc:
docstr = object_utils.docstr(func)
if docstr:
args_docstr = docstr.args
description = schema_description_from_docstr(docstr)
signature = pg_typing.get_signature(func, auto_typing=auto_typing)
arg_fields = pg_typing.get_arg_fields(signature, args, args_docstr)
if returns is not None and pg_typing.MISSING_VALUE != returns.default:
raise ValueError('return value spec should not have default value.')
returns = returns or signature.return_value
if remove_self and arg_fields and arg_fields[0].key == 'self':
arg_fields.pop(0)
# Generate init_arg_list from signature.
init_arg_list = [arg.name for arg in signature.args]
if signature.varargs:
init_arg_list.append(f'*{signature.varargs.name}')
# Decide schema name.
module_name = getattr(func, '__module__', None)
func_name = func.__qualname__
schema_name = f'{module_name}.{func_name}' if module_name else func_name
return formalize_schema(
pg_typing.create_schema(
fields=arg_fields,
name=schema_name,
metadata={
'init_arg_list': init_arg_list,
'varargs_name': getattr(signature.varargs, 'name', None),
'varkw_name': getattr(signature.varkw, 'name', None),
'returns': returns,
},
description=description,
allow_nonconst_keys=True,
)
)
def validate_init_arg_list(
init_arg_list: List[str], cls_schema: pg_typing.Schema) -> None:
"""Validate init arg list."""
for i, arg in enumerate(init_arg_list):
is_vararg = False
if i == len(init_arg_list) - 1 and arg.startswith('*'):
arg = arg[1:]
is_vararg = True
field = cls_schema.get_field(arg)
if field is None:
raise TypeError(
f'Argument {arg!r} from `init_arg_list` is not defined as a '
f'symbolic field. init_arg_list={init_arg_list!r}.')
if is_vararg and not isinstance(field.value, pg_typing.List):
raise TypeError(
f'Variable positional argument {arg!r} should be declared with '
f'`pg.typing.List(...)`. Encountered {field.value!r}.')
def auto_init_arg_list(cls):
"""Generate the init_arg_list metadata from an pg.Object subclass."""
# Inherit from the first non-empty base if they have the same signature.
# This allows to bypass interface-only bases.
init_arg_list = None
for base_cls in cls.__bases__:
schema = getattr(base_cls, '__schema__', None)
if isinstance(schema, pg_typing.Schema):
if list(schema.keys()) == list(cls.__schema__.keys()):
init_arg_list = base_cls.init_arg_list
else:
break
if init_arg_list is None:
# Automatically generate from the field definitions in their
# declaration order from base classes to subclasses.
init_arg_list = [
str(key)
for key in cls.__schema__.fields.keys()
if isinstance(key, pg_typing.ConstStrKey)
]
return init_arg_list
def formalize_schema(schema: pg_typing.Schema) -> pg_typing.Schema: # pylint: disable=redefined-outer-name
"""Formalize default values in the schema."""
def _formalize_field(path: object_utils.KeyPath, node: Any) -> bool:
"""Formalize field."""
if isinstance(node, pg_typing.Field):
field = node
if (not flags.is_empty_field_description_allowed()
and not field.description):
raise ValueError(
f'Field description must not be empty (path={path}).')
field.value.set_default(
field.apply(
field.default_value,
allow_partial=True,
transform_fn=base.symbolic_transform_fn(allow_partial=True)),
use_default_apply=False)
if isinstance(field.value, pg_typing.Dict):
if field.value.schema is not None:
field.value.schema.set_name(f'{schema.name}.{path.path}')
object_utils.traverse(field.value.schema.fields, _formalize_field,
None, path)
elif isinstance(field.value, pg_typing.List):
_formalize_field(object_utils.KeyPath(0, path), field.value.element)
elif isinstance(field.value, pg_typing.Tuple):
for i, elem in enumerate(field.value.elements):
_formalize_field(object_utils.KeyPath(i, path), elem)
elif isinstance(field.value, pg_typing.Union):
for i, c in enumerate(field.value.candidates):
_formalize_field(
object_utils.KeyPath(i, path),
pg_typing.Field(field.key, c, 'Union sub-type.'))
return True
object_utils.traverse(schema.fields, _formalize_field)
return schema
def schema_description_from_docstr(
docstr: Optional[object_utils.DocStr],
include_long_description: bool = False) -> Optional[str]:
"""Gets schema description from DocStr."""
if docstr is None:
return None
description = docstr.short_description or ''
if include_long_description:
if docstr.blank_after_short_description:
description += '\n'
if docstr.long_description:
description += '\n' + docstr.long_description
return description.rstrip('\n')