You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by "Kristian Yrjölä (JIRA)" <ji...@apache.org> on 2019/03/02 11:25:00 UTC
[jira] [Created] (AIRFLOW-3997) Add accessor for optional variables
Kristian Yrjölä created AIRFLOW-3997:
----------------------------------------
Summary: Add accessor for optional variables
Key: AIRFLOW-3997
URL: https://issues.apache.org/jira/browse/AIRFLOW-3997
Project: Apache Airflow
Issue Type: Improvement
Components: models
Affects Versions: 1.10.2
Reporter: Kristian Yrjölä
Assignee: Kristian Yrjölä
h4. TL;DR
Proposing an addition to the _Variable_ accessors for optional variables. No change in existing functions.
h4. Description
Add a function to get a variable if it exists, and don't raise an error if it doesn't.
*Rationale:*
* The get function raises a _KeyError_ for a missing variable, if not careful that can bring down the whole application. Having a optional get function can make client code safer, isolating errors to the code using the specific variable only
* Sometimes there is no obvious value to set as default, but you rather want to know if the variable is missing
* The current get function doesn't support _None_ as a default value
* Optional variables are good for feature flags for instance if you want to control certain behaviours between environments before it's released in production in a continuous deployment setup
Passing in _default_var=None_ still raises a _KeyError_ since that is the default value of the _default_var_ parameter. If variable "foo" doesn't exist:
{code:java}
foo = Variable.get("foo", default_var=None)
-> KeyError{code}
You could use empty string or other values to indicate a missing variable, like this:
{code:java}
foo = Variable.get("foo", default_var="n/a")
if foo == "n/a":
handle_missing_foo(){code}
*My proposal is this:*
{code:java}
foo = Variable.optional("foo")
if foo is None:
handle_missing_foo(){code}
Which IMHO is more in line with Python's handling of a missing value.
The function should still support the _serialize_json_ and _session_ parameters, but not the _default_var_ parameter, since that would make little sense.
Of course this can still be handled on the client side with a try-catch. However it can be a natural part of the API for the _Variable_ class, replacing many try-catches in client code.
--
This message was sent by Atlassian JIRA
(v7.6.3#76005)