Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

normalize_callable breaking action property for positional arguments #441

Open
mlkoenig opened this issue Oct 26, 2022 · 1 comment
Open

Comments

@mlkoenig
Copy link

mlkoenig commented Oct 26, 2022

Defining a CmdAction with a positional argument leads to untestable task testing.
For instance if we use the following minimal task

from doit.action import CmdAction


def task_pos_args():

    def show_params(pos):
        return f"echo {pos}"

    return {
        'actions':[CmdAction(show_params)],
        'pos_arg': 'pos',
        'verbosity': 2,
    }

and write a test that calls the action with parametrized arguments

import pytest


@pytest.mark.parametrize("pos", ["arg", ["arg1", "arg2"]])
def test_pos_args(pos):
    cmd_action = task_pos_args()["action"][0]
    cmd = cmd_action.action().format(pos=pos)
    assert cmd == f"echo {pos}"

will fail, because calling the action property on a CmdAction with a positional argument throws the error missing 1 positional argument.

doit/doit/action.py

Lines 148 to 156 in 83309d8

@property
def action(self):
if isinstance(self._action, (str, list)):
return self._action
else:
# action can be a callable that returns a string command
ref, args, kw = normalize_callable(self._action)
kwargs = self._prepare_kwargs(self.task, ref, args, kw)
return ref(*args, **kwargs)

Looking at the above show_params is called as ref on empty args field due to normalize_callable not returning the correct signature.

doit/doit/action.py

Lines 17 to 23 in 83309d8

def normalize_callable(ref):
"""return a list with (callable, *args, **kwargs)
ref can be a simple callable or a tuple
"""
if isinstance(ref, tuple):
return list(ref)
return [ref, (), {}]

Environment

  1. OS: Linux
  2. python version: 3.9.15
  3. doit version: 0.36.0

Edit: added imports to minimal examples

Fund with Polar
@mlkoenig
Copy link
Author

As a temporary solution one can skip the action property entirely and call the underlying _action directly, e.g.

import pytest


@pytest.mark.parametrize("pos", ["arg", ["arg1", "arg2"]])
def test_pos_args(pos):
    cmd_action = task_pos_args()["action"][0]
    cmd = cmd_action._action(pos)
    assert cmd == f"echo {pos}"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant