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

Provide workaround for jinja macros that use reserved words #177

Closed
tconbeer opened this issue May 5, 2022 · 2 comments · Fixed by #183
Closed

Provide workaround for jinja macros that use reserved words #177

tconbeer opened this issue May 5, 2022 · 2 comments · Fixed by #183
Labels
enhancement New feature or request
Milestone

Comments

@tconbeer
Copy link
Owner

tconbeer commented May 5, 2022

Describe the bug
jinjafmt passes jinja to black, but jinja allows some things that python does not. Specifically, variables (esp. macro arguments) can have names that are reserved words. For example:

{{ dbt_utils.star(from=ref('snowplow_page_views_30'), except=fields_to_exclude|upper) }}

This is valid jinja and dbt sql (from gitlab's project), but from and except are both reserved words in python. Running black -c "from" returns error: cannot format <string>: Cannot parse: 1:4: from

Right now jinjafmt no-ops in these situations, but it wouldn't be that hard to replace these reserved words (followed by an eq, e.g. from=) with a sentinel (e.g., from__sqlfmt__=), format that, and then replace the sentinel with the keyword again. There are 33 reserved words in python

@tconbeer tconbeer changed the title jinjafmt chokes on python reserved words that are allowed in jinja Provide workaround for jinja macros that use reserved words as arg names May 5, 2022
@tconbeer tconbeer added the enhancement New feature or request label May 5, 2022
@tconbeer
Copy link
Owner Author

tconbeer commented May 5, 2022

This could also provide a solution to #155 -- return is a reserved word, but jinja allows it as a function name. So we'd probably want to replace reserved words followed by parens, in addition to equals

@tconbeer tconbeer changed the title Provide workaround for jinja macros that use reserved words as arg names Provide workaround for jinja macros that use reserved words May 5, 2022
@tconbeer tconbeer added this to the v0.9.0 milestone May 5, 2022
@tconbeer
Copy link
Owner Author

tconbeer commented May 5, 2022

Also consider replacing ~ (jinja string concatenation) with + tilde + or similar?

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

Successfully merging a pull request may close this issue.

1 participant