Skip to content

Commit 2f9dfd7

Browse files
authored
Fix bug in passing multiple set config per task via cli (#78)
Previously dict would be updated at the base-level meaning any parameters at level below task would be overridden. Here use the executor set_config method which already handles dict update at appropriate depth. Resolves #77
1 parent 05aa2e0 commit 2f9dfd7

File tree

2 files changed

+22
-23
lines changed

2 files changed

+22
-23
lines changed

dplutils/cli.py

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import json
22
from argparse import ArgumentParser, Namespace
3-
from dplutils.pipeline.utils import dict_from_coord
43
from dplutils.pipeline import PipelineExecutor
54

65

@@ -49,14 +48,6 @@ def parse_config_element(conf):
4948
return k, v
5049

5150

52-
def config_dict_from_args(args):
53-
config = {}
54-
for conf in args.set_config:
55-
k,v = parse_config_element(conf)
56-
config.update(dict_from_coord(k, v))
57-
return config
58-
59-
6051
def set_config_from_args(pipeline: PipelineExecutor, args: Namespace):
6152
"""Configure pipeline using config from arguments
6253
@@ -72,9 +63,8 @@ def set_config_from_args(pipeline: PipelineExecutor, args: Namespace):
7263
"""
7364
for ctx in args.set_context:
7465
pipeline.set_context(*parse_config_element(ctx))
75-
config = config_dict_from_args(args)
76-
if config:
77-
pipeline.set_config_from_dict(config)
66+
for conf in args.set_config:
67+
pipeline.set_config(*parse_config_element(conf))
7868

7969

8070
def cli_run(pipeline: PipelineExecutor, args: Namespace|None = None, **argparse_kwargs):

tests/test_cli.py

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import pytest
22
from dplutils import cli
3+
from dplutils.pipeline import PipelineTask
34

45

56
TEST_ARGV = [
67
'',
78
'--set-context', 'ctx=ctx-value',
89
'--set-config', 'a.b.c=[1,2,3]',
10+
'--set-config', 'a.b.x=99',
911
'--set-config', 'x.y=1',
1012
'--set-config', 'l.m=string',
1113
]
@@ -19,15 +21,7 @@ def sys_argv(monkeypatch):
1921
def test_get_argparser_adds_default_arguments(sys_argv):
2022
args = cli.get_argparser().parse_args()
2123
assert args.set_context == ['ctx=ctx-value']
22-
assert args.set_config == ['a.b.c=[1,2,3]', 'x.y=1', 'l.m=string']
23-
24-
25-
def test_config_from_args(sys_argv):
26-
args = cli.get_argparser().parse_args()
27-
config = cli.config_dict_from_args(args)
28-
assert config['a']['b']['c'] == [1, 2, 3]
29-
assert config['x']['y'] == 1
30-
assert config['l']['m'] == 'string'
24+
assert args.set_config == ['a.b.c=[1,2,3]', 'a.b.x=99', 'x.y=1', 'l.m=string']
3125

3226

3327
def test_pipeline_set_config_from_args_no_args(monkeypatch, dummy_executor):
@@ -38,9 +32,24 @@ def test_pipeline_set_config_from_args_no_args(monkeypatch, dummy_executor):
3832
def test_run_with_cli_helper(monkeypatch, dummy_executor, tmp_path):
3933
assert len(list(tmp_path.glob('*.parquet'))) == 0
4034
monkeypatch.setattr(
41-
'sys.argv',
42-
['', '-o', str(tmp_path), '--set-context', 'ctxvar=value', '--set-config', 'task1.num_cpus=2'])
35+
'sys.argv', [
36+
'',
37+
'-o',
38+
str(tmp_path),
39+
'--set-context', 'ctxvar=value',
40+
'--set-config', 'task1.num_cpus=2',
41+
'--set-config', 'task1.batch_size=10',
42+
'--set-config', 'task_kw.kwargs.a=[1,2]',
43+
'--set-config', 'task_kw.kwargs.b=99',
44+
])
45+
def task_with_kwargs(indf, a=None, b=None):
46+
return indf
47+
# patch in kwargs task to test kwarg setting via dotted notation
48+
dummy_executor.graph.add_edge(dummy_executor.graph.task_map['task2'], PipelineTask('task_kw', task_with_kwargs))
4349
cli.cli_run(dummy_executor)
4450
assert dummy_executor.ctx['ctxvar'] == 'value'
4551
assert dummy_executor.tasks_idx['task1'].num_cpus == 2
52+
assert dummy_executor.tasks_idx['task1'].batch_size == 10
53+
assert dummy_executor.tasks_idx['task_kw'].kwargs['a'] == [1,2]
54+
assert dummy_executor.tasks_idx['task_kw'].kwargs['b'] == 99
4655
assert len(list(tmp_path.glob('*.parquet'))) == 10

0 commit comments

Comments
 (0)