-
Notifications
You must be signed in to change notification settings - Fork 67
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
Fix escape sequences #591
base: main
Are you sure you want to change the base?
Fix escape sequences #591
Conversation
@mblondel Any chance we can get this in soon? |
On Python 3.12, this gives:
|
@NeilGirdhar it should also be fixed here. Line 68 in 9552f39
+1 for merging, this breaks GPJax's tests for me. |
Do you mind doing that in separate PR? I don't use jaxopt anymore and I don't want to deal with the added back-and-forth :/ I think that one should a raw string if it's being passed to re, but I don't want to take the time to figure it out :) |
A raw string is a Python syntactic construct to create normal strings, but the result is just a normal string once it's passed to a regex function (that is, there's no such thing as a "raw string" type as far as I'm aware). What's shown above is just a normal string, and the same warning applies. (I found this error automatically with pyright). Compare >>> r"_test\.py"
'_test\\.py'
>>> type(r"_test\.py")
<class 'str'>
>>> "_test\.py"
<stdin>:1: SyntaxWarning: invalid escape sequence '\.'
'_test\\.py'
>>> "_test\.py" == "_test\\.py" == r"_test\.py"
<stdin>:1: SyntaxWarning: invalid escape sequence '\.'
True This is minor though, since |
Yes, I know. The reason it should be an r-string is because conventionally (according to linters like Ruff), r-strings should be passed as the match pattern of an re-match regardless of whether or not they take advantage of any of differences. So, your first way of writing it is the conventional way that it should be written rather than the equivalent non-r-string. |
Which ruff rule are you referring to? import re
re.match("_test\\.py", "_test\\.py") ruff check test.py
All checks passed! Whether it's |
I think the logic behind the rule is that it's a bug magnet if the pattern changes, and in this case, it's easier to read without the double-escaping. |
Fixes #598