Search code examples
pythonregexstringvisual-studio-codeautopep8

Prevent automatic escaping of backslash in strings by autopep8 in VS Code Python scripts


I have VS Code set up with the Python plugin and autopep8. My relevant settings are:

{
    "editor.defaultFormatter": "ms-python.python",   
    "editor.formatOnSave: true,
    "python.formatting.autopep8Args":[
        "--agressive"
    ]
}

I like most of what this accomplishes with regards to auto-formatting (cutting line length, replacing the odd thing here or there) but there is one feature that is really bugging me.

I am working with pySpark and have a regexp_replace function set up as

df = df.withColumn('NewCol', regexp_replace(col('OldCol'), '\W', ' ')

When I save the file, every time, the autoformatter replaces '\W' with '\\W'. I can see why its doing it (normally, a single backslash in a string is a mistakenly un-escaped character), but in this instance, I need it to stop. Are there any arguments I can pass which ignore just this case? I dont mind it never escaping a backslash again. But I would rather not turn off --agressive correction for all the other things it achieves.


Solution

  • Turning off this setting is a bad idea.

    Invalid escape sequences are depreciated as of Python 3.6 and will eventually cause a SyntaxError. Even if you currently have no intention of upgrading what Python version you're using, using invalid escape sequences will limit the portability of your code.

    Instead, use raw strings:

    df = df.withColumn('NewCol', regexp_replace(col('OldCol'), r'\W', ' ')
    

    But, if you really want to disable this setting, you can use the --ignore flag with W605, as described here.

    {
        "editor.defaultFormatter": "ms-python.python",   
        "editor.formatOnSave": true,
        "python.formatting.autopep8Args":[
            "--aggressive",
            "--ignore W605"
        ]
    }
    

    As described in the previous link, issue code W605 corresponds to "Fix invalid escape sequence 'x'".