pyglove 0.4.5.dev202412100720__py3-none-any.whl → 0.4.5.dev202501250807__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.
Files changed (118) hide show
  1. pyglove/core/__init__.py +40 -21
  2. pyglove/core/coding/__init__.py +42 -0
  3. pyglove/core/coding/errors.py +111 -0
  4. pyglove/core/coding/errors_test.py +98 -0
  5. pyglove/core/coding/execution.py +312 -0
  6. pyglove/core/coding/execution_test.py +333 -0
  7. pyglove/core/{object_utils/codegen.py → coding/function_generation.py} +10 -4
  8. pyglove/core/{object_utils/codegen_test.py → coding/function_generation_test.py} +5 -7
  9. pyglove/core/coding/parsing.py +153 -0
  10. pyglove/core/coding/parsing_test.py +150 -0
  11. pyglove/core/coding/permissions.py +100 -0
  12. pyglove/core/coding/permissions_test.py +93 -0
  13. pyglove/core/geno/base.py +53 -38
  14. pyglove/core/geno/base_test.py +2 -4
  15. pyglove/core/geno/categorical.py +36 -27
  16. pyglove/core/geno/custom.py +18 -15
  17. pyglove/core/geno/numerical.py +19 -16
  18. pyglove/core/geno/space.py +3 -4
  19. pyglove/core/hyper/base.py +6 -6
  20. pyglove/core/hyper/categorical.py +91 -52
  21. pyglove/core/hyper/custom.py +7 -7
  22. pyglove/core/hyper/custom_test.py +9 -10
  23. pyglove/core/hyper/derived.py +30 -22
  24. pyglove/core/hyper/derived_test.py +3 -5
  25. pyglove/core/hyper/dynamic_evaluation.py +3 -4
  26. pyglove/core/hyper/evolvable.py +57 -46
  27. pyglove/core/hyper/numerical.py +48 -24
  28. pyglove/core/hyper/numerical_test.py +9 -9
  29. pyglove/core/hyper/object_template.py +58 -46
  30. pyglove/core/logging_test.py +0 -2
  31. pyglove/core/patching/object_factory.py +4 -4
  32. pyglove/core/patching/pattern_based.py +4 -4
  33. pyglove/core/patching/rule_based.py +4 -3
  34. pyglove/core/symbolic/__init__.py +4 -0
  35. pyglove/core/symbolic/base.py +200 -136
  36. pyglove/core/symbolic/base_test.py +17 -19
  37. pyglove/core/symbolic/boilerplate.py +4 -5
  38. pyglove/core/symbolic/class_wrapper.py +10 -14
  39. pyglove/core/symbolic/class_wrapper_test.py +2 -2
  40. pyglove/core/symbolic/compounding.py +2 -2
  41. pyglove/core/symbolic/compounding_test.py +2 -4
  42. pyglove/core/symbolic/contextual_object.py +288 -0
  43. pyglove/core/symbolic/contextual_object_test.py +327 -0
  44. pyglove/core/symbolic/dict.py +115 -87
  45. pyglove/core/symbolic/dict_test.py +188 -131
  46. pyglove/core/symbolic/diff.py +12 -12
  47. pyglove/core/symbolic/flags.py +1 -1
  48. pyglove/core/symbolic/functor.py +16 -15
  49. pyglove/core/symbolic/functor_test.py +2 -4
  50. pyglove/core/symbolic/inferred.py +2 -2
  51. pyglove/core/symbolic/list.py +70 -47
  52. pyglove/core/symbolic/list_test.py +117 -98
  53. pyglove/core/symbolic/object.py +59 -58
  54. pyglove/core/symbolic/object_test.py +143 -90
  55. pyglove/core/symbolic/origin.py +5 -7
  56. pyglove/core/symbolic/pure_symbolic.py +4 -3
  57. pyglove/core/symbolic/ref.py +33 -16
  58. pyglove/core/symbolic/ref_test.py +17 -0
  59. pyglove/core/tuning/local_backend.py +2 -2
  60. pyglove/core/tuning/protocols.py +3 -3
  61. pyglove/core/typing/annotation_conversion.py +8 -3
  62. pyglove/core/typing/annotation_conversion_test.py +8 -0
  63. pyglove/core/typing/callable_ext.py +11 -13
  64. pyglove/core/typing/callable_signature.py +22 -19
  65. pyglove/core/typing/callable_signature_test.py +3 -5
  66. pyglove/core/typing/class_schema.py +93 -54
  67. pyglove/core/typing/class_schema_test.py +4 -5
  68. pyglove/core/typing/custom_typing.py +5 -4
  69. pyglove/core/typing/key_specs.py +5 -7
  70. pyglove/core/typing/key_specs_test.py +4 -4
  71. pyglove/core/typing/type_conversion.py +4 -5
  72. pyglove/core/typing/type_conversion_test.py +12 -12
  73. pyglove/core/typing/typed_missing.py +6 -7
  74. pyglove/core/typing/typed_missing_test.py +7 -8
  75. pyglove/core/typing/value_specs.py +287 -144
  76. pyglove/core/typing/value_specs_test.py +148 -25
  77. pyglove/core/utils/__init__.py +172 -0
  78. pyglove/core/{object_utils → utils}/common_traits.py +2 -2
  79. pyglove/core/{object_utils → utils}/common_traits_test.py +1 -3
  80. pyglove/core/utils/contextual.py +147 -0
  81. pyglove/core/utils/contextual_test.py +88 -0
  82. pyglove/core/{object_utils → utils}/docstr_utils_test.py +1 -3
  83. pyglove/core/{object_utils → utils}/error_utils.py +3 -3
  84. pyglove/core/{object_utils → utils}/error_utils_test.py +1 -1
  85. pyglove/core/{object_utils → utils}/formatting.py +1 -1
  86. pyglove/core/{object_utils → utils}/formatting_test.py +1 -2
  87. pyglove/core/{object_utils → utils}/hierarchical.py +23 -25
  88. pyglove/core/{object_utils → utils}/hierarchical_test.py +3 -5
  89. pyglove/core/{object_utils → utils}/json_conversion.py +1 -1
  90. pyglove/core/{object_utils → utils}/json_conversion_test.py +1 -3
  91. pyglove/core/{object_utils → utils}/missing.py +2 -2
  92. pyglove/core/{object_utils → utils}/missing_test.py +2 -4
  93. pyglove/core/utils/text_color.py +128 -0
  94. pyglove/core/utils/text_color_test.py +94 -0
  95. pyglove/core/{object_utils → utils}/thread_local_test.py +1 -3
  96. pyglove/core/{object_utils → utils}/timing.py +21 -10
  97. pyglove/core/{object_utils → utils}/timing_test.py +14 -12
  98. pyglove/core/{object_utils → utils}/value_location.py +2 -2
  99. pyglove/core/{object_utils → utils}/value_location_test.py +2 -4
  100. pyglove/core/views/base.py +25 -29
  101. pyglove/core/views/html/base.py +14 -15
  102. pyglove/core/views/html/controls/base.py +5 -5
  103. pyglove/core/views/html/controls/label.py +1 -1
  104. pyglove/core/views/html/controls/label_test.py +6 -6
  105. pyglove/core/views/html/controls/progress_bar.py +3 -5
  106. pyglove/core/views/html/controls/progress_bar_test.py +2 -2
  107. pyglove/core/views/html/controls/tab.py +80 -2
  108. pyglove/core/views/html/controls/tab_test.py +34 -1
  109. pyglove/core/views/html/tree_view.py +39 -37
  110. {pyglove-0.4.5.dev202412100720.dist-info → pyglove-0.4.5.dev202501250807.dist-info}/METADATA +17 -3
  111. pyglove-0.4.5.dev202501250807.dist-info/RECORD +218 -0
  112. {pyglove-0.4.5.dev202412100720.dist-info → pyglove-0.4.5.dev202501250807.dist-info}/WHEEL +1 -1
  113. pyglove/core/object_utils/__init__.py +0 -164
  114. pyglove-0.4.5.dev202412100720.dist-info/RECORD +0 -203
  115. /pyglove/core/{object_utils → utils}/docstr_utils.py +0 -0
  116. /pyglove/core/{object_utils → utils}/thread_local.py +0 -0
  117. {pyglove-0.4.5.dev202412100720.dist-info → pyglove-0.4.5.dev202501250807.dist-info}/LICENSE +0 -0
  118. {pyglove-0.4.5.dev202412100720.dist-info → pyglove-0.4.5.dev202501250807.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,327 @@
1
+ # Copyright 2025 The PyGlove Authors
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+ import inspect
15
+ from typing import Any
16
+ import unittest
17
+ import weakref
18
+
19
+ from pyglove.core import utils as pg_utils
20
+ from pyglove.core.symbolic import contextual_object
21
+
22
+ contextual_override = pg_utils.contextual_override
23
+ ContextualOverride = pg_utils.ContextualOverride
24
+ get_contextual_override = pg_utils.get_contextual_override
25
+ contextual_value = pg_utils.contextual_value
26
+ all_contextual_values = pg_utils.all_contextual_values
27
+
28
+ ContextualObject = contextual_object.ContextualObject
29
+ contextual_attribute = contextual_object.contextual_attribute
30
+
31
+
32
+ class ContextualObjectTest(unittest.TestCase):
33
+ """Tests for ContextualObject."""
34
+
35
+ def test_override(self):
36
+ class A(ContextualObject):
37
+ x: int
38
+
39
+ a = A(x=1)
40
+ with a.override(x=2, y=1):
41
+ self.assertEqual(a.x, 2)
42
+
43
+ # `y`` is not an attribute of `A`.
44
+ with self.assertRaises(AttributeError):
45
+ _ = a.y
46
+
47
+ def test_context(self):
48
+ class A(ContextualObject):
49
+ x: int
50
+ y: int = contextual_attribute()
51
+ z: int = contextual_attribute(default=-1)
52
+
53
+ with self.assertRaisesRegex(TypeError, '.* missing 1 required argument'):
54
+ _ = A()
55
+
56
+ a = A(x=1)
57
+ with self.assertRaisesRegex(
58
+ AttributeError, 'p'
59
+ ):
60
+ _ = a.p
61
+
62
+ with self.assertRaisesRegex(
63
+ AttributeError, '.* is not found under its context'
64
+ ):
65
+ _ = a.y
66
+
67
+ with contextual_override(y=1):
68
+ self.assertEqual(a.y, 1)
69
+
70
+ with self.assertRaisesRegex(
71
+ AttributeError, '.* is not found under its context'
72
+ ):
73
+ _ = a.y
74
+
75
+ # Use contextual default if it's not provided.
76
+ self.assertEqual(a.z, -1)
77
+
78
+ a1 = A(x=1, y=2)
79
+ self.assertEqual(a1.x, 1)
80
+ self.assertEqual(a1.y, 2)
81
+ self.assertEqual(a1.z, -1)
82
+
83
+ with contextual_override(x=3, y=3, z=3) as parent_override:
84
+ self.assertEqual(
85
+ parent_override,
86
+ dict(
87
+ x=ContextualOverride(3, cascade=False, override_attrs=False),
88
+ y=ContextualOverride(3, cascade=False, override_attrs=False),
89
+ z=ContextualOverride(3, cascade=False, override_attrs=False),
90
+ ),
91
+ )
92
+ self.assertEqual(
93
+ get_contextual_override('y'),
94
+ ContextualOverride(3, cascade=False, override_attrs=False),
95
+ )
96
+ self.assertEqual(contextual_value('x'), 3)
97
+ self.assertIsNone(contextual_value('f', None))
98
+ with self.assertRaisesRegex(KeyError, '.* does not exist'):
99
+ contextual_value('f')
100
+
101
+ self.assertEqual(all_contextual_values(), dict(x=3, y=3, z=3))
102
+
103
+ # Member attributes take precedence over `contextual_override`.
104
+ self.assertEqual(a1.x, 1)
105
+ self.assertEqual(a1.y, 2)
106
+
107
+ # Override attributes take precedence over member attribute.
108
+ with a1.override(y=3):
109
+ self.assertEqual(a1.y, 3)
110
+ with a1.override(y=4):
111
+ self.assertEqual(a1.y, 4)
112
+ self.assertEqual(a1.y, 3)
113
+ self.assertEqual(a1.y, 2)
114
+
115
+ # `contextual_override` takes precedence over contextual default.
116
+ self.assertEqual(a1.z, 3)
117
+
118
+ # Test nested contextual override with override_attrs=True (default).
119
+ with contextual_override(
120
+ y=4, z=4, override_attrs=True) as nested_override:
121
+ self.assertEqual(
122
+ nested_override,
123
+ dict(
124
+ x=ContextualOverride(3, cascade=False, override_attrs=False),
125
+ y=ContextualOverride(4, cascade=False, override_attrs=True),
126
+ z=ContextualOverride(4, cascade=False, override_attrs=True),
127
+ ),
128
+ )
129
+
130
+ # Member attribute is not overriden as current scope does not override
131
+ # `x``.
132
+ self.assertEqual(a1.x, 1)
133
+
134
+ # Member attribute is overriden.
135
+ self.assertEqual(a1.y, 4)
136
+
137
+ # `ContextualObject.override` takes precedence over
138
+ # `contextual_override(override_attrs=True)`.
139
+ with a1.override(y=3):
140
+ self.assertEqual(a1.y, 3)
141
+ self.assertEqual(a1.y, 4)
142
+
143
+ # Member default is overriden.
144
+ self.assertEqual(a1.z, 4)
145
+
146
+ self.assertEqual(a1.y, 2)
147
+ self.assertEqual(a1.z, 3)
148
+
149
+ self.assertEqual(a1.y, 2)
150
+ self.assertEqual(a1.z, -1)
151
+
152
+ def test_context_cascade(self):
153
+ class A(ContextualObject):
154
+ x: int
155
+ y: int = contextual_attribute()
156
+ z: int = contextual_attribute(default=-1)
157
+
158
+ a = A(1, 2)
159
+ self.assertEqual(a.x, 1)
160
+ self.assertEqual(a.y, 2)
161
+ self.assertEqual(a.z, -1)
162
+
163
+ with contextual_override(x=3, y=3, z=3, cascade=True):
164
+ self.assertEqual(a.x, 1)
165
+ self.assertEqual(a.y, 2)
166
+ self.assertEqual(a.z, 3)
167
+
168
+ # Outter `pg.contextual_override` takes precedence
169
+ # over inner `pg.contextual_override` when cascade=True.
170
+ with contextual_override(y=4, z=4, cascade=True):
171
+ self.assertEqual(a.x, 1)
172
+ self.assertEqual(a.y, 2)
173
+ self.assertEqual(a.z, 3)
174
+
175
+ with contextual_override(y=4, z=4, override_attrs=True):
176
+ self.assertEqual(a.x, 1)
177
+ self.assertEqual(a.y, 2)
178
+ self.assertEqual(a.z, 3)
179
+
180
+ self.assertEqual(a.x, 1)
181
+ self.assertEqual(a.y, 2)
182
+ self.assertEqual(a.z, -1)
183
+
184
+ with contextual_override(x=3, y=3, z=3, cascade=True, override_attrs=True):
185
+ self.assertEqual(a.x, 3)
186
+ self.assertEqual(a.y, 3)
187
+ self.assertEqual(a.z, 3)
188
+
189
+ with contextual_override(y=4, z=4, override_attrs=True):
190
+ self.assertEqual(a.x, 3)
191
+ self.assertEqual(a.y, 3)
192
+ self.assertEqual(a.z, 3)
193
+
194
+ self.assertEqual(a.x, 1)
195
+ self.assertEqual(a.y, 2)
196
+ self.assertEqual(a.z, -1)
197
+
198
+ def test_sym_inferred(self):
199
+ class A(ContextualObject):
200
+ x: int = 1
201
+ y: int = contextual_attribute()
202
+
203
+ a = A()
204
+ with self.assertRaisesRegex(
205
+ AttributeError, '.* is not found under its context'):
206
+ _ = a.sym_inferred('y')
207
+ self.assertIsNone(a.sym_inferred('y', default=None))
208
+
209
+ with self.assertRaises(AttributeError):
210
+ _ = a.sym_inferred('z')
211
+ self.assertIsNone(a.sym_inferred('z', default=None))
212
+
213
+ def test_weak_ref(self):
214
+ class A(ContextualObject):
215
+ x: int = 1
216
+
217
+ a = A()
218
+ self.assertIsNotNone(weakref.ref(a))
219
+
220
+
221
+ class ContextualAttributeTest(unittest.TestCase):
222
+ """Tests for Component."""
223
+
224
+ def test_contextualibute_access(self):
225
+
226
+ class A(ContextualObject):
227
+ x: int
228
+ y: int = contextual_attribute()
229
+
230
+ # Not okay: `A.x` is required.
231
+ with self.assertRaisesRegex(TypeError, 'missing 1 required argument'):
232
+ _ = A()
233
+
234
+ # Okay: `A.y` is contextual.
235
+ a = A(1)
236
+
237
+ # `a.y` is not yet available from the context.
238
+ with self.assertRaises(AttributeError):
239
+ _ = a.y
240
+
241
+ class B(ContextualObject):
242
+ # Attributes with annotation will be treated as symbolic fields.
243
+ p: int
244
+ q: A = A(2)
245
+ z: int = contextual_attribute()
246
+
247
+ class C(ContextualObject):
248
+ a: int
249
+ b: B = B(2)
250
+ y: int = 1
251
+ z = 2
252
+
253
+ c = C(1)
254
+ b = c.b
255
+ a = b.q
256
+
257
+ # Test symbolic attributes declared from C.
258
+ self.assertTrue(c.sym_hasattr('a'))
259
+ self.assertTrue(c.sym_hasattr('b'))
260
+ self.assertTrue(c.sym_hasattr('y'))
261
+ self.assertFalse(c.sym_hasattr('z'))
262
+
263
+ # Contextual access to c.y from a.
264
+ self.assertEqual(a.y, 1)
265
+ self.assertEqual(b.z, 2)
266
+
267
+ # 'y' is not defined as an attribute in 'B'.
268
+ with self.assertRaises(AttributeError):
269
+ _ = b.y
270
+
271
+ c.rebind(y=2)
272
+ self.assertEqual(c.y, 2)
273
+ self.assertEqual(a.y, 2)
274
+
275
+ c.z = 3
276
+ self.assertEqual(c.z, 3)
277
+ self.assertEqual(b.z, 3)
278
+
279
+ def test_to_html(self):
280
+ class A(ContextualObject):
281
+ x: int = 1
282
+ y: int = contextual_attribute()
283
+
284
+ def assert_content(html, expected):
285
+ expected = inspect.cleandoc(expected).strip()
286
+ actual = html.content.strip()
287
+ if actual != expected:
288
+ print(actual)
289
+ self.assertEqual(actual.strip(), expected)
290
+
291
+ self.assertIn(
292
+ inspect.cleandoc(
293
+ """
294
+ .contextual-attribute {
295
+ color: purple;
296
+ }
297
+ .unavailable-contextual {
298
+ color: gray;
299
+ font-style: italic;
300
+ }
301
+ """
302
+ ),
303
+ A().to_html().style_section,
304
+ )
305
+
306
+ assert_content(
307
+ A().to_html(enable_summary_tooltip=False),
308
+ """
309
+ <details open class="pyglove a"><summary><div class="summary-title">A(...)</div></summary><div class="complex-value a"><details open class="pyglove int"><summary><div class="summary-name">x<span class="tooltip">x</span></div><div class="summary-title">int</div></summary><span class="simple-value int">1</span></details><details open class="pyglove contextual-attribute"><summary><div class="summary-name">y<span class="tooltip">y</span></div><div class="summary-title">ContextualAttribute(...)</div></summary><div class="unavailable-contextual">(not available)</div></details></div></details>
310
+ """
311
+ )
312
+
313
+ class B(ContextualObject):
314
+ z: Any
315
+ y: int = 2
316
+
317
+ b = B(A())
318
+ assert_content(
319
+ b.z.to_html(enable_summary_tooltip=False),
320
+ """
321
+ <details open class="pyglove a"><summary><div class="summary-title">A(...)</div></summary><div class="complex-value a"><details open class="pyglove int"><summary><div class="summary-name">x<span class="tooltip">x</span></div><div class="summary-title">int</div></summary><span class="simple-value int">1</span></details><details open class="pyglove contextual-attribute"><summary><div class="summary-name">y<span class="tooltip">y</span></div><div class="summary-title">ContextualAttribute(...)</div></summary><span class="simple-value int">2</span></details></div></details>
322
+ """
323
+ )
324
+
325
+
326
+ if __name__ == '__main__':
327
+ unittest.main()