@@ -332,6 +332,71 @@ def count(x: ts[int]) -> ts[int]:
332332 result = csp .run (count , x , starttime = datetime (2020 , 2 , 7 , 9 ), endtime = timedelta (seconds = 10 ))[0 ]
333333 self .assertEqual ([v [1 ] for v in result ], list (x * 2 for x in range (1 , 11 )))
334334
335+ def test_csp_output_dict_unpack (self ):
336+ @csp .node
337+ def foo (x : ts [bool ]) -> csp .Outputs (a = ts [int ], b = ts [int ]):
338+ if csp .ticked (x ):
339+ values = {"a" : 1 , "b" : 2 }
340+ csp .output (** values )
341+
342+ result = csp .run (foo , csp .const (True ), starttime = datetime (2020 , 1 , 1 ), endtime = timedelta (seconds = 1 ))
343+ self .assertEqual (result ["a" ][0 ][1 ], 1 )
344+ self .assertEqual (result ["b" ][0 ][1 ], 2 )
345+
346+ def test_csp_output_dict_unpack_mixed (self ):
347+ @csp .node
348+ def foo (x : ts [bool ]) -> csp .Outputs (a = ts [int ], b = ts [int ]):
349+ if csp .ticked (x ):
350+ csp .output (a = 1 , ** {"b" : 2 })
351+
352+ result = csp .run (foo , csp .const (True ), starttime = datetime (2020 , 1 , 1 ), endtime = timedelta (seconds = 1 ))
353+ self .assertEqual (result ["a" ][0 ][1 ], 1 )
354+ self .assertEqual (result ["b" ][0 ][1 ], 2 )
355+
356+ def test_csp_output_dict_unpack_multiple (self ):
357+ @csp .node
358+ def foo (x : ts [bool ]) -> csp .Outputs (a = ts [int ], b = ts [int ]):
359+ if csp .ticked (x ):
360+ csp .output (** {"a" : 1 }, ** {"b" : 2 })
361+
362+ result = csp .run (foo , csp .const (True ), starttime = datetime (2020 , 1 , 1 ), endtime = timedelta (seconds = 1 ))
363+ self .assertEqual (result ["a" ][0 ][1 ], 1 )
364+ self .assertEqual (result ["b" ][0 ][1 ], 2 )
365+
366+ def test_csp_output_dict_unpack_unknown_key (self ):
367+ @csp .node
368+ def foo (x : ts [bool ]) -> csp .Outputs (a = ts [int ]):
369+ if csp .ticked (x ):
370+ csp .output (** {"bogus" : 1 })
371+
372+ with self .assertRaisesRegex (KeyError , "unrecognized output 'bogus'" ):
373+ csp .run (foo , csp .const (True ), starttime = datetime (2020 , 1 , 1 ), endtime = timedelta (seconds = 1 ))
374+
375+ def test_csp_output_dict_unpack_non_dict (self ):
376+ @csp .node
377+ def foo (x : ts [bool ]) -> csp .Outputs (a = ts [int ], b = ts [int ]):
378+ if csp .ticked (x ):
379+ values = [1 , 2 ] # not dict-like
380+ csp .output (** values )
381+
382+ with self .assertRaisesRegex (TypeError , "requires a dict-like value" ):
383+ csp .run (foo , csp .const (True ), starttime = datetime (2020 , 1 , 1 ), endtime = timedelta (seconds = 1 ))
384+
385+ def test_csp_output_dict_unpack_basket (self ):
386+ @csp .node
387+ def foo (x : ts [bool ]) -> csp .Outputs (
388+ a = ts [int ],
389+ b = csp .OutputBasket (Dict [str , ts [int ]], shape = ["k1" , "k2" ]),
390+ ):
391+ if csp .ticked (x ):
392+ values = {"a" : 1 , "b" : {"k1" : 10 , "k2" : 20 }}
393+ csp .output (** values )
394+
395+ result = csp .run (foo , csp .const (True ), starttime = datetime (2020 , 1 , 1 ), endtime = timedelta (seconds = 1 ))
396+ self .assertEqual (result ["a" ][0 ][1 ], 1 )
397+ self .assertEqual (result ["b[k1]" ][0 ][1 ], 10 )
398+ self .assertEqual (result ["b[k2]" ][0 ][1 ], 20 )
399+
335400 def test_single_csp_numpy_output (self ):
336401 @csp .node
337402 def count (x : ts [int ]) -> ts [int ]:
0 commit comments