@@ -6,6 +6,97 @@ Complete autogenerated reference for the Python CEL library.
66
77::: cel.evaluate
88
9+ ## Enums
10+
11+ ### EvaluationMode
12+
13+ ** Controls how CEL expressions handle type compatibility.**
14+
15+ The EvaluationMode enum allows you to choose between Python-friendly type coercion (default) and strict CEL type enforcement:
16+
17+ ``` python
18+ from cel import evaluate, EvaluationMode
19+
20+ # Python-friendly mode (default) - allows mixed arithmetic
21+ result = evaluate(" 1 + 2.5" ) # → 3.5
22+ result = evaluate(" 1 + 2.5" , mode = EvaluationMode.PYTHON ) # → 3.5
23+ result = evaluate(" 1 + 2.5" , mode = " python" ) # → 3.5
24+
25+ # Strict mode - enforces strict CEL type rules
26+ try :
27+ evaluate(" 1 + 2.5" , mode = EvaluationMode.STRICT ) # TypeError
28+ except TypeError as e:
29+ print (f " Strict mode error: { e} " ) # → "Unsupported addition operation"
30+
31+ try :
32+ evaluate(" 1 + 2.5" , mode = " strict" ) # TypeError
33+ except TypeError as e:
34+ print (f " Strict mode error: { e} " ) # → "Unsupported addition operation"
35+ ```
36+
37+ #### Values
38+
39+ ** EvaluationMode.PYTHON** (or ` "python" ` )
40+ * Default mode for Python API.* Enables Python-friendly type promotions for better mixed arithmetic compatibility:
41+ - Integer literals are automatically promoted to floats when used with floats
42+ - Context variables are promoted from int to float when mixed arithmetic is detected
43+ - Expression preprocessing converts ` 1 + 2.5 ` to ` 1.0 + 2.5 ` for compatibility
44+
45+ ** EvaluationMode.STRICT** (or ` "strict" ` )
46+ * Default mode for CLI.* Enforces strict CEL type rules with no automatic coercion:
47+ - Mixed int/float arithmetic raises TypeError
48+ - No type promotions or expression rewriting
49+ - Matches WebAssembly CEL behavior exactly
50+
51+ #### Default Modes
52+
53+ ** Note** : The Python API and CLI have different defaults:
54+ - ** Python API** : Defaults to ` EvaluationMode.PYTHON ` for seamless integration with Python code
55+ - ** CLI** : Defaults to ` EvaluationMode.STRICT ` for CEL specification compliance and testing
56+
57+ #### When to Use Each Mode
58+
59+ ** Use PYTHON mode (Python API default) when:**
60+ - Integrating with existing Python code that expects mixed numeric types
61+ - Working with data from JSON APIs (which often mix ints and floats)
62+ - Building user-friendly applications where type coercion feels natural
63+ - Migrating from pure Python evaluation logic
64+
65+ ** Use STRICT mode (CLI default) when:**
66+ - Building applications that need to match CEL implementations in other languages
67+ - Working with systems where type precision is critical
68+ - Following strict CEL specification compliance
69+ - Debugging type-related issues in expressions
70+
71+ #### Implementation Details
72+
73+ In PYTHON mode, the library:
74+ 1 . Analyzes context for mixed numeric types (int + float)
75+ 2 . Promotes integers to floats in both context variables and expression literals
76+ 3 . Preprocesses expressions like ` 1 + 2.5 ` to become ` 1.0 + 2.5 `
77+
78+ In STRICT mode, the library:
79+ 1 . Performs no type promotions or expression rewriting
80+ 2 . Passes expressions directly to the CEL evaluator
81+ 3 . Raises TypeError for incompatible type operations
82+
83+ ** Example with Context:**
84+
85+ ``` python
86+ from cel import evaluate, EvaluationMode
87+
88+ context = {" x" : 1 , " y" : 2.5 } # Mixed int/float in context
89+
90+ # Python mode handles mixed types gracefully
91+ result = evaluate(" x + y" , context, mode = EvaluationMode.PYTHON ) # → 3.5
92+
93+ # Strict mode rejects mixed types
94+ try :
95+ evaluate(" x + y" , context, mode = EvaluationMode.STRICT ) # TypeError
96+ except TypeError as e:
97+ print (" Mixed types not allowed in strict mode" )
98+ ```
99+
9100## Classes
10101
11102### Context
@@ -292,12 +383,45 @@ except TypeError as e:
292383 assert " Unsupported multiplication operation" in str (e)
293384```
294385
386+ #### EvaluationMode-Specific Errors
387+
388+ ** Strict mode can produce additional TypeError exceptions:**
389+
390+ ``` python
391+ from cel import evaluate, EvaluationMode
392+
393+ # Mixed numeric types in strict mode
394+ try :
395+ evaluate(" 1 + 2.5" , mode = EvaluationMode.STRICT )
396+ except TypeError as e:
397+ assert " Unsupported addition operation" in str (e)
398+ print (f " Strict mode type error: { e} " )
399+
400+ # Mixed types from context in strict mode
401+ context = {" int_val" : 10 , " float_val" : 2.5 }
402+ try :
403+ evaluate(" int_val * float_val" , context, mode = EvaluationMode.STRICT )
404+ except TypeError as e:
405+ assert " Unsupported multiplication operation" in str (e)
406+ print (f " Context type mixing error: { e} " )
407+ ```
408+
409+ ** Invalid mode strings raise TypeError:**
410+
411+ ``` python
412+ try :
413+ evaluate(" 1 + 2" , mode = " invalid_mode" )
414+ except TypeError as e:
415+ assert " Invalid EvaluationMode" in str (e)
416+ print (f " Invalid mode error: { e} " )
417+ ```
418+
295419### Production Error Handling
296420
297421For comprehensive error handling patterns, safety guidelines, and production best practices, see the ** [ Error Handling How-To Guide] ( ../how-to-guides/error-handling.md ) ** which covers:
298422
299423- Safe handling of malformed expressions and untrusted input
300- - Safe evaluation wrappers
424+ - Safe evaluation wrappers with EvaluationMode considerations
301425- Context validation patterns
302426- Defensive expression techniques
303427- Logging and monitoring
0 commit comments