Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve empty container types as the generic form of the container #318

Merged
merged 1 commit into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
Loading