Skip to content

Commit 5a2611c

Browse files
authored
fix: user extra arguments (#50)
* fix: user extra arguments Signed-off-by: develop-cs <43383361+develop-cs@users.noreply.github.com> * docs: update value sharing Signed-off-by: develop-cs <43383361+develop-cs@users.noreply.github.com> --------- Signed-off-by: develop-cs <43383361+develop-cs@users.noreply.github.com>
1 parent 4554f44 commit 5a2611c

File tree

14 files changed

+112
-36
lines changed

14 files changed

+112
-36
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ repos:
4444
# hooks:
4545
# - id: gitleaks
4646
- repo: https://github.com/pypa/pip-audit
47-
rev: v2.9.0
47+
rev: v2.8.0
4848
hooks:
4949
- id: pip-audit
5050
args: [.]

CHANGELOG.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,22 @@ All notable changes to this project will be documented in this file.
44

55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
## 0.10.3 - July, 2025
8+
9+
### Fixes
10+
11+
* User extra arguments in `.apply_rules()` were not given to action and condition function implementations (#49).
12+
13+
### Documentation
14+
15+
* Improve :
16+
* [Value sharing](https://maif.github.io/arta/value_sharing/) page.
17+
718
## 0.10.2 - July, 2025
819

920
### Fixes
1021

11-
* `kwargs` as an optional parameter (since v0.10.0) wasn't totally implemented: *dictionary of rules* was missing (#47).
22+
* `kwargs` were mandatory for action functions used inside a *dictionary of rules* (#47). They are now optional.
1223

1324
## 0.10.1 - April, 2025
1425

docs/pages/a_simple_example.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,8 @@ An **action** is triggered when the conditions are verified (i.e., `True`).
106106
**Actions** are defined by the following *keys* in the previous YAML file:
107107

108108
```yaml
109-
action: set_admission # (1)
110-
action_parameters: # (2)
109+
action: set_admission # (1)!
110+
action_parameters: # (2)!
111111
value: true
112112
```
113113

@@ -201,11 +201,11 @@ Now, let's apply the **rules** on a single applicant:
201201
```python
202202
from arta import RulesEngine
203203
204-
eng = RulesEngine(config_path="/to/my/config/dir") # (1)
204+
eng = RulesEngine(config_path="/to/my/config/dir") # (1)!
205205
206206
result = eng.apply_rules(input_data=applicants[0])
207207
208-
print(result) # (2)
208+
print(result) # (2)!
209209
# {
210210
# "admission": {"is_admitted": True},
211211
# "course": {"course_id": "senior"},

docs/pages/home.md

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,19 @@
1212

1313
Want to discover what is **Arta**? :arrow_right: [Get Started](a_simple_example.md)
1414

15-
1615
Want to know how to use it? :arrow_right: [User Guide](how_to.md)
1716

18-
1917
!!! info inline "New feature"
2018

21-
Use **Arta** as a *process execution engine* :zap:
22-
23-
Read [this page](rule_activation_mode.md) for more details.
19+
Use [value sharing](value_sharing.md) to customize actions and conditions :tools:
2420

2521
!!! tip "Releases"
2622

2723
Check the [Release notes](https://github.com/MAIF/arta/releases) :rocket:
2824

29-
!!! warning "Pydantic 1 compatibility is deprecated"
25+
!!! warning "Pydantic v1"
3026

31-
**Arta** is working with [Pydantic 2](https://docs.pydantic.dev/latest/) and Pydantic 1 but compatibility with V1 will be removed in the next **major** release.
27+
Compatibility with *Pydantic v1* will be removed in next **major** release.
3228

3329
**Arta** is working and automatically tested with:
3430

docs/pages/how_to.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ rules:
4343
value: KO
4444

4545
actions_source_modules:
46-
- my_folder.actions # (1)
46+
- my_folder.actions # (1)!
4747
```
4848
4949
1. Contains *action function* implementations, no need of the key `conditions_source_modules` here.
@@ -112,10 +112,10 @@ Create a YAML file and define your rules like this:
112112
```yaml hl_lines="6 16-26"
113113
---
114114
rules:
115-
default_rule_set: # (1)
115+
default_rule_set: # (1)!
116116
check_admission:
117117
ADMITTED_RULE:
118-
condition: HAS_SCHOOL_AUTHORIZED_POWER # (2)
118+
condition: HAS_SCHOOL_AUTHORIZED_POWER # (2)!
119119
action: set_admission
120120
action_parameters:
121121
value: true
@@ -125,16 +125,16 @@ rules:
125125
action_parameters:
126126
value: false
127127
128-
conditions: # (3)
128+
conditions: # (3)!
129129
HAS_SCHOOL_AUTHORIZED_POWER:
130130
description: "Does applicant have a school authorized power?"
131131
validation_function: has_authorized_super_power
132132
condition_parameters:
133133
power: input.super_power
134134
135-
conditions_source_modules: # (4)
135+
conditions_source_modules: # (4)!
136136
- my_folder.conditions
137-
actions_source_modules: # (5)
137+
actions_source_modules: # (5)!
138138
- my_folder.actions
139139
```
140140

@@ -289,9 +289,9 @@ A **rule set** is composed of **rule groups** which are themselves composed of *
289289
```yaml
290290
---
291291
rules:
292-
default_rule_set: # (1)
293-
check_admission: # (2)
294-
ADMITTED_RULE: # (3)
292+
default_rule_set: # (1)!
293+
check_admission: # (2)!
294+
ADMITTED_RULE: # (3)!
295295
condition: HAS_SCHOOL_AUTHORIZED_POWER
296296
action: set_admission
297297
action_parameters:

docs/pages/value_sharing.md

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
!!! note
1+
!!! info
22

3-
Only available with `arta>=0.10.0`.
3+
Needs `arta>=0.10.0`.
44

5+
## Between conditions and actions
56

67
It is possible to share some informations between **condition** and **action** implementations.
78

@@ -22,12 +23,12 @@ In the following example, a **condition** is computing the *median* of some *inp
2223
def is_median_above(
2324
values: list[float],
2425
limit: float,
25-
**kwargs: Any, # (1)
26+
**kwargs: Any, # (1)!
2627
) -> bool:
2728
"""Check if the median of some values is above limit."""
2829
median = statistics.median(values)
2930
# Store the value for later use by an action function
30-
kwargs["input_data"]["median"] = median # (2)
31+
kwargs["input_data"]["median"] = median # (2)!
3132
kwargs["input_data"]["median_limit"] = limit
3233
return median > limit
3334
```
@@ -39,13 +40,45 @@ def is_median_above(
3940
**Get the value (in an action for example):**
4041

4142
```python hl_lines="1 4"
42-
def alert_on_median(**kwargs: Any) -> str: # (1)
43+
def alert_on_median(**kwargs: Any) -> str: # (1)!
4344
"""Alert: "Median is too high: 13, limit is: 10."""
4445
return (
45-
f"Median is too high: {kwargs['input_data']['median']}, " # (2)
46+
f"Median is too high: {kwargs['input_data']['median']}, " # (2)!
4647
f"limit is: {kwargs['input_data']['median_limit']}."
4748
)
4849
```
4950

5051
1. Add the ****kwargs** parameter.
51-
2. Get the value.
52+
2. Get the value.
53+
54+
## User extra arguments
55+
56+
The following code shows how to set custom **user extra arguments** (e.g., `add_details`) within the `.apply_rules()` method:
57+
58+
```python hl_lines="5"
59+
from arta import RulesEngine
60+
61+
eng = RulesEngine(config_path="/to/my/config/dir")
62+
63+
result = eng.apply_rules(input_data={...}, add_details=True)
64+
```
65+
66+
Used inside **action** and/or **condition functions**:
67+
68+
```python hl_lines="4"
69+
def my_action(**kwargs: Any) -> str: # (1)!
70+
"""A simple action function."""
71+
72+
if kwargs["add_details"]: # (2)!
73+
action_result = (
74+
f"Median is too high: {kwargs['input_data']['median']}, "
75+
f"limit is: {kwargs['input_data']['median_limit']}."
76+
)
77+
else:
78+
action_result = "Median is too high."
79+
80+
return action_result
81+
```
82+
83+
1. Don't forget to add the ****kwargs** parameter.
84+
2. Straight use of the corresponding extra argument.

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
44

55
[project]
66
name = "arta"
7-
version = "0.10.2"
7+
version = "0.10.3"
88
requires-python = ">3.8.0"
99
description = "A Python Rules Engine - Make rule handling simple"
1010
readme = "README.md"

src/arta/condition.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ def verify(self, input_data: dict[str, Any], parsing_error_strategy: ParsingErro
145145
arg_spec: inspect.FullArgSpec = inspect.getfullargspec(self._validation_function)
146146
if arg_spec.varkw is not None:
147147
parameters["input_data"] = input_data
148+
parameters.update(kwargs)
148149

149150
# Run validation_function
150151
return self._validation_function(**parameters)

src/arta/rule.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,18 +111,21 @@ def apply(
111111
arg_spec: inspect.FullArgSpec = inspect.getfullargspec(self._action)
112112
if arg_spec.varkw is not None:
113113
parameters["input_data"] = input_data
114+
parameters.update(kwargs)
114115

115116
# Backward compatibility case (now deprecated)
116117
if "input_data" in arg_spec.args or "input_data" in arg_spec.kwonlyargs:
117118
warn(
118119
(
119120
"Using 'input_data' directly as an action function parameter is deprecated. "
120-
"Use '**kwargs' instead. See how at https://maif.github.io/arta/value_sharing/"
121+
"Use '**kwargs' instead. See how "
122+
"at https://maif.github.io/arta/value_sharing/#between-conditions-and-actions"
121123
),
122124
DeprecationWarning,
123125
stacklevel=2,
124126
)
125127
parameters["input_data"] = input_data
128+
parameters.update(kwargs)
126129

127130
# Run action
128131
rule_results["action_result"] = self._action(**parameters)

tests/examples/code/actions.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,11 @@ def concatenate(value1: str, value2: str) -> str:
5454
def alert_on_median(**kwargs: Any) -> str:
5555
"""Alert: "Median is too high: 13, limit is: 10."""
5656
return f"Median is too high: {kwargs['input_data']['median']}, limit is: {kwargs['input_data']['median_limit']}."
57+
58+
59+
def set_admission_custom(value: bool, **kwargs: Any) -> dict[str, bool]:
60+
"""Return a dictionary containing the admission result and use a user defined argument."""
61+
# Pseudo edge case
62+
value = value if kwargs["my_parameter"] is True else False
63+
64+
return {"admission": value}

0 commit comments

Comments
 (0)