transformers icon indicating copy to clipboard operation
transformers copied to clipboard

Avoid invalid escape sequences, use raw strings

Open TurtleOrangina opened this issue 2 years ago • 1 comments

What does this PR do?

Fixes invalid escape sequences in strings, they are illegal in python, and will throw a SyntaxError if run with "-W error", and always throw a syntax error starting with python-3.12 (planned). With python between 3.6 and 3.11 and "-W default" they produce a DeprecationWarning:

> python -W error -c '"(.*?)-\d{5}-of-\d{5}"'                                                                                                                                 ✔    File "<string>", line 1
    "(.*?)-\d{5}-of-\d{5}"
    ^^^^^^^^^^^^^^^^^^^^^^
SyntaxError: invalid escape sequence '\d'

This has been fixed in the past, e.g. https://github.com/huggingface/transformers/pull/4924 - but missing linter support and the fact that python only fails this with "-W" flag set has let the issue be re-introduced. This PR fixes those occurrences and enables ruff "W605" error, which will prevent this for the future.

Who can review?

Maybe @sgugger who added ruff in the first place or (Why was "W605" disabled when switching to ruff?) @patrickvonplaten wo recently introduced some invalid escape sequences @LysandreJik who merged the linked fix from 2020

TurtleOrangina avatar Apr 22 '23 11:04 TurtleOrangina

The documentation is not available anymore as the PR was closed or merged.

Failures are unrelated to this PR and due to the last release of huggingface_hub. All is fixed on main so merging :-)

sgugger avatar Apr 24 '23 20:04 sgugger

Rebased on top of current main branch to pass the tests.

TurtleOrangina avatar Apr 25 '23 07:04 TurtleOrangina

Rebased on top of current main branch to pass the tests.

Does not seem to help the tests, let me know if I should do anything else for this PR

TurtleOrangina avatar Apr 25 '23 08:04 TurtleOrangina

The Hub is currently having high-response times due to some abusive traffic, which is why the tests are all red. I just forgot to push the merge button yesterday, so merging this as it shouldn't have any negative impact on main.

sgugger avatar Apr 25 '23 13:04 sgugger