Is it possible to map over kwargs as asked in this...
# prefect-community
j
Is it possible to map over kwargs as asked in this Q&A? https://github.com/PrefectHQ/prefect/discussions/5014 Context: I’m trying to create a cross-product of multiple lists to map over. I want to operate on the downstream values by taking them as kwargs. Inspiration from this question answered by @Kevin Kho https://prefect-community.slack.com/archives/CL09KU1K7/p1645196983284599
Copy code
from prefect import Flow, task
from pprint import pformat


@task
def numbers():
    return [1, 2]


@task
def letters():
    return ["a", "b", "c"]


@task
def cross_product(numbers, letters):
    res = []
    for letter in letters:
        for num in numbers:
            res.append({"num": num, "letter": letter})
    return res


@task
def pretty_print(**kwargs):
    print(pformat(kwargs))


with Flow("...") as flow:
    n = numbers()
    l = letters()
    map_items = cross_product(n, l)
    pretty_print.map(map_items)

flow.run()
I get the following error:
Copy code
Traceback (most recent call last):
  File "flow.py", line 33, in <module>
    pretty_print.map(map_items)
  File "prefect/core/task.py", line 760, in map
    *args, mapped=True, upstream_tasks=upstream_tasks, flow=flow, **kwargs
  File "prefect/core/task.py", line 674, in bind
    callargs = dict(signature.bind(*args, **kwargs).arguments)  # type: Dict
  File "inspect.py", line 3015, in bind
    return args[0]._bind(args[1:], kwargs)
  File "inspect.py", line 2942, in _bind
    'too many positional arguments') from None
TypeError: too many positional arguments
k
I believe you really just can’t do it for **kwargs but I think you can just accept the dict and then work with that right?
j
yeah we could use it just as an
items
dictionary. Can we dynamically rename the task with
task_run_name="number: {items[num]} letter: {items[letter]}"
?
is that just a format string?
k
Someone told me it worked if items is the name of the input into the task