Skip to content

Commit 86b7ce2

Browse files
committed
example StepDecorator test
1 parent 72d868d commit 86b7ce2

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from metaflow.plugins import STEP_DECORATORS
2+
3+
from metaflow.tests.flows import NewLinearFlow
4+
from metaflow.tests.step_counter import StepCounter
5+
6+
STEP_DECORATORS.append(StepCounter)
7+
8+
9+
class StepCounterFlow(NewLinearFlow):
10+
"""Example flow that can receive `--with step_counter` from the CLI
11+
12+
In order for `--with my_decorator` to work on the CLI, `my_decorator` needs to added to `STEP_DECORATORS`, as above.
13+
I don't know of a good way to accomplish that other than making a trivial flow (`StepCounterFlow` here) that just
14+
mixes in the target flow (`NewLinearFlow`) in a file that appends the decorator (`StepCounter`) to `STEP_DECORATORS.
15+
16+
TODO: formalize an API for registering step- and flow-decorators without having to define a new flow like this.
17+
"""
18+
19+
pass

metaflow/tests/step_counter.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from metaflow.decorators import StepDecorator
2+
3+
4+
class StepCounter(StepDecorator):
5+
name = "step_counter"
6+
7+
def task_post_step(
8+
self, step_name, flow, graph, retry_count, max_user_code_retries
9+
):
10+
if not hasattr(flow, "step_count"):
11+
flow.step_count = 0
12+
flow.step_count += 1
13+
print("step count: %d" % flow.step_count)

metaflow/tests/test_decorators.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from metaflow.tests.flows.step_counter_flow import StepCounterFlow
2+
from metaflow.tests.utils import run
3+
4+
5+
def test_step_count():
6+
flow = StepCounterFlow
7+
data = run(
8+
flow,
9+
args=(
10+
"run",
11+
"--with",
12+
"step_counter",
13+
),
14+
)
15+
assert data == {
16+
"a": 111,
17+
"b": 222,
18+
"checked": True,
19+
"step_count": 5,
20+
}

0 commit comments

Comments
 (0)