Skip to content

Improve logging of warnings from Jinja2 processing - #7384

Merged
oliver-sanders merged 2 commits into
cylc:8.6.xfrom
MetRonnie:j2-warnings
Aug 25, 2026
Merged

Improve logging of warnings from Jinja2 processing#7384
oliver-sanders merged 2 commits into
cylc:8.6.xfrom
MetRonnie:j2-warnings

Conversation

@MetRonnie

@MetRonnie MetRonnie commented Jul 17, 2026

Copy link
Copy Markdown
Member

Follow-up to #7365

Changes the apparent source of some warnings raised during jinja2 processing from the jinja2 source code to the Cylc config file

Repro

#!jinja2
{% set x = '\${HOME}/foo/bar' %}
[scheduler]
    allow implicit tasks = True
[scheduling]
    [[graph]]
        R1 = foo

Before

$ cylc validate wflow
WARNING - The following warnings were raised during Jinja2 preprocessing (note: any Jinja 3.1 deprecations will break at Cylc 8.7):
    .../lib/python3.14/site-packages/jinja2/lexer.py:654: DeprecationWarning: "\$" is an invalid escape sequence. Such sequences will not work in the future.
      .decode("unicode-escape")

After

$ cylc validate wflow
WARNING - The following warnings were raised during Jinja2 preprocessing (note: any Jinja 3.1 deprecations will break at Cylc 8.7):
    ~/cylc-run/wflow/flow.cylc:<jinja2 template>: DeprecationWarning: "\$" is an invalid escape sequence. Such sequences will not work in the future.

Check List

  • I have read CONTRIBUTING.md and added my name as a Code Contributor.
  • Contains logically grouped changes (else tidy your branch by rebase).
  • Does not contain off-topic changes (use other PRs for other changes).
  • No dependency changes
  • No tests included as minor
  • Changelog entry not needed as follow-up to unreleased PR
  • No docs needed
  • If this is a bug fix, PR should be raised against the relevant ?.?.x branch.

@MetRonnie MetRonnie added this to the 8.6.6 milestone Jul 17, 2026
@MetRonnie
MetRonnie requested a review from oliver-sanders July 17, 2026 16:21
@MetRonnie MetRonnie self-assigned this Jul 17, 2026
@MetRonnie MetRonnie added small could be better Not exactly a bug, but not ideal. labels Jul 17, 2026
@oliver-sanders

Copy link
Copy Markdown
Member

Would be good to add a test (the branch is uncovered so could easily break), here's my attempt:

diff --git a/tests/unit/parsec/test_fileparse.py b/tests/unit/parsec/test_fileparse.py
index d0190246a7..307b3205ba 100644
--- a/tests/unit/parsec/test_fileparse.py
+++ b/tests/unit/parsec/test_fileparse.py
@@ -15,6 +15,7 @@
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 from contextlib import suppress
+import logging
 import os
 from pathlib import Path
 import re
@@ -313,6 +314,31 @@ def test_read_and_proc_no_template_engine():
         assert r == ['a=b', 'c=d', '']
 
 
+def test_read_and_proc_jinja2_warning(log_filter):
+    with NamedTemporaryFile() as tf:
+        tf.write((
+            '#!Jinja2\n'
+            r"{% set x = '\${HOME}/foo/bar' %}"
+        ).encode())
+        tf.flush()
+        read_and_proc(
+            fpath=tf.name,
+            template_vars=None,
+            viewcfg={
+                'jinja2': True,
+                'contin': False,
+                'inline': False,
+            },
+        )
+        assert log_filter(
+            level=logging.WARNING,
+            contains=(
+                f'{tf.name}:<jinja2 template>:'
+                ' DeprecationWarning: invalid escape sequence'
+            ),
+        )
+
+
 def test_inline():
     with NamedTemporaryFile() as tf:
         fpath = tf.name

@MetRonnie

Copy link
Copy Markdown
Member Author

I'm a little hesitant to add a test with a limited shelf life that will break at some point in the future on a new Python version

@oliver-sanders

oliver-sanders commented Aug 4, 2026

Copy link
Copy Markdown
Member

That's just a starter using the example in the OP, can maybe swap out the Python deprecation for something like:

{% from "warnings" import warn %}
{% do warn(...) %}

@MetRonnie

Copy link
Copy Markdown
Member Author

If I do that, the warning comes from site-packages/jinja2/runtime.py (not lexer.py) so doesn't trigger the check I've added, guess I'll have to modify it to catch that filename as well

@MetRonnie MetRonnie changed the title Improve logging of warnings from Jinja2 lexing Improve logging of warnings from Jinja2 processing Aug 4, 2026
@oliver-sanders
oliver-sanders merged commit b6d124c into cylc:8.6.x Aug 25, 2026
23 checks passed
@MetRonnie
MetRonnie deleted the j2-warnings branch August 25, 2026 11:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

could be better Not exactly a bug, but not ideal. small

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants