Skip to content
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

Introducing NeverUndefined #1923

Closed
James4Ever0 opened this issue Jan 3, 2024 · 3 comments
Closed

Introducing NeverUndefined #1923

James4Ever0 opened this issue Jan 3, 2024 · 3 comments

Comments

@James4Ever0
Copy link

James4Ever0 commented Jan 3, 2024

This feature is done by introducing a new type of undefined class called NeverUndefined.

The problem is that when using the previously most restricted undefined policy StrictUndefined is not enough.

Take example on the following template:

{% macro test(a, b, c) %}
a macro with three parameters but not used
{% endmacro %}
{{ test() }}

NeverUndefined will capture this error because in the normal case one shall pass three parameters to this macro in order to call it. StrictUndefined will not.

And this problem would be harder to debug, if one pass these undefined variables to another macro or function calls.

Take another example:

{% macro test(a, b, c) %}
append undefined parameters to list
{% set s = [] %}
{% do s.append(a) %}
{{ s }}
{% endmacro %}
{{ test() }}

StrictUndefined will not throw error. It will produce:

append undefined parameters to list
[Undefined]

However, NeverUndefined will:

  File "template.j2", line 9, in top-level template code
    {{mtest()}}
      └ <Macro 'test'>
  File "/usr/local/lib/python3.9/dist-packages/jinja2/runtime.py", line 777, in _invoke
    rv = self._func(*arguments)
         │           └ [missing, missing, missing]
         └ <Macro 'test'>
  File "template.j2", line 2, in template
    {% macro test(a, b, c) %}
  File "./test_neverundefined/../jinja_utils.py", line 99, in __init__
    raise Exception(info)
                    └ "parameter 'a' was not provided"
Exception: parameter 'a' was not provided

The implementation:

class NeverUndefined(jinja2.StrictUndefined):
    def __init__(self, *args, **kwargs):
        # ARGS: ("parameter 'myvar2' was not provided",)
        # KWARGS: {'name': 'myvar2'}
        if len(args) == 1:
            info = args[0]
        elif "name" in kwargs.keys():
            info = f"Undefined variable '{kwargs['name']}"
        else:
            infoList = ["Not allowing any undefined variable."]
            infoList.append(f"ARGS: {args}")
            infoList.append(f"KWARGS: {kwargs}")
            info = "\n".join(infoList)

        raise Exception(info)
@davidism
Copy link
Member

davidism commented Jan 3, 2024

I don't plan on adding this to Jinja core. This doesn't match the general behavior of other Undefined types. If you think it would be generally useful to others, you can release it as an extension on PyPI, there's nothing required from Jinja itself for this.

@davidism davidism closed this as not planned Won't fix, can't repro, duplicate, stale Jan 3, 2024
@James4Ever0
Copy link
Author

James4Ever0 commented Jan 3, 2024

I will release some extension for this.

The reason I do this is because in Python we do not have such concept of undefined and it should be turned off from the very beginning, as default. This concept is most likely in Javascript, where lawlessness is up to a whole new level.

@James4Ever0
Copy link
Author

James4Ever0 commented Jan 5, 2024

I don't plan on adding this to Jinja core. This doesn't match the general behavior of other Undefined types. If you think it would be generally useful to others, you can release it as an extension on PyPI, there's nothing required from Jinja itself for this.

Consider reopen this. I can tell you StrictUndefined is a mistake. I have updated examples above.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 20, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants