Skip to content

Jinja2: catch and log warnings - #7365

Merged
MetRonnie merged 2 commits into
cylc:8.6.xfrom
MetRonnie:j2-warnings
Jul 7, 2026
Merged

Jinja2: catch and log warnings#7365
MetRonnie merged 2 commits into
cylc:8.6.xfrom
MetRonnie:j2-warnings

Conversation

@MetRonnie

@MetRonnie MetRonnie commented Jul 6, 2026

Copy link
Copy Markdown
Member

Follow-on from #7325 (comment)

Partly addresses #6850

Example

#!jinja2
# flow.cylc
[scheduler]
    allow implicit tasks = True
[scheduling]
    [[graph]]
        R1 = {{ 'foo' | myfilter }}
# Jinja2Filters/myfilter.py
from jinja2 import escape

def myfilter(task):
    return escape(task)
$ cylc val .
WARNING - The following warnings were raised during Jinja2 preprocessing:
    ~/cylc-run/j2/Jinja2Filters/myfilter.py:5: DeprecationWarning: 'jinja2.escape' is
    deprecated and will be removed in Jinja 3.1. Import 'markupsafe.escape' instead.
      return escape(task)
    
Valid for cylc-8.6.6.dev

Whereas before, no warning would be seen.

Note

It seems warnings that are already emitted by the time the config file is processed do not get re-emitted, so the current sre_constants warning from isodatetime will not be logged (even if the user imports sre_constants themselves. Not sure how to get around this so I'm just going to leave it as a mixed blessing, unless anyone has any ideas)

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
  • Tests are included (or explain why tests are not needed).
  • Changelog entry included if this is a change that can affect users
  • Add changelog for jinja2 warnings cylc-doc#954
  • 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 6, 2026
@MetRonnie MetRonnie self-assigned this Jul 6, 2026
@MetRonnie MetRonnie added the small label Jul 6, 2026
@MetRonnie
MetRonnie requested a review from Copilot July 6, 2026 11:50

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

This PR improves observability of Jinja2 preprocessing by capturing Python warnings raised during templating and emitting them via Cylc’s logger, helping users see deprecations (e.g. Jinja2/MarkupSafe changes) that were previously silent.

Changes:

  • Capture warnings during jinja2process(...) in read_and_proc and log them after preprocessing.
  • Modernize/clarify typing annotations (move away from typing as t, use built-in generics and | unions).

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
cylc/flow/parsec/fileparse.py Adds warnings capture around Jinja2 preprocessing and logs captured warnings; updates typing/imports.
cylc/flow/parsec/jinja2support.py Updates typing annotations/import formatting (no behavioral change).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread cylc/flow/parsec/fileparse.py
Comment thread cylc/flow/parsec/fileparse.py
Comment thread cylc/flow/parsec/fileparse.py

@oliver-sanders oliver-sanders left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

👍

Asserted that the built-in filters do not raise warnings:

#!jinja2

# {{ 'P1D' | duration_as('s') }}
# {{ 1 | pad(5) }}
# {{ '10661004T08+01' | strftime('%H') }}

Comment thread cylc/flow/parsec/fileparse.py Outdated
Comment on lines +518 to +519
"The following warnings were raised during Jinja2 "
"preprocessing:\n"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Should tailor this message to make it clearer that this is a call to action on the part of the user.

@MetRonnie MetRonnie Jul 7, 2026

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

How about?

Suggested change
"The following warnings were raised during Jinja2 "
"preprocessing:\n"
"The following warnings were raised during Jinja2 "
"preprocessing (if not addressed, your workflow may break "
"in future versions of Cylc):\n"

or

Suggested change
"The following warnings were raised during Jinja2 "
"preprocessing:\n"
"The following warnings were raised during Jinja2 "
"preprocessing (please address at the earliest "
"opportunity):\n"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It's a hard one to phrase. We really want to say, "this will break at Cylc 8.7.0", but that might not be true.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Perhaps something like:

Any Jinja2 v3.1 deprecations will break at Cylc 8.7.0

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.

If you are putting in version specific text anyway, would it be appropriate to detect 'v3.1' or something like that in the warning and alter the text to be more specific?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Effort 😅

@samuel-denton samuel-denton 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.

LGTM

@oliver-sanders oliver-sanders left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

That aught to do it.

Can remove the codicil at 8.7.0 - #7368

@MetRonnie

MetRonnie commented Jul 7, 2026

Copy link
Copy Markdown
Member Author

(Tutorial tests failing for unrelated HTTP 404, all other tests passing. Merging with 2 approvals and only minor test fix in last force push)

@MetRonnie
MetRonnie merged commit 963d576 into cylc:8.6.x Jul 7, 2026
21 of 23 checks passed
@MetRonnie
MetRonnie deleted the j2-warnings branch July 7, 2026 16:19
oliver-sanders added a commit to oliver-sanders/cylc-flow that referenced this pull request Jul 10, 2026
* Back support Jinja2 interfaces deprecated at 3.0 and removed at 3.1.
* Jinja2 renamed a bunch of functions and deprecated their old names.
* Our users didn't spot this as Python warnings are not visible to them.
* As of cylc#7365 (8.6.6) Python warnings
  originating from Jinja2 will be turned into Cylc warnings.
* To provide a migration window, the old function names will be supported
  in Cylc 8.7.x and removed in 8.8.0.
oliver-sanders added a commit to oliver-sanders/cylc-flow that referenced this pull request Jul 10, 2026
* Back support Jinja2 interfaces deprecated at 3.0 and removed at 3.1.
* Jinja2 renamed a bunch of functions and deprecated their old names.
* Our users didn't spot this as Python warnings are not visible to them.
* As of cylc#7365 (8.6.6) Python warnings
  originating from Jinja2 will be turned into Cylc warnings.
* To provide a migration window, the old function names will be supported
  in Cylc 8.7.x and removed in 8.8.0.
oliver-sanders added a commit to oliver-sanders/cylc-flow that referenced this pull request Jul 17, 2026
* Back support Jinja2 interfaces deprecated at 3.0 and removed at 3.1.
* Jinja2 renamed a bunch of functions and deprecated their old names.
* Our users didn't spot this as Python warnings are not visible to them.
* As of cylc#7365 (8.6.6) Python warnings
  originating from Jinja2 will be turned into Cylc warnings.
* To provide a migration window, the old function names will be supported
  in Cylc 8.7.x and removed in 8.8.0.
oliver-sanders added a commit to oliver-sanders/cylc-flow that referenced this pull request Jul 28, 2026
* Back support Jinja2 interfaces deprecated at 3.0 and removed at 3.1.
* Jinja2 renamed a bunch of functions and deprecated their old names.
* Our users didn't spot this as Python warnings are not visible to them.
* As of cylc#7365 (8.6.6) Python warnings
  originating from Jinja2 will be turned into Cylc warnings.
* To provide a migration window, the old function names will be supported
  in Cylc 8.7.x and removed in 8.8.0.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants