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
Custom jinja Undefined class for handling nested undefined attributes #51768
Conversation
f1136ce
to
91729c2
Compare
This is technically probably a breaking change, since we're reimplementing I'm a big +1 for the change in behavior, so long as nobody can come up with a scenario where the current behavior actually makes sense (in which case we'd probably have to talk some combination of switch and deprecation warning). |
Huge fan of this behavior change. Like @nitzmahone , I can think of no situation where this would break that isn't already breaking. |
91729c2
to
8e2af75
Compare
8e2af75
to
932ef4b
Compare
I'd like this too. This is something I've wanted to do in when: conditions and also when using ternary filter in the past. I've added to my list to test. |
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.
Tested and works fine. My only concern is that people have been doing ugly tricks to circumvent the original behavior that may be bitten by this. (ignore_errors?) But honestly, they deserve breakage :-) This is for the better.
I agree with @dagwieers and I'm a +1 for this. This makes life a lot easier. |
I guess it should mentioned in the porting guide and changelog. Besides that, +1 from me, too! |
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.
@agaffney Seems like people are happy, though could you please add a changelog/fragment and an entry in the porting guide?
Once you've done that please poke me on IRC and I'll merge ASAP (as porting guides can end up in merge conflicts)
Adding requires changes as currently this has sh1pit
932ef4b
to
1afc036
Compare
1afc036
to
d66791e
Compare
Added changelog fragment and section in the porting guide |
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.
This is a nice addition - I've made some suggestions to make the documentation even better. Thanks @agaffney for the PR.
66b6c7f
to
62b0ec0
Compare
This commit creates a custom Jinja2 Undefined class that returns Undefined for any further accesses, rather than raising an exception
62b0ec0
to
ffc617b
Compare
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.
Looks good to me, and adding a toggle if this breaks things for people is a good back-up plan. Here's an example playbook that does change behavior:
---
- hosts: localhost
connection: local
gather_facts: no
tasks:
- name: test
command: echo "{{ dict_var.bar.baz | default('test') }}"
ignore_errors: yes
ignore_errors also prevents templating errors - e.g.
ignore_errors: yes
when: dict_var.bar.baz | default('test') is defined
Happy to see this change merged. Could we extend this further? I'd like to do: my_network: '{{ (my_project ~ "-netprivate") | default("netpublic") }}' Currently, I have to: my_network: '{{ (my_project is defined) | ternary(my_project ~ "-netprivate", "netpublic") }}' Would it be possible to override the concatenation operator in the same way? |
Doing something similar for the concatenation operator would be much more involved, and probably not worth the effort. You can use the python-style ternary syntax for that, instead.
|
Hi, Thanks for this PR. I backported it to 2.7 and it saved my life ;) One thing I'm wondering about: is it normal this new code doesn't handle accessing data with bracket notation ?
|
All this PR does is replace the class that Jinja returns when it detects an undefined value. It does not touch/change the logic for determining what is undefined or how that is handled. |
SUMMARY
This commit creates a custom Jinja2 Undefined class that returns Undefined for any further accesses, rather than raising an exception. This allows doing something like
foo.bar.baz | default('whatever')
rather than((foo | default({})).bar | default({})).baz | default('whatever')
when you don't know if all levels of the structure are defined.This was created as an alternative to #50706
ISSUE TYPE
COMPONENT NAME
templates
ADDITIONAL INFORMATION