diff --git a/pydra/engine/helpers.py b/pydra/engine/helpers.py index 4d8e84132..682e7e193 100644 --- a/pydra/engine/helpers.py +++ b/pydra/engine/helpers.py @@ -649,12 +649,9 @@ def argstr_formatting(argstr, inputs, value_updates=None): if value_updates: inputs_dict.update(value_updates) # getting all fields that should be formatted, i.e. {field_name}, ... - inp_fields = re.findall(r"{\w+}", argstr) - inp_fields_float = re.findall(r"{\w+:[0-9.]+f}", argstr) - inp_fields += [re.sub(":[0-9.]+f", "", el) for el in inp_fields_float] + inp_fields = re.findall(r"{(\w+)(?::[0-9.]+f|\[[\w]+\])?}", argstr) val_dict = {} - for fld in inp_fields: - fld_name = fld[1:-1] # extracting the name form {field_name} + for fld_name in inp_fields: fld_value = inputs_dict[fld_name] fld_attr = getattr(attrs.fields(type(inputs)), fld_name) if fld_value is attr.NOTHING or ( diff --git a/pydra/engine/tests/test_helpers.py b/pydra/engine/tests/test_helpers.py index 06ce39220..308980acb 100644 --- a/pydra/engine/tests/test_helpers.py +++ b/pydra/engine/tests/test_helpers.py @@ -4,6 +4,7 @@ import random import platform import pytest +import attrs import cloudpickle as cp from unittest.mock import Mock from fileformats.generic import Directory, File @@ -15,9 +16,9 @@ load_and_run, position_sort, parse_copyfile, + argstr_formatting, ) from ...utils.hash import hash_function -from .. import helpers_file from ..core import Workflow @@ -311,3 +312,21 @@ def mock_field(copyfile): parse_copyfile(mock_field((1, 2))) with pytest.raises(TypeError, match="Unrecognised type for collation copyfile"): parse_copyfile(mock_field((Mode.copy, 2))) + + +def test_argstr_formatting(): + @attrs.define + class Inputs: + a1_field: str + b2_field: float + c3_field: dict[str, str] + d4_field: list[str] + + inputs = Inputs("1", 2.0, {"c": "3"}, ["4"]) + assert ( + argstr_formatting( + "{a1_field} {b2_field:02f} -test {c3_field[c]} -me {d4_field[0]}", + inputs, + ) + == "1 2.000000 -test 3 -me 4" + )