Skip to content

Commit 3a0e759

Browse files
jnthntatumcopybara-github
authored andcommitted
Fix cel-python type(...) tests and run cel-python TAP tests on cel-cpp changes.
PiperOrigin-RevId: 1015048185
1 parent 79f3d98 commit 3a0e759

2 files changed

Lines changed: 19 additions & 17 deletions

File tree

‎cel_expr_python/cel_test.py‎

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -681,25 +681,27 @@ def testTypeType(self):
681681
cel.Type.Type(cel.Type.STRING).is_assignable_from(expr.return_type())
682682
)
683683

684-
# The following two tests demonstrate a quirk of CEL assignability
685-
# rules. In the case of type checking, `type(1)` is deemed to be
686-
# *not* assignable to Type.STRING. However, in runtime use, it would be seen
687-
# as assignable.
684+
# When the branches of a conditional produce type values whose parameters
685+
# are not co-assignable, the checker unifies them to `type(dyn)`.
686+
expr = self.env.compile("var_bool ? type('a') : type(1)")
687+
self.assertEqual(expr.return_type(), cel.Type.Type(cel.Type.DYN))
688+
self.assertFalse(
689+
cel.Type.Type(cel.Type.STRING).is_assignable_from(expr.return_type())
690+
)
691+
688692
res = self._eval(
689693
"var_bool ? type('a') : type(1)",
690694
{"var_bool": True},
691-
expected_return_type=cel.Type.Type(cel.Type.STRING),
695+
expected_return_type=cel.Type.Type(cel.Type.DYN),
692696
)
693697
self.assertEqual(res.value(), cel.Type.STRING)
694698

695699
res = self._eval(
696700
"var_bool ? type('a') : type(1)",
697701
{"var_bool": False},
698-
expected_return_type=cel.Type.Type(cel.Type.STRING),
702+
expected_return_type=cel.Type.Type(cel.Type.DYN),
699703
)
700-
self.assertEqual(
701-
res.value(), cel.Type.INT
702-
) # This behavior is counterintuitive but works as implemented.
704+
self.assertEqual(res.value(), cel.Type.INT)
703705

704706
def testTypeInitSignature(self):
705707
self.assertEqual(cel.Type("int"), cel.Type.INT)

‎conformance/conformance_test.py‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,6 @@ class ConformanceTestSuite(unittest.TestSuite):
7878
"proto3/set_null/map_timestamp_null_pruned",
7979
"proto3/set_null/map_duration_null_pruned",
8080
"proto3/set_null/map_wrapper_null_pruned",
81-
# TODO(b/577138668): C++ type checker bug, though lower priority.
82-
"type_deduction/type_decay/type_list_decay",
83-
"type_deduction/type_decay/type_ternary_decay",
84-
"type_deduction/type_decay/type_free_param_list_decay",
85-
"type_deduction/type_decay/type_free_param_list_decay_order_independent",
86-
"type_deduction/type_decay/type_nested_list_decay",
87-
"type_deduction/type_decay/type_free_param_list_concat",
8881
# Skip until duration/timestamp range and proto wrapper validation is updated for spec parity.
8982
"timestamps/timestamp_arithmetic/add_duration_to_duration_max",
9083
"timestamps/timestamp_arithmetic/subtract_duration_from_duration_min",
@@ -317,8 +310,13 @@ def _convert_type(self, tp: checked_pb.Type) -> cel.Type:
317310
for param in tp.abstract_type.parameter_types
318311
],
319312
)
313+
elif kind == "type":
314+
# An unset type parameter denotes the unparameterized `type`.
315+
if tp.type.WhichOneof("type_kind") is None:
316+
return cel.Type.TYPE
317+
return cel.Type.Type(self._convert_type(tp.type))
320318
else:
321-
self.fail("Unsupported type: " + str(type))
319+
self.fail("Unsupported type: " + str(tp))
322320

323321
def _convert_value(self, value: value_pb.Value) -> Any:
324322
if value.HasField("null_value"):
@@ -503,6 +501,8 @@ def assertType(self, result_type: cel.Type, expected_type: checked_pb.Type):
503501
],
504502
),
505503
)
504+
elif kind == "type":
505+
self.assertEqual(result_type, self._convert_type(expected_type))
506506
else:
507507
self.fail("Unsupported type validation: " + str(expected_type))
508508

0 commit comments

Comments
 (0)