Search code examples
airflowairflow-scheduler

Airflow: set a default value in code when Variable doesn't exist without an exception


I have a little problem, I want to do the typical conditional like

setting_x  = Variable.get('setting_x')
variable = setting_x if setting_x else 0

But since the Airflow model throws an exception when the key doesn't exist is impossible to do it without trycatching and that's not very cool.

Is there any solution that I'm missing to solve that case? I've searched in the whole internet of course, but without a solution yet.

Thanks, Angel


Solution

  • You can set the default for the Variable if it doesn't exist when you're retrieving it with the get method.

    variable = Variable.get('setting_x', default_var=0)

    https://github.com/apache/airflow/blob/master/airflow/models/variable.py#L127