Skip to content

Commit 144dfd5

Browse files
dmitriplotnikovcopybara-github
authored andcommitted
Add Python support for CEL optional values and types.
Expose `cel.Optional` (`of`, `none`, `has_value`, `value`, `or_value`) and `cel.Type.Optional` / `cel.Type.OPTIONAL`, and convert between `cel::OptionalValue` and `cel.Optional` in `CelValueToPyObject` and `PyObjectToCelValue` so optional values can be evaluated, passed in activations, and accepted or returned by Python functions. PiperOrigin-RevId: 1018344380
1 parent 1711b42 commit 144dfd5

25 files changed

Lines changed: 2101 additions & 125 deletions

‎README.md‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,25 @@ env = cel.NewEnv(pool, extensions=[ext_math.ExtMath()])
314314
expr = env.compile("math.sqrt(4)")
315315
```
316316

317+
#### Working with optional values
318+
319+
When `ext_optional.ExtOptional()` is enabled, CEL `optional_type` values map to
320+
`cel.Optional` (`cel.Optional.of(value)` and `cel.Optional.none()`), and
321+
optional variable/function types can be declared with `cel.Type.Optional(...)`
322+
(or `cel.Type.OPTIONAL` for `optional_type<dyn>`):
323+
324+
```python
325+
env = cel.NewEnv(
326+
variables={"x": cel.Type.Optional(cel.Type.INT)},
327+
extensions=[ext_optional.ExtOptional()],
328+
)
329+
res = env.compile("x").eval(data={"x": cel.Optional.of(42)})
330+
opt = res.plain_value() # cel.Optional.of(42)
331+
print(opt.has_value()) # True
332+
print(opt.value()) # 42
333+
print(opt.or_value(0)) # 42
334+
```
335+
317336
#### Defining a custom extension in Python
318337

319338
You can define custom functions and pass them as an extension.

‎cel_expr_python/BUILD‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
load("@pybind11_bazel//:build_defs.bzl", "pybind_extension", "pybind_library")
22
load("@rules_cc//cc:cc_library.bzl", "cc_library")
3+
load("@rules_python//python:py_library.bzl", "py_library")
34
load("@rules_python//python:py_test.bzl", "py_test")
45

56
package(default_visibility = ["//visibility:public"])
@@ -20,6 +21,7 @@ pybind_library(
2021
"py_cel_expression.cc",
2122
"py_cel_function.cc",
2223
"py_cel_function_decl.cc",
24+
"py_cel_optional.cc",
2325
"py_cel_options.cc",
2426
"py_cel_overload.cc",
2527
"py_cel_python_extension.cc",
@@ -38,6 +40,7 @@ pybind_library(
3840
"py_cel_expression.h",
3941
"py_cel_function.h",
4042
"py_cel_function_decl.h",
43+
"py_cel_optional.h",
4144
"py_cel_options.h",
4245
"py_cel_overload.h",
4346
"py_cel_python_extension.h",
@@ -150,20 +153,34 @@ cc_library(
150153
],
151154
)
152155

156+
py_library(
157+
name = "leak_check",
158+
testonly = True,
159+
srcs = ["leak_check.py"],
160+
visibility = [":__subpackages__"],
161+
)
162+
153163
py_test(
154164
name = "cel_test",
155165
srcs = ["cel_test.py"],
156166
data = [
157167
":cel",
168+
"//cel_expr_python/ext:ext_optional",
158169
],
159170
deps = [
171+
":leak_check",
160172
"//testing:proto2_test_all_types_py_pb2",
161173
"@com_google_absl_py//absl/testing:absltest",
174+
"@com_google_absl_py//absl/testing:parameterized",
175+
"@com_google_cel_spec//proto/cel/expr:checked_py_pb2",
162176
"@com_google_protobuf//:protobuf",
163177
"@com_google_protobuf//:protobuf_python",
164178
] + select({
165179
"@platforms//os:windows": [],
166-
"//conditions:default": [":cel"],
180+
"//conditions:default": [
181+
":cel",
182+
"//cel_expr_python/ext:ext_optional",
183+
],
167184
}),
168185
)
169186

@@ -174,6 +191,7 @@ py_test(
174191
":cel",
175192
],
176193
deps = [
194+
":leak_check",
177195
"//testing:proto2_test_all_types_py_pb2",
178196
"@com_google_absl_py//absl/testing:absltest",
179197
] + select({
@@ -196,6 +214,7 @@ py_test(
196214
deps = [
197215
"//testing:proto2_test_all_types_py_pb2",
198216
"@com_google_absl_py//absl/testing:absltest",
217+
"@com_google_absl_py//absl/testing:parameterized",
199218
] + select({
200219
"@platforms//os:windows": [],
201220
"//conditions:default": [

‎cel_expr_python/cel.pyi‎

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
from collections.abc import Callable, Mapping, Sequence
22
import datetime
3-
from typing import Any, ClassVar, TypeAlias, Union
3+
from typing import Any, ClassVar, Generic, TypeAlias, TypeVar, Union
44
from google.protobuf import descriptor_pool as proto_descriptor_pool
55
from google.protobuf import message as proto_message
66

7+
_T = TypeVar('_T')
8+
_D = TypeVar('_D')
9+
710
class Activation: pass
811

912
class CelExtension(CelExtensionBase):
@@ -46,6 +49,18 @@ class FunctionDecl:
4649
class Overload:
4750
def __init__(self, id: str | None = ..., return_type: Type = ..., parameters: Sequence[Type] = ..., is_member: bool = ..., impl: Callable[..., Any] = ..., signature: str | None = ...) -> None: ...
4851

52+
class Optional(Generic[_T]):
53+
def __init__(self) -> None: ...
54+
@staticmethod
55+
def of(value: _T) -> Optional[_T]: ...
56+
@staticmethod
57+
def none() -> Optional[Any]: ...
58+
def has_value(self) -> bool: ...
59+
def value(self) -> _T: ...
60+
def or_value(self, default: _D) -> _T | _D: ...
61+
def __bool__(self) -> bool: ...
62+
def __eq__(self, other: object) -> bool: ...
63+
4964
class Type:
5065
BOOL: ClassVar[Type] = ...
5166
BYTES: ClassVar[Type] = ...
@@ -57,6 +72,7 @@ class Type:
5772
LIST: ClassVar[Type] = ...
5873
MAP: ClassVar[Type] = ...
5974
NULL: ClassVar[Type] = ...
75+
OPTIONAL: ClassVar[Type] = ...
6076
STRING: ClassVar[Type] = ...
6177
TIMESTAMP: ClassVar[Type] = ...
6278
TYPE: ClassVar[Type] = ...
@@ -70,15 +86,17 @@ class Type:
7086
@staticmethod
7187
def Map(key_type: Type, element_type: Type) -> Type: ...
7288
@staticmethod
89+
def Optional(element_type: Type = ...) -> Type: ...
90+
@staticmethod
7391
def Type(element_type: Type) -> Type: ...
7492
def is_assignable_from(self, arg0: Type) -> bool: ...
7593
def is_message(self) -> bool: ...
7694
def name(self) -> str: ...
7795
def __eq__(self, arg0: object) -> bool: ...
7896

7997
BaseValue: TypeAlias = Union[None, bool, int, float, str, bytes, proto_message.Message, datetime.datetime, datetime.timedelta, Type]
80-
PlainValue: TypeAlias = Union[BaseValue, list[PlainValue], dict[PlainValue, PlainValue]]
81-
CelValue: TypeAlias = Union[BaseValue, list[Value], dict[PlainValue, Value]]
98+
PlainValue: TypeAlias = Union[BaseValue, Optional[PlainValue], list[PlainValue], dict[PlainValue, PlainValue]]
99+
CelValue: TypeAlias = Union[BaseValue, Optional[Value], list[Value], dict[PlainValue, Value]]
82100

83101
class Value:
84102
def equals(self, other: Value) -> bool: ...

0 commit comments

Comments
 (0)