File tree Expand file tree Collapse file tree 2 files changed +22
-10
lines changed
Expand file tree Collapse file tree 2 files changed +22
-10
lines changed Original file line number Diff line number Diff line change @@ -112,6 +112,7 @@ def value_of(
112112 Raises:
113113 RecursionError: If the ParamResolver detects a loop in recursive
114114 resolution.
115+ sympy.SympifyError: If the resulting value cannot be interpreted.
115116 """
116117
117118 # Input is a pass through type, no resolution needed: return early
@@ -179,7 +180,12 @@ def value_of(
179180 if not recursive :
180181 # Resolves one step at a time. For example:
181182 # 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)
182187 v = value .subs (self .param_dict , simultaneous = True )
188+
183189 if v .free_symbols :
184190 return v
185191 elif sympy .im (v ):
Original file line number Diff line number Diff line change @@ -227,25 +227,31 @@ class Foo:
227227 def _resolved_value_ (self ):
228228 return self
229229
230- class Bar :
231- def _resolved_value_ (self ):
232- return NotImplemented
233-
234230 class Baz :
235231 def _resolved_value_ (self ):
236232 return 'Baz'
237233
238234 foo = Foo ()
239- bar = Bar ()
240235 baz = Baz ()
241236
242237 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 })
246240 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 )
249255
250256
251257def test_compose ():
You can’t perform that action at this time.
0 commit comments