diff --git a/csp/impl/types/instantiation_type_resolver.py b/csp/impl/types/instantiation_type_resolver.py index 7945b84fb..3e1e06343 100644 --- a/csp/impl/types/instantiation_type_resolver.py +++ b/csp/impl/types/instantiation_type_resolver.py @@ -557,10 +557,7 @@ def _resolve_tvar_container_internal_types(self, tvar, container_typ, arg, raise else: return False if len(arg) == 0: - if raise_on_error: - raise ContainerTypeVarResolutionError(self._function_name, tvar, arg) - else: - return None + return container_typ res = None if isinstance(arg, set): first_val = arg.__iter__().__next__() diff --git a/csp/tests/test_type_checking.py b/csp/tests/test_type_checking.py index 88ef15588..2bb638f9d 100644 --- a/csp/tests/test_type_checking.py +++ b/csp/tests/test_type_checking.py @@ -314,13 +314,8 @@ def graph(): }, ) l_good = csp.const.using(T={int: float})({}) - with self.assertRaisesRegex( - TypeError, - "Unable to resolve container type for type variable T explicit value must have" - + " uniform values and be non empty.*", - ): - # Passing a Dummy value instead of expected float - l_bad = csp.const({}) + l_also_good = csp.const({}) + self.assertEqual(l_also_good.tstype.typ, dict) l_good = csp.const.using(T={int: float})({2: 1}) l_good = csp.const.using(T={int: float})({2: 1.0}) @@ -377,13 +372,8 @@ def graph(): typed_ts_and_scalar_generic(l_i, [1, 2], TestTypeChecking.Dummy()) l_good = csp.const.using(T=[int])([]) - with self.assertRaisesRegex( - TypeError, - "Unable to resolve container type for type variable T explicit value must have" - + " uniform values and be non empty.*", - ): - # Passing a Dummy value instead of expected float - l_bad = csp.const([]) + l_also_good = csp.const([]) + self.assertEqual(l_also_good.tstype.typ, list) csp.run(graph, starttime=datetime(2020, 2, 7, 9), endtime=datetime(2020, 2, 7, 9, 1)) @@ -434,13 +424,8 @@ def graph(): typed_ts_and_scalar_generic(l_i, {1, 2}, TestTypeChecking.Dummy()) l_good = csp.const.using(T={int})(set()) - with self.assertRaisesRegex( - TypeError, - "Unable to resolve container type for type variable T explicit value must have" - + " uniform values and be non empty.*", - ): - # Passing a Dummy value instead of expected float - l_bad = csp.const({}) + l_also_good = csp.const(set()) + self.assertEqual(l_also_good.tstype.typ, set) csp.run(graph, starttime=datetime(2020, 2, 7, 9), endtime=datetime(2020, 2, 7, 9, 1)) @@ -612,6 +597,21 @@ def test_pickle_type_resolver_errors(self): pickled = pickle.loads(pickle.dumps(err)) self.assertEqual(str(err), str(pickled)) + def test_empty_containers(self): + def g(): + x = csp.const([]) + y = csp.const(set()) + z = csp.const(dict()) + + csp.add_graph_output("x", x) + csp.add_graph_output("y", y) + csp.add_graph_output("z", z) + + res = csp.run(g, starttime=datetime(2020, 1, 1), endtime=timedelta()) + self.assertEqual(res["x"][0][1], []) + self.assertEqual(res["y"][0][1], set()) + self.assertEqual(res["z"][0][1], {}) + if __name__ == "__main__": unittest.main()