@@ -552,46 +552,93 @@ def addplusplus(a, b, c=0):
552552 assert results ["sum" ] == sum (named_inputs .values ())
553553
554554
555- def test_sideffects ():
556- # Function without return value.
557- def extend (box ):
558- box .extend ([1 , 2 ])
555+ # Function without return value.
556+ def _box_extend (box , * args ):
557+ box .extend ([1 , 2 ])
559558
560- def increment (box ):
561- for i in range (len (box )):
562- box [i ] += 1
563559
564- # Designate `a`, `b` as sideffect inp/out arguments.
565- graph = compose ("mygraph" )(
566- operation (
567- name = "extend" ,
568- needs = ["box" , sideffect ("a" )],
569- provides = [sideffect ("b" )],
570- )(extend ),
571- operation (
572- name = "increment" ,
573- needs = ["box" , sideffect ("b" )],
574- provides = sideffect ("c" ),
575- )(increment ),
576- )
560+ def _box_increment (box ):
561+ for i in range (len (box )):
562+ box [i ] += 1
563+
577564
578- assert graph ({"box" : [0 ], "a" : True })["box" ] == [1 , 2 , 3 ]
565+ @pytest .mark .parametrize ("bools" , range (4 ))
566+ def test_sideffect_no_real_data (bools ):
567+ reverse = bools >> 0 & 1
568+ parallel = bools >> 1 & 1
579569
580- # Reverse order of functions.
581- graph = compose ("mygraph" )(
570+ ops = [
582571 operation (
583- name = "increment" ,
584- needs = ["box" , sideffect ("a" )],
585- provides = sideffect ("b" ),
586- )(increment ),
572+ name = "extend" , needs = ["box" , sideffect ("a" )], provides = [sideffect ("b" )]
573+ )(_box_extend ),
587574 operation (
588- name = "extend" ,
589- needs = ["box" , sideffect ("b" )],
590- provides = [sideffect ("c" )],
591- )(extend ),
592- )
575+ name = "increment" , needs = ["box" , sideffect ("b" )], provides = sideffect ("c" )
576+ )(_box_increment ),
577+ ]
578+ if reverse :
579+ ops = reversed (ops )
580+ # Designate `a`, `b` as sideffect inp/out arguments.
581+ graph = compose ("mygraph" )(* ops )
582+ if parallel :
583+ graph .set_execution_method ("parallel" )
584+
585+ # Normal data must not match sideffects
586+ with pytest .raises (ValueError , match = "Unknown output node" ):
587+ graph ({"box" : [0 ], "a" : True }, outputs = ["a" ])
588+ with pytest .raises (ValueError , match = "Unknown output node" ):
589+ graph ({"box" : [0 ], "a" : True }, outputs = ["b" ])
590+
591+ sol = graph ({"box" : [0 ], "a" : True })
592+ # Nothing run if no sideffect inputs given.
593+ assert not graph .net .last_plan .executed
594+ assert sol == {"box" : [0 ], "a" : True }
595+
596+ # Nothing run if no sideffect inputs given.
597+ sol = graph ({"box" : [0 ], "a" : True }, outputs = ["box" , sideffect ("b" )])
598+ assert not graph .net .last_plan .executed
599+ assert sol == {"box" : [0 ]}
600+
601+ ## OK INPUT SIDEFFECTS
602+ #
603+ # ok, no asked out
604+ sol = graph ({"box" : [0 ], sideffect ("a" ): True })
605+ assert sol == {"box" : [1 , 2 , 3 ], sideffect ("a" ): True }
606+ #
607+ # bad, not asked the out-sideffect
608+ sol = graph ({"box" : [0 ], sideffect ("a" ): True }, "box" )
609+ assert sol == {"box" : [0 ]}
610+ #
611+ # ok, asked the 1st out-sideffect
612+ sol = graph ({"box" : [0 ], sideffect ("a" ): True }, ["box" , sideffect ("b" )])
613+ assert sol == {"box" : [0 , 1 , 2 ]}
614+ #
615+ # ok, asked the 2nd out-sideffect
616+ sol = graph ({"box" : [0 ], sideffect ("a" ): True }, ["box" , sideffect ("c" )])
617+ assert sol == {"box" : [1 , 2 , 3 ]}
618+
619+
620+ @pytest .mark .parametrize ("bools" , range (4 ))
621+ def test_sideffect_real_input (bools ):
622+ reverse = bools >> 0 & 1
623+ parallel = bools >> 1 & 1
624+
625+ ops = [
626+ operation (name = "extend" , needs = ["box" , "a" ], provides = [sideffect ("b" )])(
627+ _box_extend
628+ ),
629+ operation (name = "increment" , needs = ["box" , sideffect ("b" )], provides = "c" )(
630+ _box_increment
631+ ),
632+ ]
633+ if reverse :
634+ ops = reversed (ops )
635+ # Designate `a`, `b` as sideffect inp/out arguments.
636+ graph = compose ("mygraph" )(* ops )
637+ if parallel :
638+ graph .set_execution_method ("parallel" )
593639
594- assert graph ({"box" : [0 ], "a" : None })["box" ] == [1 , 1 , 2 ]
640+ assert graph ({"box" : [0 ], "a" : True }) == {"a" : True , "box" : [1 , 2 , 3 ], "c" : None }
641+ assert graph ({"box" : [0 ], "a" : True }, ["box" , "c" ]) == {"box" : [1 , 2 , 3 ], "c" : None }
595642
596643
597644@pytest .mark .xfail (
0 commit comments