https://prefect.io logo
Title
b

Ben Fogelson

03/26/2020, 11:02 PM
Is there a way to have a
Parameter
whose default is the value of another
Parameter
? Something like
from prefect import task, Flow, Parameter

@task
def add(x, y):
    return x + y

with Flow('flow') as flow:
    a = Parameter('a', default=0)
    b = Parameter('b', default=a)
    c = add(a, b)
c

Chris White

03/26/2020, 11:10 PM
Hey Ben - that’s a super interesting idea that we’ve honestly never considered; it’s not supported in a first class way, but you could achieve the same behavior with the addition of a new task:
from prefect import task, Flow, Parameter

@task
def add(x, y):
    return x + y

@task
def b(a, _b):
    "Stand-in for the b Parameter"
    return a if _b is None else _b

with Flow('flow') as flow:
    a = Parameter('a', default=0)
    _b = Parameter('_b', default=None, required=False)
    c = add(a, b(a, _b))
b

Ben Fogelson

03/26/2020, 11:18 PM
@Chris White Thanks for the reply! Adding another task makes sense. It is a common enough use case for us that I’ll probably make it more reusable
from prefect import task, Flow, Parameter

@task
def add(x, y):
    return x + y

@task
def default_param(param, default):
    return param if param else default

with Flow('flow') as flow:
    a = Parameter('a', default=0)
    _b = Parameter('b', default=None, required=False)
    b = default_param(_b, a)
    c = add(a, b)
c

Chris White

03/26/2020, 11:19 PM
Glad I could help!
@Marvin archive “How to have a Parameter whose default value is another Parameter?”
b

Ben Fogelson

03/26/2020, 11:36 PM
@Chris White I think this behavior can get first class support with a couple lines added to the
run
method of `Parameter`:
def run(self) -> Any:
        params = prefect.context.get("parameters") or {}
        if self.required and self.name not in params:
            self.logger.debug(
                'Parameter "{}" was required but not provided.'.format(self.name)
            )
            raise prefect.engine.signals.FAIL(
                'Parameter "{}" was required but not provided.'.format(self.name)
            )
        if self.name not in params and isinstance(self.default, Parameter):
            return self.default.run()
        return params.get(self.name, self.default)
Are there any corner cases that would trip this up that I’m not aware of?
c

Chris White

03/26/2020, 11:39 PM
Hmmmm off the top of my head I don’t believe so; it’s an interesting idea — feel free to open an issue or even a PR and we can consider including it!
b

Ben Fogelson

03/26/2020, 11:39 PM
👍