-
Notifications
You must be signed in to change notification settings - Fork 14.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[AIRFLOW-3997] Add getter to Variable that returns None instead of throwing #4819
Conversation
I think |
@RosterIn Agree. EDIT: |
4e2f000
to
871f599
Compare
You have an error in your code:
|
871f599
to
3050f44
Compare
@OmerJog Sorry, fixed now. Struggling a bit with running the tests locally... |
airflow/models/__init__.py
Outdated
try: | ||
return cls.get(key, deserialize_json=deserialize_json, session=session) | ||
except KeyError: | ||
return None |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd like to do this without an extra method. What I propose is:
NO_DEFAULT_SENTINEL = object()
def get(cls, key, default_var=NO_DEFAULT_SENTINEL, deserialize_json=False, session=None):
obj = session.query(cls).filter(cls.key == key).first()
if obj is None:
if default_var is not NO_DEFAULT_SENTINEL:
return default_var
...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 I like this. However this will change the current behaviour when default_var
is set to None
explicitly, but maybe that is not something people do? 🤷♂️
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's probably a rare-enough case that it won't affect many people.
Still, we should add a note to UPDATING.md saying something along the lines of "if you want the old behaviour of raising when the variable is not found, ensure default_var
is not passed"
Could you update your commit message subject to "[AIRFLOW-3997] Add getter to Variable that returns None instead of throwing" please? |
5c61090
to
5879bc7
Compare
PR re-done and body edited with using the existing Can't find the |
…stead of throwing This will not change existing regular functions in the `Variable` class. If variable `foo` doesn't exist: ``` foo = Variable.get("foo") -> KeyError ``` For passing `default_var=None` to get, `None` is returned instead: ``` foo = Variable.get("foo", default_var=None) if foo is None: handle_missing_foo() ``` * Fix default so `None` works as default value for the get method * Test `None` as default, and throwing when no default is provided * Describe the default_var parameter with a `None` value in the doc, in the get variable examples section. Also describe the current behavior for `get` of raising an error when a variable is missing * Use `None` as default value in the `setdefault` method * Add description of the behaviour change in UPDATING.md
5879bc7
to
a57297c
Compare
Added a description to |
Codecov Report
@@ Coverage Diff @@
## master #4819 +/- ##
==========================================
+ Coverage 75.3% 75.56% +0.26%
==========================================
Files 450 451 +1
Lines 29023 29099 +76
==========================================
+ Hits 21855 21990 +135
+ Misses 7168 7109 -59
Continue to review full report at Codecov.
|
… found (#4819) This will not change existing regular functions in the `Variable` class. If variable `foo` doesn't exist: ``` foo = Variable.get("foo") -> KeyError ``` For passing `default_var=None` to get, `None` is returned instead: ``` foo = Variable.get("foo", default_var=None) if foo is None: handle_missing_foo() ```
… found (#4819) This will not change existing regular functions in the `Variable` class. If variable `foo` doesn't exist: ``` foo = Variable.get("foo") -> KeyError ``` For passing `default_var=None` to get, `None` is returned instead: ``` foo = Variable.get("foo", default_var=None) if foo is None: handle_missing_foo() ```
… found (apache#4819) This will not change existing regular functions in the `Variable` class. If variable `foo` doesn't exist: ``` foo = Variable.get("foo") -> KeyError ``` For passing `default_var=None` to get, `None` is returned instead: ``` foo = Variable.get("foo", default_var=None) if foo is None: handle_missing_foo() ```
… found (apache#4819) This will not change existing regular functions in the `Variable` class. If variable `foo` doesn't exist: ``` foo = Variable.get("foo") -> KeyError ``` For passing `default_var=None` to get, `None` is returned instead: ``` foo = Variable.get("foo", default_var=None) if foo is None: handle_missing_foo() ```
Make sure you have checked all steps below.
Jira
Description
Fix default so
None
works as default value for the get method.This will not change existing regular functions in the
Variable
class (unless the redundantdefault_var=None
is used). Ifvariable
foo
doesn't exist:For passing
default_var=None
to get,None
is returned instead:Tests
None
as default, and throw when no default is providedget
function to raise aKeyError
None
as default value in thesetdefault
methodCommits
Documentation
Describe the default_var parameter with a
None
value in the doc, in the get variable examples section. Also describe the current behaviour forget
raising an error when a variable is missing.Code Quality
flake8