Repository navigation
✨ jinja: add round, sum, min and max filters - #2885
Open
dmitriybabichev wants to merge 1 commit into
Open
dmitriybabichev wants to merge 1 commit into
dmitriybabichev wants to merge 1 commit into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The jinja package has no
round,sum,minormaxfilters, so templates using them fail with "Unknown NumericValue filter: round" (or "Unknown ArrayValue filter: sum", etc.) while Python Jinja2 renders them.This adds all four, following the Jinja2 signatures:
round(precision=0, method='common'|'ceil'|'floor'),sum(attribute=None, start=0),min/max(case_sensitive=False, attribute=None).Details worth checking against Python:
roundwith the default method is Python'sround(), so ties go to the even digit (2.5|roundis2.0) and2.675|round(2)is2.67; ints stay ints and floats stay floats;sumreturns a float as soon as a float is involved;min/maxon an empty list give undefined and keep the first of equal items.Tests are in
test/numeric-filters.test.js; expected values were generated with Jinja2 3.1.vitest runpasses excepte2e.test.js, which fails to resolve@huggingface/hubin my checkout both with and without this change.tscandoxfmt --checkare clean.Note
Low Risk
Scoped runtime filter additions with dedicated tests; no auth or data-path changes, mainly template compatibility.
Overview
Adds Python Jinja2–compatible built-in filters
round,sum,min, andmaxso templates that use them no longer fail at runtime.roundsupportsprecision,method(common|ceil|floor), and keyword args. Defaultcommonuses a newroundHalfEvenhelper (ties to even, decimal-aware behavior like2.675|round(2)→2.67); integers/booleans stay integers when appropriate, floats stay floats.sumworks on arrays with optionalattributeandstart, promoting to float when any operand is float.min/maxcompare items (or anattribute) via existingcompareRuntimeValues, honorcase_sensitive, returnundefinedon empty lists, and keep the first tie.Filter dispatch in
applyFilterroutes these four names (with or without call syntax) throughapplyNumericFilter. Newnumeric-filters.test.jsasserts outputs against Jinja2 3.1 expectations.Reviewed by Cursor Bugbot for commit 9b977d0. Bugbot is set up for automated code reviews on this repo. Configure here.