Visitar URL original
fix: accept signed offsets in the Jinja2 {% now %} tag by simpleqt · Pull Request #13653 · deepset-ai/haystack · GitHub
Skip to content

fix: accept signed offsets in the Jinja2 {% now %} tag - #13653

Merged
sjrl merged 2 commits into
deepset-ai:mainfrom
simpleqt:fix-now-tag-signed-offset
Nov 12, 2026
Merged

sjrl merged 2 commits into
deepset-ai:mainfrom
simpleqt:fix-now-tag-signed-offset

Conversation

@simpleqt

Copy link
Copy Markdown
Contributor

Related Issues

  • N/A (no existing issue found)

Proposed Changes:

Jinja2TimeExtension._get_datetime built the shift amount by string concatenation: float(operator + value.strip()). For an offset value that already carries a sign — e.g. {% now 'UTC' + 'hours=-2' %} — that produced the literal "+-2" and raised ValueError: could not convert string to float: '+-2', so the tag could not express a negative offset under an explicit +. The value is now parsed numerically first and the operator's sign is applied afterwards, so the operator and the value's own sign compose (+/-2 -> -2, -/-2 -> +2). An invalid operator is still rejected explicitly, preserving the previous ValueError.

Example now supported: {% now 'UTC' + 'days=-1, hours=-2' %}.

How did you test it?

Added parametrized unit tests in test/utils/test_jinja2_extensions.py covering signed values under both + and -, signed multi-interval offsets, and equivalence of {% now 'UTC' + 'hours=-2' %} with {% now 'UTC' - 'hours=2' %}.

  • test/utils/test_jinja2_extensions.py: 24 passed (was 17; the 7 new tests fail on the unpatched source with could not convert string to float: '+-2').
  • test/utils regression: 591 passed / 2 pre-existing environment failures (missing torch in test_device.py), identical on a clean tree (584 passed / same 2 failures).
  • ruff check and ruff format clean.

Notes for the reviewer

The explicit operator not in ("+", "-") guard is deliberate: a sign-multiplier-only implementation would silently accept an invalid operator and break the existing test_invalid_operator, so the invalid-operator contract is preserved rather than replaced.

This PR was fully generated with an AI assistant. I have reviewed the changes and run the relevant tests.

@simpleqt
simpleqt requested a review from a team as a code owner November 10, 2026 06:05
@simpleqt
simpleqt requested review from sjrl and a balanced review from Copilot and removed request for a team November 10, 2026 06:05
@vercel

vercel Bot commented Nov 10, 2026

Copy link
Copy Markdown
Contributor

@simpleqt is attempting to deploy a commit to the deepset Team on Vercel.

A member of the Team first needs to authorize it.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@github-actions github-actions Bot added topic:tests type:documentation Improvements on the docs labels Nov 10, 2026
@github-actions

github-actions Bot commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

Coverage report

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  haystack/utils
  jinja2_extensions.py
Project Total  

This report was generated by python-coverage-comment-action

@sjrl sjrl self-assigned this Nov 10, 2026
Comment thread test/utils/test_jinja2_extensions.py Resolved
_get_datetime built the shift amount by string concatenation,
float(operator + value.strip()), so an offset whose value already carries a
sign produced an invalid literal: {% now 'UTC' + 'hours=-2' %} raised
ValueError: could not convert string to float: '+-2'.

Parse the value numerically first and apply the operator's sign afterwards,
so operator and value sign compose. An invalid operator is still rejected.
@simpleqt
simpleqt force-pushed the fix-now-tag-signed-offset branch from 32cf9a9 to b3df411 Compare November 12, 2026 05:04
Comment thread haystack/utils/jinja2_extensions.py Resolved

@sjrl sjrl left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@sjrl
sjrl enabled auto-merge (squash) November 12, 2026 10:27
@sjrl
sjrl merged commit 47f2aac into deepset-ai:main Nov 12, 2026
22 of 23 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

topic:tests type:documentation Improvements on the docs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants