Skip to content

Span for invalid escape sequence in multiline strings is wrong #116042

Closed
@konstin

Description

@konstin

Bug report

Bug description:

a = """
Invalid\ Escape
"""

When running with PYTHONWARNINGS=error python3.13 example.py, i get the correct error that there is an invalid escape sequence, but the error span is located at the beginning of the string, not at the location of the actual error:

  File "/home/konsti/example.py", line 1
    a = """
        ^
SyntaxError: invalid escape sequence '\ '

Similarly, for docstrings, the opening quotes are marked, not the actual location:

def f():
    """This function computes f.
    Invalid\ Escape
    """
$ PYTHONWARNINGS=error python3.13 example.py 
  File "/home/konsti/example.py", line 2
    """This function computes f.
    ^^^
SyntaxError: invalid escape sequence '\ '

This makes it look like the file is somehow corrupted or there is an encoding error rather than checking the actual docstring (astral-sh/uv#1928).

Python 3.13.0a1+, installed with pyenv.

I'd expected this to have been reported before, but searching for "invalid escape sequence strings", "escape sequence span" and "SyntaxWarning location" i didn't find anything matching.

CPython versions tested on:

3.13

Operating systems tested on:

Linux

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.13bugs and security fixesinterpreter-core(Objects, Python, Grammar, and Parser dirs)

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions