Skip to content

Syntax highlighting doesn't work with escape characters #25774

@cos4ni2s

Description

@cos4ni2s

\n in print("Hello\nWorld!") isn't highlighted. In fact any escape sequence, valid or invalid, isn't highlighted.
This causes confusion and hidden bugs when working with Windows paths.

path = "C:\Program Files"
print(path)

# Output:

# C:\Program Files
# <>:1: SyntaxWarning: "\P" is an invalid escape sequence. Such sequences will not work in ## the future. Did you mean "\\P"? A raw string is also an option.
# <>:1: SyntaxWarning: "\P" is an invalid escape sequence. Such sequences will not work in ## the future. Did you mean "\\P"? A raw string is also an option.
# /tmp/ipykernel_8389/650748438.py:1: SyntaxWarning: "\P" is an invalid escape sequence. # Such sequences will not work in the future. Did you mean "\\P"? A raw string is also an # option.
#   path = "C:\Program Files"

Proposal:

Every backslash (\) outside of a raw string must be treated as a meta-character and highlighted accordingly.
Additionally, any valid sequence of one or more non-white-space characters immediately following the backslash should be highlighted to indicate a recognized escape sequence.

String Context Code Highlight Behavior
Standard "Line\n" \ is Meta-color & n is escape color.
Standard: Hex "\xff" \ is Meta-color & xff is escape color.
Raw r"Line\n" \ and n are both standard string color.
Invalid "\P" \ is Meta-color & P is warning/error color.

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature-requestRequest for new features or functionalitytriage-neededNeeds assignment to the proper sub-team

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions