Skip to content

Commit 989e75f

Browse files
dmitriplotnikovcopybara-github
authored andcommitted
Rename "py_cel.Cel" class to "py_cel.Env" for consistency
Documentation of CEL ubiquitously refers to this object as "enviroment". The API should reflect this convention. PiperOrigin-RevId: 853942563
1 parent ff7bb6c commit 989e75f

21 files changed

Lines changed: 603 additions & 591 deletions

BUILD

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ exports_files(["LICENSE"])
1212
pybind_extension(
1313
name = "py_cel",
1414
srcs = [
15-
"py_cel.cc",
16-
"py_cel.h",
1715
"py_cel_activation.cc",
1816
"py_cel_activation.h",
1917
"py_cel_arena.cc",
2018
"py_cel_arena.h",
2119
"py_cel_env.cc",
2220
"py_cel_env.h",
21+
"py_cel_env_internal.cc",
22+
"py_cel_env_internal.h",
2323
"py_cel_expression.cc",
2424
"py_cel_expression.h",
2525
"py_cel_function.cc",

conformance/conformance_test.py

Lines changed: 53 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
from cel.expr import value_pb2 as value_pb
2828
from absl.testing import absltest
2929
from python.runfiles import runfiles
30-
import py_cel as cel
30+
import py_cel
3131
from ext import ext_bindings
3232
from ext import ext_encoders
3333
from ext import ext_math
@@ -158,14 +158,14 @@ def _run_conformance_test(self, simple_test: simple_pb.SimpleTest):
158158
break
159159

160160
self.descriptor_pool = descriptor_pool.Default()
161-
self.cel = cel.Cel(
161+
self.env = py_cel.NewEnv(
162162
self.descriptor_pool,
163163
variables=decls,
164164
extensions=extensions,
165165
container=simple_test.container,
166166
)
167167
try:
168-
compiled_expr = self.cel.compile(
168+
compiled_expr = self.env.compile(
169169
simple_test.expr, disable_check=simple_test.disable_check
170170
)
171171
except Exception as e: # pylint: disable=broad-except
@@ -188,7 +188,7 @@ def _run_conformance_test(self, simple_test: simple_pb.SimpleTest):
188188
for key, value in simple_test.bindings.items():
189189
values[key] = self._convert_value(value.value)
190190

191-
act = self.cel.Activation(values)
191+
act = self.env.Activation(values)
192192
try:
193193
res = compiled_expr.eval(act)
194194
except Exception as e: # pylint: disable=broad-except
@@ -208,7 +208,7 @@ def assertEvalResult(self, result, simple_test: simple_pb.SimpleTest):
208208
self.fail(
209209
"Multiple errors not supported: " + str(simple_test.eval_error)
210210
)
211-
self.assertEqual(result.type(), cel.Type.ERROR)
211+
self.assertEqual(result.type(), py_cel.Type.ERROR)
212212
# At this point, error messages from the C++ side are not the same as
213213
# those in the conformance tests.
214214
# self.assertIn(
@@ -220,41 +220,41 @@ def assertEvalResult(self, result, simple_test: simple_pb.SimpleTest):
220220

221221
def _convert_primitive_type(self, primitive: checked_pb.Type.PrimitiveType):
222222
if primitive == checked_pb.Type.PrimitiveType.BOOL:
223-
return cel.Type.BOOL
223+
return py_cel.Type.BOOL
224224
elif primitive == checked_pb.Type.PrimitiveType.INT64:
225-
return cel.Type.INT
225+
return py_cel.Type.INT
226226
elif primitive == checked_pb.Type.PrimitiveType.UINT64:
227-
return cel.Type.UINT
227+
return py_cel.Type.UINT
228228
elif primitive == checked_pb.Type.PrimitiveType.DOUBLE:
229-
return cel.Type.DOUBLE
229+
return py_cel.Type.DOUBLE
230230
elif primitive == checked_pb.Type.PrimitiveType.STRING:
231-
return cel.Type.STRING
231+
return py_cel.Type.STRING
232232
elif primitive == checked_pb.Type.PrimitiveType.BYTES:
233-
return cel.Type.BYTES
233+
return py_cel.Type.BYTES
234234
else:
235235
self.fail("Unsupported primitive type: " + str(primitive))
236236

237237
def _convert_type(self, tp: checked_pb.Type):
238238
kind = tp.WhichOneof("type_kind")
239239
if kind == "dyn":
240-
return cel.Type.DYN
240+
return py_cel.Type.DYN
241241
elif kind == "null":
242-
return cel.Type.NULL
242+
return py_cel.Type.NULL
243243
elif kind == "primitive":
244244
return self._convert_primitive_type(tp.primitive)
245245
elif kind == "wrapper":
246246
return self._convert_primitive_type(tp.wrapper)
247247
elif kind == "message_type":
248-
return cel.Type(tp.message_type)
248+
return py_cel.Type(tp.message_type)
249249
elif kind == "list_type":
250-
return cel.Type.List(self._convert_type(tp.list_type.elem_type))
250+
return py_cel.Type.List(self._convert_type(tp.list_type.elem_type))
251251
elif kind == "map_type":
252-
return cel.Type.Map(
252+
return py_cel.Type.Map(
253253
self._convert_type(tp.map_type.key_type),
254254
self._convert_type(tp.map_type.value_type),
255255
)
256256
elif kind == "abstract_type":
257-
return cel.Type.AbstractType(
257+
return py_cel.Type.AbstractType(
258258
tp.abstract_type.name,
259259
[
260260
self._convert_type(param)
@@ -300,42 +300,42 @@ def _convert_value(self, value: value_pb.Value):
300300

301301
def _type_by_name(self, type_name: str):
302302
if type_name == "dyn":
303-
return cel.Type.DYN
303+
return py_cel.Type.DYN
304304
elif type_name == "null_type":
305-
return cel.Type.NULL
305+
return py_cel.Type.NULL
306306
elif type_name == "bool":
307-
return cel.Type.BOOL
307+
return py_cel.Type.BOOL
308308
elif type_name == "int":
309-
return cel.Type.INT
309+
return py_cel.Type.INT
310310
elif type_name == "uint":
311-
return cel.Type.UINT
311+
return py_cel.Type.UINT
312312
elif type_name == "double":
313-
return cel.Type.DOUBLE
313+
return py_cel.Type.DOUBLE
314314
elif type_name == "string":
315-
return cel.Type.STRING
315+
return py_cel.Type.STRING
316316
elif type_name == "bytes":
317-
return cel.Type.BYTES
317+
return py_cel.Type.BYTES
318318
elif type_name == "list":
319-
return cel.Type.LIST
319+
return py_cel.Type.LIST
320320
elif type_name == "map":
321-
return cel.Type.MAP
321+
return py_cel.Type.MAP
322322
elif type_name == "timestamp" or type_name == "google.protobuf.Timestamp":
323-
return cel.Type.TIMESTAMP
323+
return py_cel.Type.TIMESTAMP
324324
elif type_name == "duration" or type_name == "google.protobuf.Duration":
325-
return cel.Type.DURATION
325+
return py_cel.Type.DURATION
326326
elif type_name == "type":
327-
return cel.Type.TYPE
327+
return py_cel.Type.TYPE
328328
else:
329329
desc = self.descriptor_pool.FindMessageTypeByName(type_name)
330330
if desc is not None:
331-
return cel.Type(type_name)
331+
return py_cel.Type(type_name)
332332
desc = self.descriptor_pool.FindEnumTypeByName(type_name)
333333
if desc is not None:
334-
return cel.Type(type_name)
334+
return py_cel.Type(type_name)
335335
else:
336336
self.fail("Unsupported type: " + str(type_name))
337337

338-
def assertValue(self, result: cel.Value, expected_value: value_pb.Value):
338+
def assertValue(self, result: py_cel.Value, expected_value: value_pb.Value):
339339
if expected_value.HasField("null_value"):
340340
self.assertIsNone(result.value())
341341
elif expected_value.HasField("bool_value"):
@@ -377,7 +377,7 @@ def assertValue(self, result: cel.Value, expected_value: value_pb.Value):
377377
proto.ToDatetime().replace(tzinfo=datetime.timezone.utc),
378378
)
379379
elif expected_value.HasField("type_value"):
380-
self.assertEqual(result.type(), cel.Type.TYPE)
380+
self.assertEqual(result.type(), py_cel.Type.TYPE)
381381
self.assertEqual(
382382
result.value(), self._type_by_name(expected_value.type_value)
383383
)
@@ -388,58 +388,62 @@ def assertValue(self, result: cel.Value, expected_value: value_pb.Value):
388388

389389
def assertPrimitiveType(
390390
self,
391-
result_type: cel.Type,
391+
result_type: py_cel.Type,
392392
expected_primitive: checked_pb.Type.PrimitiveType,
393393
):
394394
if expected_primitive == checked_pb.Type.PrimitiveType.BOOL:
395-
self.assertEqual(result_type, cel.Type.BOOL)
395+
self.assertEqual(result_type, py_cel.Type.BOOL)
396396
elif expected_primitive == checked_pb.Type.PrimitiveType.INT64:
397-
self.assertEqual(result_type, cel.Type.INT)
397+
self.assertEqual(result_type, py_cel.Type.INT)
398398
elif expected_primitive == checked_pb.Type.PrimitiveType.UINT64:
399-
self.assertEqual(result_type, cel.Type.UINT)
399+
self.assertEqual(result_type, py_cel.Type.UINT)
400400
elif expected_primitive == checked_pb.Type.PrimitiveType.DOUBLE:
401-
self.assertEqual(result_type, cel.Type.DOUBLE)
401+
self.assertEqual(result_type, py_cel.Type.DOUBLE)
402402
elif expected_primitive == checked_pb.Type.PrimitiveType.STRING:
403-
self.assertEqual(result_type, cel.Type.STRING)
403+
self.assertEqual(result_type, py_cel.Type.STRING)
404404
elif expected_primitive == checked_pb.Type.PrimitiveType.BYTES:
405-
self.assertEqual(result_type, cel.Type.BYTES)
405+
self.assertEqual(result_type, py_cel.Type.BYTES)
406406
else:
407407
self.fail(
408408
"Unsupported primitive type validation " + str(expected_primitive)
409409
)
410410

411-
def assertType(self, result_type: cel.Type, expected_type: checked_pb.Type):
411+
def assertType(
412+
self, result_type: py_cel.Type, expected_type: checked_pb.Type
413+
):
412414
kind = expected_type.WhichOneof("type_kind")
413415
if kind == "primitive":
414416
self.assertPrimitiveType(result_type, expected_type.primitive)
415417
elif kind == "wrapper":
416418
self.assertPrimitiveType(result_type, expected_type.wrapper)
417419
elif kind == "null":
418-
self.assertEqual(result_type, cel.Type.NULL)
420+
self.assertEqual(result_type, py_cel.Type.NULL)
419421
elif kind == "list_type":
420422
self.assertEqual(
421423
result_type,
422-
cel.Type.List(self._convert_type(expected_type.list_type.elem_type)),
424+
py_cel.Type.List(
425+
self._convert_type(expected_type.list_type.elem_type)
426+
),
423427
)
424428
elif kind == "map_type":
425429
self.assertEqual(
426430
result_type,
427-
cel.Type.Map(
431+
py_cel.Type.Map(
428432
self._convert_type(expected_type.map_type.key_type),
429433
self._convert_type(expected_type.map_type.value_type),
430434
),
431435
)
432436
elif kind == "message_type":
433-
self.assertEqual(result_type, cel.Type(expected_type.message_type))
437+
self.assertEqual(result_type, py_cel.Type(expected_type.message_type))
434438
elif kind == "well_known":
435439
if expected_type.well_known == checked_pb.Type.WellKnownType.TIMESTAMP:
436-
self.assertEqual(result_type, cel.Type.TIMESTAMP)
440+
self.assertEqual(result_type, py_cel.Type.TIMESTAMP)
437441
elif expected_type.well_known == checked_pb.Type.WellKnownType.DURATION:
438-
self.assertEqual(result_type, cel.Type.DURATION)
442+
self.assertEqual(result_type, py_cel.Type.DURATION)
439443
elif kind == "abstract_type":
440444
self.assertEqual(
441445
result_type,
442-
cel.Type.AbstractType(
446+
py_cel.Type.AbstractType(
443447
expected_type.abstract_type.name,
444448
[
445449
self._convert_type(param)

custom_ext/custom_ext_test.py

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
from absl.testing import absltest
2222
from absl.testing import parameterized
23-
import py_cel as cel
23+
import py_cel
2424
from custom_ext import sample_cel_ext
2525
from custom_ext import sample_cel_ext_cc
2626

@@ -36,25 +36,25 @@ class CustomExtTest(parameterized.TestCase):
3636

3737
# TODO(b/462745713): reuse the same extension instance for all tests.
3838
def _compile_expr(
39-
self, ext: Callable[[], cel.CelExtension], expression: str
40-
) -> cel.Expression:
39+
self, ext: Callable[[], py_cel.CelExtension], expression: str
40+
) -> py_cel.Expression:
4141
"""Creates a CEL expression for the given extension and compiles the expression."""
4242
self.descriptor_pool = descriptor_pool.Default()
43-
self.cel = cel.Cel(
43+
self.env = py_cel.NewEnv(
4444
self.descriptor_pool,
4545
variables={},
4646
extensions=[ext()],
4747
)
48-
return self.cel.compile(expression)
48+
return self.env.compile(expression)
4949

50-
def _create_activation(self, impl) -> cel.Activation:
50+
def _create_activation(self, impl) -> py_cel.Activation:
5151
"""Creates a CEL Activation with a late-bound translate function."""
52-
return self.cel.Activation(
52+
return self.env.Activation(
5353
{},
5454
functions=[
55-
cel.Function(
55+
py_cel.Function(
5656
"translate_late",
57-
[cel.Type.STRING],
57+
[py_cel.Type.STRING],
5858
False,
5959
impl,
6060
)
@@ -66,7 +66,7 @@ def test_basic_function(self, ext):
6666
compiled_expr = self._compile_expr(
6767
ext, "'Hello, world!'.translate('en', 'es')"
6868
)
69-
act = self.cel.Activation({})
69+
act = self.env.Activation({})
7070
res = compiled_expr.eval(act)
7171

7272
self.assertEqual(res.value(), "¡Hola Mundo!")
@@ -85,20 +85,20 @@ def test_late_bound_function(self, ext):
8585
@parameterized.named_parameters(EXT_IMPLEMENTATIONS)
8686
def test_error_no_matching_overload(self, ext):
8787
compiled_expr = self._compile_expr(ext, "translate_late('Hello, world!')")
88-
act = self.cel.Activation(
88+
act = self.env.Activation(
8989
{},
9090
functions=[
91-
cel.Function(
91+
py_cel.Function(
9292
"translate_late",
93-
[cel.Type.STRING, cel.Type.INT],
93+
[py_cel.Type.STRING, py_cel.Type.INT],
9494
False,
9595
lambda _: "¡Hola Mundo!",
9696
)
9797
],
9898
)
9999
res = compiled_expr.eval(act)
100100

101-
self.assertEqual(res.type(), cel.Type.ERROR)
101+
self.assertEqual(res.type(), py_cel.Type.ERROR)
102102
self.assertIn(
103103
"No matching overloads found : translate_late(string)",
104104
res.value(),
@@ -110,7 +110,7 @@ def test_error_wrong_arg_number(self, ext):
110110
act = self._create_activation(_lost_in_translation_wrong_arg_number)
111111
res = compiled_expr.eval(act)
112112

113-
self.assertEqual(res.type(), cel.Type.ERROR)
113+
self.assertEqual(res.type(), py_cel.Type.ERROR)
114114
self.assertIn(
115115
"_lost_in_translation_wrong_arg_number() missing 1 required positional"
116116
" argument: 'arg2'",
@@ -123,7 +123,7 @@ def test_error_wrong_return_type(self, ext):
123123
act = self._create_activation(_lost_in_translation_wrong_return_type)
124124
res = compiled_expr.eval(act)
125125

126-
self.assertEqual(res.type(), cel.Type.ERROR)
126+
self.assertEqual(res.type(), py_cel.Type.ERROR)
127127
self.assertRegex(
128128
res.value(),
129129
r"Unexpected value type for .*_lost_in_translation_wrong_return_type.*:"
@@ -138,7 +138,7 @@ def test_error_return_none(self, ext):
138138

139139
res = compiled_expr.eval(act)
140140

141-
self.assertEqual(res.type(), cel.Type.ERROR)
141+
self.assertEqual(res.type(), py_cel.Type.ERROR)
142142
self.assertRegex(
143143
res.value(),
144144
r"Unexpected value type for .*_lost_in_translation_return_none.*:"
@@ -153,7 +153,7 @@ def test_error_propagation(self, ext):
153153

154154
res = compiled_expr.eval(act)
155155

156-
self.assertEqual(res.type(), cel.Type.ERROR)
156+
self.assertEqual(res.type(), py_cel.Type.ERROR)
157157
self.assertIn("NOT_FOUND: Lost in translation", res.value())
158158

159159
def test_bad_extension_type(self):

0 commit comments

Comments
 (0)