File tree 2 files changed +22
-10
lines changed
2 files changed +22
-10
lines changed Original file line number Diff line number Diff line change @@ -112,6 +112,7 @@ def value_of(
112
112
Raises:
113
113
RecursionError: If the ParamResolver detects a loop in recursive
114
114
resolution.
115
+ sympy.SympifyError: If the resulting value cannot be interpreted.
115
116
"""
116
117
117
118
# Input is a pass through type, no resolution needed: return early
@@ -179,7 +180,12 @@ def value_of(
179
180
if not recursive :
180
181
# Resolves one step at a time. For example:
181
182
# a.subs({a: b, b: c}) == b
183
+ #
184
+ # Note that a sympy.SympifyError here likely means
185
+ # that one of the expressions was not parsable by sympy
186
+ # (such as a function returning NotImplemented)
182
187
v = value .subs (self .param_dict , simultaneous = True )
188
+
183
189
if v .free_symbols :
184
190
return v
185
191
elif sympy .im (v ):
Original file line number Diff line number Diff line change @@ -227,25 +227,31 @@ class Foo:
227
227
def _resolved_value_ (self ):
228
228
return self
229
229
230
- class Bar :
231
- def _resolved_value_ (self ):
232
- return NotImplemented
233
-
234
230
class Baz :
235
231
def _resolved_value_ (self ):
236
232
return 'Baz'
237
233
238
234
foo = Foo ()
239
- bar = Bar ()
240
235
baz = Baz ()
241
236
242
237
a = sympy .Symbol ('a' )
243
- b = sympy .Symbol ('b' )
244
- c = sympy .Symbol ('c' )
245
- r = cirq .ParamResolver ({a : foo , b : bar , c : baz })
238
+ b = sympy .Symbol ('c' )
239
+ r = cirq .ParamResolver ({a : foo , b : baz })
246
240
assert r .value_of (a ) is foo
247
- assert r .value_of (b ) is b
248
- assert r .value_of (c ) == 'Baz'
241
+ assert r .value_of (b ) == 'Baz'
242
+
243
+
244
+ @pytest .mark .xfail (reason = 'this test requires sympy 1.12' , strict = True )
245
+ def test_custom_value_not_implemented ():
246
+ class Bar :
247
+ def _resolved_value_ (self ):
248
+ return NotImplemented
249
+
250
+ b = sympy .Symbol ('b' )
251
+ bar = Bar ()
252
+ r = cirq .ParamResolver ({b : bar })
253
+ with pytest .raises (sympy .SympifyError ):
254
+ _ = r .value_of (b )
249
255
250
256
251
257
def test_compose ():
You can’t perform that action at this time.
0 commit comments