Skip to content

Commit 825e1a2

Browse files
committed
Add a test to show issue cloud-custodian#42 seems to be resolved
1 parent 55ed372 commit 825e1a2

6 files changed

Lines changed: 54 additions & 18 deletions

File tree

‎.gitignore‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,3 +132,5 @@ type_check/lineprecision.txt
132132
# PyCharm Stuff
133133
.idea/*
134134
demo/issue_41.py
135+
demo/issue_80.py
136+
setup.sh

‎demo/README.rst‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,4 @@ and examine the notebooks. We'll look at this from a Cloud Custodian (C7N) persp
5050
- `demo/complex_policy.ipynb`_ shows a complex policy and how we can build -- and debug -- the policy
5151
filter expression.
5252

53-
These should help youo build an understanding of
53+
These should help you build an understanding of how to develop complex filters.

‎src/celpy/evaluation.py‎

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1417,7 +1417,8 @@ def expr(self, tree: lark.Tree) -> Result:
14171417
expr : conditionalor ["?" conditionalor ":" expr]
14181418
14191419
The default implementation short-circuits
1420-
and can ignore an CELEvalError in a sub-expression.
1420+
and can ignore a CELEvalError in the two alternative sub-expressions.
1421+
The conditional sub-expression CELEvalError is propogated out as the result.
14211422
14221423
See https://github.com/google/cel-spec/blob/master/doc/langdef.md#logical-operators
14231424
@@ -2030,15 +2031,25 @@ def member_dot_arg(self, tree: lark.Tree) -> Result:
20302031
Tuple[lark.Tree, lark.Token], tree.children[:2]
20312032
)
20322033

2033-
if method_name_token.value in {"map", "filter", "all", "exists", "exists_one", "reduce", "min"}:
2034+
if method_name_token.value in {
2035+
"map",
2036+
"filter",
2037+
"all",
2038+
"exists",
2039+
"exists_one",
2040+
"reduce",
2041+
"min",
2042+
}:
20342043
member_list = cast(celpy.celtypes.ListType, self.visit(member_tree))
20352044

20362045
if isinstance(member_list, CELEvalError):
20372046
return member_list
20382047

20392048
if method_name_token.value == "map":
20402049
sub_expr = self.build_macro_eval(tree)
2041-
mapping = cast(Iterable[celpy.celtypes.Value], map(sub_expr, member_list))
2050+
mapping = cast(
2051+
Iterable[celpy.celtypes.Value], map(sub_expr, member_list)
2052+
)
20422053
result = celpy.celtypes.ListType(mapping)
20432054
return result
20442055

@@ -2052,19 +2063,25 @@ def member_dot_arg(self, tree: lark.Tree) -> Result:
20522063
and_oper = cast(
20532064
CELBoolFunction,
20542065
eval_error("no such overload", TypeError)(
2055-
celpy.celtypes.logical_and)
2066+
celpy.celtypes.logical_and
2067+
),
2068+
)
2069+
reduction = reduce(
2070+
and_oper, map(sub_expr, member_list), celpy.celtypes.BoolType(True)
20562071
)
2057-
reduction = reduce(and_oper, map(sub_expr, member_list), celpy.celtypes.BoolType(True))
20582072
return reduction
20592073

20602074
elif method_name_token.value == "exists":
20612075
sub_expr = self.build_ss_macro_eval(tree)
20622076
or_oper = cast(
20632077
CELBoolFunction,
20642078
eval_error("no such overload", TypeError)(
2065-
celpy.celtypes.logical_or)
2079+
celpy.celtypes.logical_or
2080+
),
2081+
)
2082+
reduction = reduce(
2083+
or_oper, map(sub_expr, member_list), celpy.celtypes.BoolType(False)
20662084
)
2067-
reduction = reduce(or_oper, map(sub_expr, member_list), celpy.celtypes.BoolType(False))
20682085
return reduction
20692086

20702087
elif method_name_token.value == "exists_one":

‎tests/test_evaluation.py‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -548,8 +548,24 @@ def bad_condition(a, b, c):
548548
activation,
549549
functions={"_?_:_": bad_condition}
550550
)
551-
with raises(celpy.evaluation.CELEvalError):
551+
with raises(celpy.evaluation.CELEvalError) as exc_info:
552552
evaluator.evaluate()
553+
assert exc_info.value.args == ("found no matching overload for _?_:_ applied to '(<class 'celpy.celtypes.BoolType'>, <class 'celpy.celtypes.IntType'>, <class 'celpy.celtypes.BoolType'>)'", TypeError, ())
554+
555+
def test_eval_expr_3_bad_cond_value(mock_left_expr_tree):
556+
def bad_condition(a, b, c):
557+
raise celpy.evaluation.CELEvalError("Baseline Error")
558+
activation = Mock()
559+
evaluator = Evaluator(
560+
mock_left_expr_tree,
561+
activation,
562+
functions={"_?_:_": bad_condition}
563+
)
564+
with raises(celpy.evaluation.CELEvalError) as exc_info:
565+
evaluator.evaluate()
566+
print(repr(exc_info.value.args))
567+
assert exc_info.value.args == ('Baseline Error',)
568+
553569

554570
@fixture
555571
def mock_right_expr_tree():

‎tox.ini‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
[tox]
1717
skipsdist = true
18-
envlist = py38,py39,py310,py311,py312,lint
18+
envlist = py39,py310,py311,py312,py313,lint
1919
minversion = 4.15.0
2020

2121
[testenv]
@@ -36,6 +36,7 @@ commands =
3636
poetry run mypy --show-error-codes src
3737

3838
[testenv:lint]
39+
usedevelop = true
3940
depends = py312
4041
basepython = python3.12
4142
commands =

‎type_check/lineprecision.txt‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
Name Lines Precise Imprecise Any Empty Unanalyzed
22
-------------------------------------------------------------------
3-
celpy 293 76 0 4 213 0
4-
celpy.__main__ 465 172 7 42 244 0
5-
celpy.adapter 137 35 3 9 85 5
6-
celpy.c7nlib 1584 340 15 154 1075 0
7-
celpy.celparser 401 207 2 22 170 0
8-
celpy.celtypes 1503 438 14 221 791 39
9-
celpy.evaluation 2472 835 33 177 1411 16
3+
celpy 306 76 0 4 226 0
4+
celpy.__main__ 508 204 8 42 254 0
5+
celpy.adapter 141 34 3 9 89 6
6+
celpy.c7nlib 1663 351 14 149 1149 0
7+
celpy.celparser 388 199 3 22 164 0
8+
celpy.celtypes 1504 417 14 224 812 37
9+
celpy.evaluation 2595 872 38 177 1490 18
1010
xlate 0 0 0 0 0 0
11-
xlate.c7n_to_cel 1730 377 102 145 1101 5
11+
xlate.c7n_to_cel 1755 385 103 146 1115 6

0 commit comments

Comments
 (0)