Skip to content

[rejected AI] Ignore the codec warning for unknown string escapes - #2267

Closed
mmdverse wants to merge 1 commit into
pallets:mainfrom
mmdverse:fix/gh-1156-unknown-escape-warning
Closed

[rejected AI] Ignore the codec warning for unknown string escapes#2267
mmdverse wants to merge 1 commit into
pallets:mainfrom
mmdverse:fix/gh-1156-unknown-escape-warning

Conversation

@mmdverse

@mmdverse mmdverse commented Sep 7, 2026

Copy link
Copy Markdown

Fixes #1156.

String literals containing an escape sequence that Python does not know
(e.g. "\d" in a regex fragment) are rendered as-is, but the
unicode-escape decode in the lexer emits an internal
DeprecationWarning for them. The warning points at jinja internals
instead of the user's template, and under strict warning setups
(-W error) it becomes a TemplateSyntaxError that stops the template
from compiling.

This scopes a warnings.catch_warnings() around the decode so the
codec's warning is ignored. Notes:

  • rendered output is unchanged — verified across the escape matrix
    (valid escapes, unknown escapes, non-ASCII input, concatenated
    literals, async and sandboxed environments). Changing what an unknown
    escape renders to would be a behaviour break, so this only removes
    the spurious warning;
  • malformed sequences (\xZZ, \u12, \N{unknown name}) still raise
    TemplateSyntaxError exactly as before;
  • checked on Python 3.10, 3.12, 3.13 and 3.14 — the codec warns with
    DeprecationWarning on all of them (verified against CPython 3.14
    source as well).

Adds a regression test that promotes DeprecationWarning to an error:
it fails without the fix and passes with it.

The unicode-escape decode in the lexer emits an internal
DeprecationWarning for escape sequences it does not know (e.g.
"\d" in a regex fragment). The warning points at jinja internals
and, under -W error, breaks template compilation with a misleading
TemplateSyntaxError, even though the value is rendered as-is.

Scope a warnings.catch_warnings() around the decode so the warning
is ignored. Output is unchanged and malformed sequences (e.g.
"\xZZ", "\N{unknown name}") still raise TemplateSyntaxError.

Adds a regression test (fails without the fix, passes with it).

Fixes #1156
@davidism davidism closed this Sep 7, 2026
@davidism

davidism commented Sep 7, 2026

Copy link
Copy Markdown
Member

@davidism davidism changed the title Ignore the codec warning for unknown string escapes [rejected AI] Ignore the codec warning for unknown string escapes Sep 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

invalid escape sequence DeprecationWarning from template syntax itself

2 participants