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

Support explicitly disabling positional arguments in macros #1992

Open
phemmer opened this issue Jun 3, 2024 · 0 comments
Open

Support explicitly disabling positional arguments in macros #1992

phemmer opened this issue Jun 3, 2024 · 0 comments

Comments

@phemmer
Copy link

phemmer commented Jun 3, 2024

Currently when you define a macro, it can be called with positional arguments and named arguments. In normal python you can disable the use of positional arguments by using a asterisk, forcing any further arguments to be named. The proposal is to support the same functionality in Jinja.

For example, python allows:

def foo(bar='bar', *, a=1, b):
    ...

The proposal is to support:

{% macro foo(bar='bar', *, a=1, b) %}
    ...
{% endmacro %}

Commonly in python, this is something like *args and not just a bare *. However Jinja already has the special variable varargs. While support for *args (or any other custom name) could be supported, I figure a bare * would be a simpler change (and forward compatible if *args were to be supported).

Why?

One reason is when you don't want to support positional arguments. For example you might not want to make API guarantees that the position of arguments will remain unchanged.

Another is when you don't want to have to put default arguments at the end. It might make the code more readable to keep related arguments together, instead of having to put the ones without defaults at the front, and the ones with defaults at the end.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant