pyglove 0.4.5.dev20240318__py3-none-any.whl → 0.4.5.dev202501132210__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pyglove/core/__init__.py +54 -20
- pyglove/core/coding/__init__.py +42 -0
- pyglove/core/coding/errors.py +111 -0
- pyglove/core/coding/errors_test.py +98 -0
- pyglove/core/coding/execution.py +309 -0
- pyglove/core/coding/execution_test.py +333 -0
- pyglove/core/{object_utils/codegen.py → coding/function_generation.py} +10 -4
- pyglove/core/{object_utils/codegen_test.py → coding/function_generation_test.py} +5 -7
- pyglove/core/coding/parsing.py +153 -0
- pyglove/core/coding/parsing_test.py +150 -0
- pyglove/core/coding/permissions.py +100 -0
- pyglove/core/coding/permissions_test.py +93 -0
- pyglove/core/geno/base.py +54 -41
- pyglove/core/geno/base_test.py +2 -4
- pyglove/core/geno/categorical.py +37 -28
- pyglove/core/geno/custom.py +19 -16
- pyglove/core/geno/numerical.py +20 -17
- pyglove/core/geno/space.py +4 -5
- pyglove/core/hyper/base.py +6 -6
- pyglove/core/hyper/categorical.py +94 -55
- pyglove/core/hyper/custom.py +7 -7
- pyglove/core/hyper/custom_test.py +9 -10
- pyglove/core/hyper/derived.py +30 -22
- pyglove/core/hyper/derived_test.py +2 -4
- pyglove/core/hyper/dynamic_evaluation.py +5 -6
- pyglove/core/hyper/evolvable.py +57 -46
- pyglove/core/hyper/numerical.py +48 -24
- pyglove/core/hyper/numerical_test.py +9 -9
- pyglove/core/hyper/object_template.py +58 -46
- pyglove/core/io/__init__.py +1 -0
- pyglove/core/io/file_system.py +17 -7
- pyglove/core/io/file_system_test.py +2 -0
- pyglove/core/io/sequence.py +299 -0
- pyglove/core/io/sequence_test.py +124 -0
- pyglove/core/logging_test.py +0 -2
- pyglove/core/patching/object_factory.py +4 -4
- pyglove/core/patching/pattern_based.py +4 -4
- pyglove/core/patching/rule_based.py +17 -5
- pyglove/core/patching/rule_based_test.py +27 -4
- pyglove/core/symbolic/__init__.py +2 -7
- pyglove/core/symbolic/base.py +320 -183
- pyglove/core/symbolic/base_test.py +123 -19
- pyglove/core/symbolic/boilerplate.py +7 -13
- pyglove/core/symbolic/boilerplate_test.py +25 -23
- pyglove/core/symbolic/class_wrapper.py +48 -45
- pyglove/core/symbolic/class_wrapper_test.py +2 -2
- pyglove/core/symbolic/compounding.py +9 -15
- pyglove/core/symbolic/compounding_test.py +2 -4
- pyglove/core/symbolic/dict.py +154 -110
- pyglove/core/symbolic/dict_test.py +238 -130
- pyglove/core/symbolic/diff.py +199 -10
- pyglove/core/symbolic/diff_test.py +226 -0
- pyglove/core/symbolic/flags.py +1 -1
- pyglove/core/symbolic/functor.py +29 -26
- pyglove/core/symbolic/functor_test.py +102 -50
- pyglove/core/symbolic/inferred.py +2 -2
- pyglove/core/symbolic/list.py +81 -50
- pyglove/core/symbolic/list_test.py +119 -97
- pyglove/core/symbolic/object.py +225 -113
- pyglove/core/symbolic/object_test.py +320 -108
- pyglove/core/symbolic/origin.py +17 -14
- pyglove/core/symbolic/origin_test.py +4 -2
- pyglove/core/symbolic/pure_symbolic.py +4 -3
- pyglove/core/symbolic/ref.py +108 -21
- pyglove/core/symbolic/ref_test.py +93 -0
- pyglove/core/symbolic/symbolize_test.py +10 -2
- pyglove/core/tuning/local_backend.py +2 -2
- pyglove/core/tuning/protocols.py +3 -3
- pyglove/core/tuning/sample_test.py +3 -3
- pyglove/core/typing/__init__.py +14 -5
- pyglove/core/typing/annotation_conversion.py +43 -27
- pyglove/core/typing/annotation_conversion_test.py +23 -0
- pyglove/core/typing/callable_ext.py +241 -3
- pyglove/core/typing/callable_ext_test.py +255 -0
- pyglove/core/typing/callable_signature.py +510 -66
- pyglove/core/typing/callable_signature_test.py +619 -99
- pyglove/core/typing/class_schema.py +229 -154
- pyglove/core/typing/class_schema_test.py +149 -95
- pyglove/core/typing/custom_typing.py +5 -4
- pyglove/core/typing/inspect.py +63 -0
- pyglove/core/typing/inspect_test.py +39 -0
- pyglove/core/typing/key_specs.py +10 -11
- pyglove/core/typing/key_specs_test.py +7 -4
- pyglove/core/typing/type_conversion.py +4 -5
- pyglove/core/typing/type_conversion_test.py +12 -12
- pyglove/core/typing/typed_missing.py +6 -7
- pyglove/core/typing/typed_missing_test.py +7 -8
- pyglove/core/typing/value_specs.py +604 -362
- pyglove/core/typing/value_specs_test.py +328 -90
- pyglove/core/utils/__init__.py +164 -0
- pyglove/core/{object_utils → utils}/common_traits.py +3 -67
- pyglove/core/utils/common_traits_test.py +36 -0
- pyglove/core/{object_utils → utils}/docstr_utils.py +23 -0
- pyglove/core/{object_utils → utils}/docstr_utils_test.py +36 -4
- pyglove/core/{object_utils → utils}/error_utils.py +78 -9
- pyglove/core/{object_utils → utils}/error_utils_test.py +61 -5
- pyglove/core/utils/formatting.py +464 -0
- pyglove/core/utils/formatting_test.py +453 -0
- pyglove/core/{object_utils → utils}/hierarchical.py +23 -25
- pyglove/core/{object_utils → utils}/hierarchical_test.py +3 -5
- pyglove/core/{object_utils → utils}/json_conversion.py +177 -52
- pyglove/core/{object_utils → utils}/json_conversion_test.py +97 -16
- pyglove/core/{object_utils → utils}/missing.py +3 -3
- pyglove/core/{object_utils → utils}/missing_test.py +2 -4
- pyglove/core/utils/text_color.py +128 -0
- pyglove/core/utils/text_color_test.py +94 -0
- pyglove/core/{object_utils → utils}/thread_local_test.py +1 -3
- pyglove/core/utils/timing.py +236 -0
- pyglove/core/utils/timing_test.py +154 -0
- pyglove/core/{object_utils → utils}/value_location.py +275 -6
- pyglove/core/utils/value_location_test.py +707 -0
- pyglove/core/views/__init__.py +32 -0
- pyglove/core/views/base.py +804 -0
- pyglove/core/views/base_test.py +580 -0
- pyglove/core/views/html/__init__.py +27 -0
- pyglove/core/views/html/base.py +547 -0
- pyglove/core/views/html/base_test.py +830 -0
- pyglove/core/views/html/controls/__init__.py +35 -0
- pyglove/core/views/html/controls/base.py +275 -0
- pyglove/core/views/html/controls/label.py +207 -0
- pyglove/core/views/html/controls/label_test.py +157 -0
- pyglove/core/views/html/controls/progress_bar.py +183 -0
- pyglove/core/views/html/controls/progress_bar_test.py +97 -0
- pyglove/core/views/html/controls/tab.py +320 -0
- pyglove/core/views/html/controls/tab_test.py +87 -0
- pyglove/core/views/html/controls/tooltip.py +99 -0
- pyglove/core/views/html/controls/tooltip_test.py +99 -0
- pyglove/core/views/html/tree_view.py +1517 -0
- pyglove/core/views/html/tree_view_test.py +1461 -0
- {pyglove-0.4.5.dev20240318.dist-info → pyglove-0.4.5.dev202501132210.dist-info}/METADATA +18 -4
- pyglove-0.4.5.dev202501132210.dist-info/RECORD +214 -0
- {pyglove-0.4.5.dev20240318.dist-info → pyglove-0.4.5.dev202501132210.dist-info}/WHEEL +1 -1
- pyglove/core/object_utils/__init__.py +0 -154
- pyglove/core/object_utils/common_traits_test.py +0 -82
- pyglove/core/object_utils/formatting.py +0 -234
- pyglove/core/object_utils/formatting_test.py +0 -223
- pyglove/core/object_utils/value_location_test.py +0 -385
- pyglove/core/symbolic/schema_utils.py +0 -327
- pyglove/core/symbolic/schema_utils_test.py +0 -57
- pyglove/core/typing/class_schema_utils.py +0 -202
- pyglove/core/typing/class_schema_utils_test.py +0 -194
- pyglove-0.4.5.dev20240318.dist-info/RECORD +0 -185
- /pyglove/core/{object_utils → utils}/thread_local.py +0 -0
- {pyglove-0.4.5.dev20240318.dist-info → pyglove-0.4.5.dev202501132210.dist-info}/LICENSE +0 -0
- {pyglove-0.4.5.dev20240318.dist-info → pyglove-0.4.5.dev202501132210.dist-info}/top_level.txt +0 -0
pyglove/core/symbolic/functor.py
CHANGED
@@ -22,15 +22,14 @@ import types
|
|
22
22
|
import typing
|
23
23
|
from typing import Any, Callable, Dict, List, Optional, Set, Tuple, Type, Union
|
24
24
|
|
25
|
-
from pyglove.core import object_utils
|
26
25
|
from pyglove.core import typing as pg_typing
|
26
|
+
from pyglove.core import utils
|
27
27
|
from pyglove.core.symbolic import base
|
28
28
|
from pyglove.core.symbolic import flags
|
29
29
|
from pyglove.core.symbolic import object as pg_object
|
30
|
-
from pyglove.core.symbolic import schema_utils
|
31
30
|
|
32
31
|
|
33
|
-
class Functor(pg_object.Object,
|
32
|
+
class Functor(pg_object.Object, utils.Functor):
|
34
33
|
"""Symbolic functions (Functors).
|
35
34
|
|
36
35
|
A symbolic function is a symbolic class with a ``__call__`` method, whose
|
@@ -99,7 +98,7 @@ class Functor(pg_object.Object, object_utils.Functor):
|
|
99
98
|
# Update the return value of subclassed functors.
|
100
99
|
if cls.is_subclassed_functor: # pylint: disable=using-constant-test
|
101
100
|
private_call_signature = pg_typing.Signature.from_callable(
|
102
|
-
cls._call, auto_typing=True
|
101
|
+
cls._call, auto_typing=True, auto_doc=True,
|
103
102
|
)
|
104
103
|
if (
|
105
104
|
len(private_call_signature.args) > 1
|
@@ -125,7 +124,7 @@ class Functor(pg_object.Object, object_utils.Functor):
|
|
125
124
|
if not hasattr(cls, '__orig_init__'):
|
126
125
|
setattr(cls, '__orig_init__', cls.__init__)
|
127
126
|
|
128
|
-
@
|
127
|
+
@utils.explicit_method_override
|
129
128
|
@functools.wraps(pseudo_init)
|
130
129
|
def _init(self, *args, **kwargs):
|
131
130
|
self.__class__.__orig_init__(self, *args, **kwargs)
|
@@ -149,14 +148,15 @@ class Functor(pg_object.Object, object_utils.Functor):
|
|
149
148
|
return instance()
|
150
149
|
return instance
|
151
150
|
|
152
|
-
@
|
151
|
+
@utils.explicit_method_override
|
153
152
|
def __init__(
|
154
153
|
self,
|
155
154
|
*args,
|
156
|
-
root_path: Optional[
|
155
|
+
root_path: Optional[utils.KeyPath] = None,
|
157
156
|
override_args: bool = False,
|
158
157
|
ignore_extra_args: bool = False,
|
159
|
-
**kwargs
|
158
|
+
**kwargs,
|
159
|
+
):
|
160
160
|
"""Constructor.
|
161
161
|
|
162
162
|
Args:
|
@@ -183,8 +183,8 @@ class Functor(pg_object.Object, object_utils.Functor):
|
|
183
183
|
varargs = list(args[len(signature.args) :])
|
184
184
|
args = args[: len(signature.args)]
|
185
185
|
else:
|
186
|
-
arg_phrase =
|
187
|
-
was_phrase =
|
186
|
+
arg_phrase = utils.auto_plural(len(signature.args), 'argument')
|
187
|
+
was_phrase = utils.auto_plural(len(args), 'was', 'were')
|
188
188
|
raise TypeError(
|
189
189
|
f'{signature.id}() takes {len(signature.args)} '
|
190
190
|
f'positional {arg_phrase} but {len(args)} {was_phrase} given.'
|
@@ -258,8 +258,7 @@ class Functor(pg_object.Object, object_utils.Functor):
|
|
258
258
|
# pylint: enable=protected-access
|
259
259
|
return typing.cast(Functor, other)
|
260
260
|
|
261
|
-
def _on_change(
|
262
|
-
self, field_updates: Dict[object_utils.KeyPath, base.FieldUpdate]):
|
261
|
+
def _on_change(self, field_updates: Dict[utils.KeyPath, base.FieldUpdate]):
|
263
262
|
"""Custom handling field change to update bound args."""
|
264
263
|
for relative_path, update in field_updates.items():
|
265
264
|
assert relative_path
|
@@ -376,7 +375,7 @@ class Functor(pg_object.Object, object_utils.Functor):
|
|
376
375
|
if (
|
377
376
|
signature.return_value
|
378
377
|
and flags.is_type_check_enabled()
|
379
|
-
and
|
378
|
+
and pg_typing.MISSING_VALUE != return_value
|
380
379
|
):
|
381
380
|
return_value = signature.return_value.apply(
|
382
381
|
return_value, root_path=self.sym_path + 'returns'
|
@@ -407,8 +406,8 @@ class Functor(pg_object.Object, object_utils.Functor):
|
|
407
406
|
if ignore_extra_args:
|
408
407
|
args = args[: len(signature.args)]
|
409
408
|
else:
|
410
|
-
arg_phrase =
|
411
|
-
was_phrase =
|
409
|
+
arg_phrase = utils.auto_plural(len(signature.args), 'argument')
|
410
|
+
was_phrase = utils.auto_plural(len(args), 'was', 'were')
|
412
411
|
raise TypeError(
|
413
412
|
f'{signature.id}() takes {len(signature.args)} '
|
414
413
|
f'positional {arg_phrase} but {len(args)} {was_phrase} given.'
|
@@ -426,7 +425,7 @@ class Functor(pg_object.Object, object_utils.Functor):
|
|
426
425
|
varargs = list(args[len(signature.args) :])
|
427
426
|
if flags.is_type_check_enabled():
|
428
427
|
varargs = [
|
429
|
-
signature.varargs.value_spec.apply(
|
428
|
+
signature.varargs.value_spec.element.value.apply(
|
430
429
|
v, root_path=self.sym_path + signature.varargs.name
|
431
430
|
)
|
432
431
|
for v in varargs
|
@@ -484,9 +483,10 @@ class Functor(pg_object.Object, object_utils.Functor):
|
|
484
483
|
missing_required_arg_names.append(arg.name)
|
485
484
|
|
486
485
|
if missing_required_arg_names:
|
487
|
-
arg_phrase =
|
488
|
-
len(missing_required_arg_names), 'argument'
|
489
|
-
|
486
|
+
arg_phrase = utils.auto_plural(
|
487
|
+
len(missing_required_arg_names), 'argument'
|
488
|
+
)
|
489
|
+
args_str = utils.comma_delimited_str(missing_required_arg_names)
|
490
490
|
raise TypeError(
|
491
491
|
f'{signature.id}() missing {len(missing_required_arg_names)} '
|
492
492
|
f'required positional {arg_phrase}: {args_str}.'
|
@@ -571,10 +571,11 @@ def functor(
|
|
571
571
|
|
572
572
|
def functor_class(
|
573
573
|
func: types.FunctionType,
|
574
|
-
args:
|
575
|
-
|
576
|
-
|
577
|
-
|
574
|
+
args: Union[
|
575
|
+
List[Union[pg_typing.Field, pg_typing.FieldDef]],
|
576
|
+
Dict[pg_typing.FieldKeyDef, pg_typing.FieldValueDef],
|
577
|
+
None
|
578
|
+
] = None,
|
578
579
|
returns: Optional[pg_typing.ValueSpec] = None,
|
579
580
|
base_class: Optional[Type[Functor]] = None,
|
580
581
|
*,
|
@@ -684,9 +685,11 @@ def functor_class(
|
|
684
685
|
cls.auto_register = True
|
685
686
|
|
686
687
|
# Apply function schema.
|
687
|
-
|
688
|
-
|
689
|
-
|
688
|
+
cls.apply_schema(
|
689
|
+
pg_typing.schema(
|
690
|
+
func, args, returns, auto_doc=auto_doc, auto_typing=auto_typing
|
691
|
+
)
|
692
|
+
)
|
690
693
|
|
691
694
|
# Register functor class for deserialization if needed.
|
692
695
|
if add_to_registry:
|
@@ -11,15 +11,13 @@
|
|
11
11
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
12
|
# See the License for the specific language governing permissions and
|
13
13
|
# limitations under the License.
|
14
|
-
"""Tests for pyglove.Functor."""
|
15
|
-
|
16
14
|
import inspect
|
17
15
|
import io
|
18
16
|
import typing
|
19
17
|
import unittest
|
20
18
|
|
21
|
-
from pyglove.core import object_utils
|
22
19
|
from pyglove.core import typing as pg_typing
|
20
|
+
from pyglove.core import utils
|
23
21
|
from pyglove.core.symbolic import flags
|
24
22
|
from pyglove.core.symbolic.base import from_json_str as pg_from_json_str
|
25
23
|
from pyglove.core.symbolic.dict import Dict
|
@@ -31,7 +29,7 @@ from pyglove.core.symbolic.object import members as pg_members
|
|
31
29
|
from pyglove.core.symbolic.object import Object
|
32
30
|
|
33
31
|
|
34
|
-
MISSING_VALUE =
|
32
|
+
MISSING_VALUE = utils.MISSING_VALUE
|
35
33
|
|
36
34
|
|
37
35
|
class FunctorTest(unittest.TestCase):
|
@@ -108,19 +106,41 @@ class FunctorTest(unittest.TestCase):
|
|
108
106
|
self.assertEqual(
|
109
107
|
f.__signature__.args,
|
110
108
|
[
|
111
|
-
pg_typing.Argument(
|
112
|
-
|
109
|
+
pg_typing.Argument(
|
110
|
+
'a',
|
111
|
+
pg_typing.Argument.Kind.POSITIONAL_OR_KEYWORD,
|
112
|
+
pg_typing.Any()
|
113
|
+
),
|
114
|
+
pg_typing.Argument(
|
115
|
+
'b',
|
116
|
+
pg_typing.Argument.Kind.POSITIONAL_OR_KEYWORD,
|
117
|
+
pg_typing.Any()
|
118
|
+
),
|
113
119
|
],
|
114
120
|
)
|
115
121
|
self.assertEqual(
|
116
|
-
f.__signature__.varargs,
|
122
|
+
f.__signature__.varargs,
|
123
|
+
pg_typing.Argument(
|
124
|
+
'args',
|
125
|
+
pg_typing.Argument.Kind.VAR_POSITIONAL,
|
126
|
+
pg_typing.List(pg_typing.Any())
|
127
|
+
)
|
117
128
|
)
|
118
129
|
self.assertEqual(
|
119
|
-
f.__signature__.varkw,
|
130
|
+
f.__signature__.varkw,
|
131
|
+
pg_typing.Argument(
|
132
|
+
'kwargs',
|
133
|
+
pg_typing.Argument.Kind.VAR_KEYWORD,
|
134
|
+
pg_typing.Dict(pg_typing.Any())
|
135
|
+
)
|
120
136
|
)
|
121
137
|
self.assertEqual(
|
122
138
|
f.__signature__.kwonlyargs,
|
123
|
-
[pg_typing.Argument(
|
139
|
+
[pg_typing.Argument(
|
140
|
+
'c',
|
141
|
+
pg_typing.Argument.Kind.KEYWORD_ONLY,
|
142
|
+
pg_typing.Any(default=0)
|
143
|
+
)],
|
124
144
|
)
|
125
145
|
self.assertIsNone(f.__signature__.return_value, None)
|
126
146
|
self.assertTrue(f.__signature__.has_varargs)
|
@@ -148,8 +168,16 @@ class FunctorTest(unittest.TestCase):
|
|
148
168
|
self.assertEqual(
|
149
169
|
f.__signature__.args,
|
150
170
|
[
|
151
|
-
pg_typing.Argument(
|
152
|
-
|
171
|
+
pg_typing.Argument(
|
172
|
+
'a',
|
173
|
+
pg_typing.Argument.Kind.POSITIONAL_OR_KEYWORD,
|
174
|
+
pg_typing.Int(default=1)
|
175
|
+
),
|
176
|
+
pg_typing.Argument(
|
177
|
+
'b',
|
178
|
+
pg_typing.Argument.Kind.POSITIONAL_OR_KEYWORD,
|
179
|
+
pg_typing.Int(default=2)
|
180
|
+
),
|
153
181
|
],
|
154
182
|
)
|
155
183
|
self.assertEqual(
|
@@ -191,17 +219,36 @@ class FunctorTest(unittest.TestCase):
|
|
191
219
|
],
|
192
220
|
)
|
193
221
|
self.assertEqual(
|
194
|
-
f.__signature__.args,
|
222
|
+
f.__signature__.args,
|
223
|
+
[pg_typing.Argument(
|
224
|
+
'a',
|
225
|
+
pg_typing.Argument.Kind.POSITIONAL_OR_KEYWORD,
|
226
|
+
pg_typing.Int()
|
227
|
+
)]
|
195
228
|
)
|
196
229
|
self.assertEqual(
|
197
|
-
f.__signature__.varargs,
|
230
|
+
f.__signature__.varargs,
|
231
|
+
pg_typing.Argument(
|
232
|
+
'args',
|
233
|
+
pg_typing.Argument.Kind.VAR_POSITIONAL,
|
234
|
+
pg_typing.List(pg_typing.Any())
|
235
|
+
)
|
198
236
|
)
|
199
237
|
self.assertEqual(
|
200
238
|
f.__signature__.kwonlyargs,
|
201
|
-
[pg_typing.Argument(
|
239
|
+
[pg_typing.Argument(
|
240
|
+
'b',
|
241
|
+
pg_typing.Argument.Kind.KEYWORD_ONLY,
|
242
|
+
pg_typing.Int(default=2)
|
243
|
+
)],
|
202
244
|
)
|
203
245
|
self.assertEqual(
|
204
|
-
f.__signature__.varkw,
|
246
|
+
f.__signature__.varkw,
|
247
|
+
pg_typing.Argument(
|
248
|
+
'kwargs',
|
249
|
+
pg_typing.Argument.Kind.VAR_KEYWORD,
|
250
|
+
pg_typing.Dict(pg_typing.Any())
|
251
|
+
)
|
205
252
|
)
|
206
253
|
self.assertEqual(f.__signature__.return_value, pg_typing.Int())
|
207
254
|
|
@@ -253,9 +300,21 @@ class FunctorTest(unittest.TestCase):
|
|
253
300
|
self.assertEqual(
|
254
301
|
f.__signature__.args,
|
255
302
|
[
|
256
|
-
pg_typing.Argument(
|
257
|
-
|
258
|
-
|
303
|
+
pg_typing.Argument(
|
304
|
+
'a',
|
305
|
+
pg_typing.Argument.Kind.POSITIONAL_OR_KEYWORD,
|
306
|
+
pg_typing.Int()
|
307
|
+
),
|
308
|
+
pg_typing.Argument(
|
309
|
+
'b',
|
310
|
+
pg_typing.Argument.Kind.POSITIONAL_OR_KEYWORD,
|
311
|
+
pg_typing.Any(default=1)
|
312
|
+
),
|
313
|
+
pg_typing.Argument(
|
314
|
+
'c',
|
315
|
+
pg_typing.Argument.Kind.POSITIONAL_OR_KEYWORD,
|
316
|
+
pg_typing.Int(default=1)
|
317
|
+
),
|
259
318
|
],
|
260
319
|
)
|
261
320
|
self.assertFalse(f.__signature__.has_varargs)
|
@@ -287,20 +346,31 @@ class FunctorTest(unittest.TestCase):
|
|
287
346
|
def _call(self) -> int:
|
288
347
|
return self.x + self.y
|
289
348
|
|
290
|
-
print(Foo.__signature__)
|
291
349
|
self.assertEqual(Foo.__signature__.name, '__call__')
|
292
350
|
self.assertEqual(Foo.__signature__.qualname, Foo.__qualname__)
|
293
351
|
self.assertEqual(Foo.__signature__.module_name, Foo.__module__)
|
294
352
|
self.assertEqual(
|
295
353
|
Foo.__signature__.args,
|
296
354
|
[
|
297
|
-
pg_typing.Argument(
|
298
|
-
|
355
|
+
pg_typing.Argument(
|
356
|
+
'x',
|
357
|
+
pg_typing.Argument.Kind.POSITIONAL_OR_KEYWORD,
|
358
|
+
pg_typing.Int()
|
359
|
+
),
|
360
|
+
pg_typing.Argument(
|
361
|
+
'y',
|
362
|
+
pg_typing.Argument.Kind.POSITIONAL_OR_KEYWORD,
|
363
|
+
pg_typing.Int()
|
364
|
+
),
|
299
365
|
],
|
300
366
|
)
|
301
367
|
self.assertEqual(
|
302
368
|
Foo.__signature__.varkw,
|
303
|
-
pg_typing.Argument(
|
369
|
+
pg_typing.Argument(
|
370
|
+
'kwargs',
|
371
|
+
pg_typing.Argument.Kind.VAR_KEYWORD,
|
372
|
+
pg_typing.Dict(pg_typing.Any(annotation=typing.Any)),
|
373
|
+
),
|
304
374
|
)
|
305
375
|
self.assertEqual(Foo.__signature__.return_value, pg_typing.Int())
|
306
376
|
|
@@ -422,7 +492,10 @@ class FunctorTest(unittest.TestCase):
|
|
422
492
|
f = pg_as_functor(lambda x: x)
|
423
493
|
self.assertIsInstance(f, Functor)
|
424
494
|
self.assertEqual(
|
425
|
-
f.__signature__.args,
|
495
|
+
f.__signature__.args,
|
496
|
+
[pg_typing.Argument(
|
497
|
+
'x', pg_typing.Argument.Kind.POSITIONAL_OR_KEYWORD, pg_typing.Any()
|
498
|
+
)]
|
426
499
|
)
|
427
500
|
self.assertIsNone(f.__signature__.return_value)
|
428
501
|
self.assertEqual(f(1), 1)
|
@@ -453,41 +526,20 @@ class FunctorTest(unittest.TestCase):
|
|
453
526
|
# `pg.functor` decorator found extra symbolic argument.
|
454
527
|
with self.assertRaisesRegex(
|
455
528
|
ValueError,
|
456
|
-
'
|
457
|
-
|
529
|
+
'Variable positional argument .* should have a value of .*List'
|
530
|
+
):
|
458
531
|
@pg_functor([('args', pg_typing.Int())])
|
459
532
|
def f3(*args): # pylint: disable=unused-variable
|
460
533
|
del args
|
461
534
|
|
462
|
-
# `pg.functor` decorator has multiple StrKey.
|
463
|
-
with self.assertRaisesRegex(
|
464
|
-
KeyError,
|
465
|
-
'.* multiple StrKey found in symbolic arguments declaration.'):
|
466
|
-
@pg_functor([
|
467
|
-
('a', pg_typing.Int()),
|
468
|
-
(pg_typing.StrKey(), pg_typing.Any()),
|
469
|
-
(pg_typing.StrKey(), pg_typing.Any()),
|
470
|
-
]) # pylint: disable=unused-variable
|
471
|
-
def f4(a, **kwargs):
|
472
|
-
del a, kwargs
|
473
|
-
|
474
|
-
with self.assertRaisesRegex(
|
475
|
-
KeyError, '.* multiple symbolic fields found for argument.'):
|
476
|
-
@pg_functor([
|
477
|
-
('a', pg_typing.Int()),
|
478
|
-
('a', pg_typing.Str()),
|
479
|
-
]) # pylint: disable=unused-variable
|
480
|
-
def f5(a):
|
481
|
-
del a
|
482
|
-
|
483
535
|
with self.assertRaisesRegex(
|
484
536
|
ValueError,
|
485
|
-
'
|
486
|
-
|
537
|
+
'The annotated default value .* is not equal'
|
538
|
+
):
|
487
539
|
@pg_functor([
|
488
540
|
('a', pg_typing.Int(default=2)),
|
489
541
|
]) # pylint: disable=unused-variable
|
490
|
-
def
|
542
|
+
def f4(a=1):
|
491
543
|
del a
|
492
544
|
|
493
545
|
with self.assertRaisesRegex(
|
@@ -495,7 +547,7 @@ class FunctorTest(unittest.TestCase):
|
|
495
547
|
@pg_functor([
|
496
548
|
('a', pg_typing.Int()),
|
497
549
|
], returns=pg_typing.Any(default=None))
|
498
|
-
def
|
550
|
+
def f5(a=1): # pylint: disable=unused-variable
|
499
551
|
del a
|
500
552
|
|
501
553
|
def test_bad_call(self):
|
@@ -14,8 +14,8 @@
|
|
14
14
|
"""Common inferential values."""
|
15
15
|
|
16
16
|
from typing import Any, Tuple
|
17
|
-
from pyglove.core import object_utils
|
18
17
|
from pyglove.core import typing as pg_typing
|
18
|
+
from pyglove.core import utils
|
19
19
|
from pyglove.core.symbolic import base
|
20
20
|
from pyglove.core.symbolic.object import Object
|
21
21
|
|
@@ -65,7 +65,7 @@ class ValueFromParentChain(InferredValue):
|
|
65
65
|
if v == pg_typing.MISSING_VALUE:
|
66
66
|
if parent is None:
|
67
67
|
raise AttributeError(
|
68
|
-
|
68
|
+
utils.message_on_path(
|
69
69
|
(
|
70
70
|
f'`{self.inference_key}` is not found under its context '
|
71
71
|
'(along its symbolic parent chain).'
|