Search code examples
pythonarguments

How can I pass key word arguments into the function `_extract_celery_task_args(*args: tuple, **kwargs: dict)`?


So I have the following function which I am trying to unit test:

def _extract_celery_task_args(*args: tuple, **kwargs: dict):
    ....

I tried something like:

def test_extract_celery_task_args_max_four_args(mock_ConvertToParsingIntDate):
    args = ('task', 'parser_code', 'group_name', 'date')
    kwargs = {'mic': 'XEUR', 'something': 'else'}

    try:
        _extract_celery_task_args(args, kwargs)

    except ValueError as ve:
        assert False, ("More than 4 positional arguments passed to _extract_celery_task_args"
                       "function")

But when I set a breakpoint inside _extract_celery_task_args and print out args and kwargs, I get:

args = (('task', 'parser_code', 'group_name', 'date'), {'mic': 'XEUR', 'something': 'else'})
kwargs = {}

Am I passing in kwargs wrongly?


Solution

    • you are calling _extract_celery_task_args(args, kwargs), which means the args variable will be treated as a single argument, and the kwargs variable will be treated as an empty dictionary.
      To fix this:
    _extract_celery_task_args(*args, **kwargs)
    
    • By using *args and **kwargs, individual elements of args will be passed as separate positional arguments, and the key-value pairs in kwargs will be passed as keyword arguments.