Skip to content

AI spam#2188

Closed
rumitvn wants to merge 1 commit into
pallets:mainfrom
rumitvn:docs/operator-precedence
Closed

AI spam#2188
rumitvn wants to merge 1 commit into
pallets:mainfrom
rumitvn:docs/operator-precedence

Conversation

@rumitvn

@rumitvn rumitvn commented Jun 14, 2026

Copy link
Copy Markdown

Closes #1755
Closes #379

The Expressions section of the template docs documents each operator group (Math, Comparisons, Logic, Other Operators) but never explains how they combine when mixed. This adds an Operator Precedence subsection.

What's added

  • A precedence list of all operators, from lowest (binds least tightly) to highest (binds most tightly), derived from the parse chain in src/jinja2/parser.py (parse_condexprparse_orparse_andparse_notparse_compareparse_math1parse_concatparse_math2parse_powparse_unary → postfix/filter).
  • A note that all binary operators are left-associative, and that ** is left-associative in Jinja (unlike Python): {{ 3 ** 3 ** 3 }}(3 ** 3) ** 3.
  • The common gotcha that filters/tests bind tighter than binary operators, so {{ [3] + [2, 1] | sort }} parses as [3] + ([2, 1] | sort)[3, 1, 2], with parentheses as the fix.

Verification

Rendered the examples against Jinja 3.1.x to confirm the documented results:

  • {{ [3] + [2, 1] | sort }}[3, 1, 2]
  • {{ ([3] + [2, 1]) | sort }}[1, 2, 3]
  • {{ 3 ** 3 ** 3 }}19683 (= (3**3)**3)

Docs-only change. Closes the two long-standing requests (#1755, #379) for documenting precedence/associativity.

The template Expressions section described each operator group but never
stated how they combine. Add an 'Operator Precedence' subsection listing
all operators from lowest to highest binding (verified against the parser
in src/jinja2/parser.py), noting that all binary operators are
left-associative, that '**' is left-associative (unlike Python), and that
filters/tests bind tighter than binary operators -- the common
'[3] + [2, 1] | sort' surprise.

Closes pallets#1755
Closes pallets#379
@davidism davidism closed this Jun 14, 2026
@davidism davidism changed the title docs: document operator precedence and associativity AI spam Jun 14, 2026
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

Successfully merging this pull request may close these issues.

explain associativity and precedence between operators Jinja2 filter operator precedence is confusing

2 participants