Skip to content

fix(python): fix invalid escape sequences #2987

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 24, 2025
Merged

Conversation

e-kwsm
Copy link
Contributor

@e-kwsm e-kwsm commented Jun 9, 2025

Description

Regex such as '\d' is invalid, which must be r'\d' or '\\d'.

Copy link

codecov bot commented Jun 9, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 90.98%. Comparing base (416b075) to head (d20901b).
Report is 2 commits behind head on devel.

Additional details and impacted files
@@           Coverage Diff           @@
##            devel    #2987   +/-   ##
=======================================
  Coverage   90.98%   90.98%           
=======================================
  Files         198      198           
  Lines        8599     8599           
=======================================
  Hits         7823     7823           
  Misses        776      776           
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@ChrisThrasher
Copy link
Collaborator

Could you please explain what makes these sequences invalid?

@e-kwsm
Copy link
Contributor Author

e-kwsm commented Jun 22, 2025

See https://docs.python.org/3.13/library/re.html.

The solution is to use Python’s raw string notation for regular expression
patterns; backslashes are not handled in any special way in a string literal
prefixed with 'r'. So r"\n" is a two-character string containing
'' and 'n', while "\n" is a one-character string containing a
newline. Usually patterns will be expressed in Python code using this raw
string notation.

Python 3.13.3 (main, Apr  9 2025, 07:44:25) [GCC 14.2.1 20250207] on linux
>>> s = "\d"   # wrong
<python-input-0>:1: SyntaxWarning: invalid escape sequence '\d'
>>> s = r"\d"  # correct

@ChrisThrasher ChrisThrasher requested a review from horenmar June 24, 2025 16:51
Copy link
Member

@horenmar horenmar left a comment

Choose a reason for hiding this comment

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

@ChrisThrasher The changes are correct.

Honestly it is kinda fascinating that the scripts work, I am surprised that after Python sees the escape is not valid, it just leaves the literal backslash and letter in there.

@ChrisThrasher ChrisThrasher merged commit ceed268 into catchorg:devel Jun 24, 2025
92 checks passed
@e-kwsm e-kwsm deleted the W605 branch June 25, 2025 09:51
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.

3 participants