pallets / jinja

A very fast and expressive template engine.
https://jinja.palletsprojects.com
BSD 3-Clause "New" or "Revised" License
10.23k stars 1.6k forks source link

Eliminating undefined parameters entirely by `NeverUndefined` #1924

Closed James4Ever0 closed 8 months ago

James4Ever0 commented 8 months ago

This pull request is done by changing two files in the source code directory. It integrates the NeverUndefined class and make it usable by:

import jinja2
# access by: jinja2.NeverUndefined

This will raise exception for the following template, while StrictUndefined will not:

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

{{ test() }}

Checklist:

ThiefMaster commented 8 months ago

did you create the PR using an LLM? the code and comments in there surely look like that's the case...

You also don't explain when your code would actually be useful...

James4Ever0 commented 8 months ago

did you create the PR using an LLM? the code and comments in there surely look like that's the case...

You also don't explain when your code would actually be useful...

This code is half a year old and I do it for fixing this long standing error.

I want to explain but my memory is fuzzy. It is useful.

About your suspicion, I can tell that I had the feeling too. I am building a project named Cybergod, which can control mouse and keyboard and possibly do stuffs like browsing Github, making pull requests :)

The comment in the code is to indicate what were passed as *args and **kwargs at that location. I reason about the *args are not so easy to be parsed, neither as the **kwargs.

I decide to collect the info from *args and **kwargs as error message, and raise Exception right after __init__, not waiting for invocation, unlike StrictUndefined.

davidism commented 8 months ago

See issue for slightly longer explanation. If you think this is generally useful to others, you can create an extension, but there's nothing requiring it to be in Jinja core.