Skip to content

Commit

Permalink
Resolve empty container types as the generic form of the container (#318
Browse files Browse the repository at this point in the history
)

Signed-off-by: Adam Glustein <[email protected]>
  • Loading branch information
AdamGlustein committed Jul 11, 2024
1 parent 841f355 commit 48d8de6
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 25 deletions.
5 changes: 1 addition & 4 deletions csp/impl/types/instantiation_type_resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__()
Expand Down
42 changes: 21 additions & 21 deletions csp/tests/test_type_checking.py
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand Down Expand Up @@ -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))

Expand Down Expand Up @@ -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))

Expand Down Expand Up @@ -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()

0 comments on commit 48d8de6

Please sign in to comment.