Skip to content

[rejected AI] ignore invalid escape sequence warning when unescaping string literals (#1156) - #2264

Closed
amasen02 wants to merge 1 commit into
pallets:mainfrom
amasen02:fix/lexer-unescape-deprecation-warning-1156
Closed

[rejected AI] ignore invalid escape sequence warning when unescaping string literals (#1156)#2264
amasen02 wants to merge 1 commit into
pallets:mainfrom
amasen02:fix/lexer-unescape-deprecation-warning-1156

Conversation

@amasen02

@amasen02 amasen02 commented Sep 4, 2026

Copy link
Copy Markdown

fixes #1156

Problem

When string literals in templates contain unrecognized escape sequences (such as regex patterns like "\d", "\w", "\s"), Python's decode("unicode-escape") emits a DeprecationWarning (or SyntaxWarning in Python 3.12+).

Under strict warning configurations such as python -Werror or test suites with warning filters enabled, this warning is converted into an exception during decoding. Jinja's lexer catches this in except Exception as e: and raises a TemplateSyntaxError: invalid escape sequence '\d').

Solution

Wrap the decode("unicode-escape") call inside Lexer.tokeniter in a warning filter context that ignores DeprecationWarning and SyntaxWarning. This permits template string literals containing regex patterns or unrecognized escape sequences to be unescaped without raising warnings or failing when -Werror is active.

Verification

  • Added unit test test_unrecognized_escape_sequence in tests/test_lexnparse.py verifying template strings with \d \w \s parse and render properly without raising warnings.
  • Ran test suite under -Werror with pytest: 912/912 tests passed 100% green.
  • Ran ruff check src tests and ruff format --check src tests: all clean.
  • Added changelog entry in CHANGES.rst.

pallets#1156)

When template strings contain unrecognized escape sequences (e.g. '\d', '\w'), Python emits a DeprecationWarning (or SyntaxWarning in Python 3.12+) when calling decode('unicode-escape'). Under -Werror, this warning is raised as an exception and caught by Jinja as a TemplateSyntaxError.

Ignore DeprecationWarning and SyntaxWarning during decode('unicode-escape') in string unescaping so templates with raw regex/escape patterns parse cleanly under strict warning filters.

Fixes pallets#1156
@davidism

davidism commented Sep 4, 2026

Copy link
Copy Markdown
Member

@davidism davidism closed this Sep 4, 2026
@davidism davidism changed the title fix(lexer): ignore invalid escape sequence warning when unescaping string literals (#1156) [rejected AI] ignore invalid escape sequence warning when unescaping string literals (#1156) Sep 4, 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