question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support ChainableUndefined for jinja2 Environment

See original GitHub issue

Jinja 2.11 supports setting undefined option on an Environment to ChainableUndefined. This allows the default filter to work on nested elements and attributes.

Arguably this could be a better default behavior for use in copier, but regardless of the default it would be nice to be able to configure this in copier.yml. The _envops configuration exists, but there’s no way to set undefined to a python class in that YAML file (that I know of at least), so I think either a specific config value would be needed, or someway to lookup a class for the undefined key. I’m not sure which is the best or preferred way, but would like to turn this on to make the templates that use nested configuration easier to set their defaults.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
ppgcommented, Dec 3, 2020

Yes, sorry I should have included one.

This doesn’t have great support in copier (which is a different issue request), but if you have more complex configuration, say in .copier-answers.yml you have:

go:
  lint:
    changes_only: true

So that you can control when you generate a helper bash file to lint golang files whether to fail on any lint issues, or only ones that were introduced by a line your PR touches (i.e. a reviewdog pattern if anyone is interested). In my helper file to lint I want to do something like:

{%- if go.lint.changes_only|default(False) %}
golint ./...
{%- else %}
golint ./... | reviewdog -fail-on-error -f=golint -diff='git diff origin/master'
{%- endif%}

I would like that to also work for:

go: {}

or even no go key. If you can set the jinja2’s Environment’s undefined is set to ChainableUndefined it will, but with how copier has it you must do:

{%- if go and go.lint and go.lint.changes_only|default(False) %}
golint ./...
{%- else %}
golint ./... | reviewdog -fail-on-error -f=golint -diff='git diff origin/master'
{%- endif%}
0reactions
yajocommented, Dec 23, 2020

I read the linked comment, but I don’t think that’s how it works is it? At least not on v5.1.0. I routinely change .copier-answers.yml and run copier --force update and it reads the answers and then applies them.

Believe me, I’m the maintainer, I know how this thing works. 😋

I merged recently #317 to clarify this misunderstanding. The fact it works for you is an accident because you didn’t happen to hit any corner cases yet (or because you hit them but didn’t notice), but yes, that’s an unsupported way of updating, and it will always be because of technical reasons explained there.

If you have further questions regarding this subject, please open a new discussion. Let’s keep the issue focused, I hope you don’t mind. 😄

Read more comments on GitHub >

github_iconTop Results From Across the Web

API — Jinja Documentation (3.0.x)
Returns a list of templates for this environment. This requires that the loader supports the loader's list_templates() method.
Read more >
How to set the Jinja environment variable in Flask?
from flask import Flask from jinja2 import ChainableUndefined app = Flask(__name__) app.jinja_options["undefined"] = ChainableUndefined.
Read more >
jinja2.environment.Environment — Jinja2 API - GitHub Pages
The core component of Jinja is the Environment . It contains important shared variables like configuration, filters, tests, globals and others. Instances of ......
Read more >
Python Examples of jinja2.ChoiceLoader - ProgramCreek.com
Environment (loader=jinja2. ... attach the service to the loop self.loop.service = self. Example #19 ... ChainableUndefined loaderset = [jinja2.
Read more >
[ansible-project] Ansible module "template" support for Jinja2 ...
However, I don't know how to pass this new "ChainableUndefined" class to > the parameter "undefined" in the Jinja2 environment via the ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found