Skip to content

Use raw strings for regular expressions - #2897

Open
tompollard wants to merge 1 commit into
devfrom
fix-escape-sequences
Open

tompollard wants to merge 1 commit into
devfrom
fix-escape-sequences

Conversation

@tompollard

@tompollard tompollard commented Nov 14, 2026 •

Copy link
Copy Markdown
Member

Several regexes were written as normal strings, so sequences like \w, \s and \- were invalid string escapes. They still worked because Python passes unknown escapes through unchanged, but Python 3.12 shows a SyntaxWarning for each one every time the code is loaded (they appear at the top of the CI logs), and a future Python version will make them errors.

This adds an r prefix to the 11 affected strings:

  • lightwave/views.py: the CGI header regex
  • search/urls.py: six re_path patterns for published project content and legacy redirects
  • user/test_integration.py and user/test_views.py: the email verification link regex in four tests

The resulting strings are identical, so the patterns don't change. I checked this by comparing every string constant in the four files before and after. One line in user/test_views.py is rewrapped to stay within the line length limit.

Testing

  • No SyntaxWarnings from any tracked Python file.
  • manage.py check passes with -W error::SyntaxWarning.
  • Full test suite passes (1114 tests).

Regexes like '\w' and '\s' in normal strings are invalid escape
sequences. Python 3.12 shows a SyntaxWarning for them on every
startup, and a future version will make them errors. Raw strings
give the same patterns without the warning.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant