Skip to content

gh-140334: Fix IDLE syntax highlighting after line continuations - #140335

Open
TheLizzard wants to merge 6 commits into
python:mainfrom
TheLizzard:fix-issue-140334
Open

TheLizzard wants to merge 6 commits into
python:mainfrom
TheLizzard:fix-issue-140334

Conversation

@TheLizzard

Copy link
Copy Markdown

Description

This PR fixes incorrect syntax highlighting in IDLE when a line ends with a backslash (\) used for line continuation.

Previously

Identifiers following a backslash were sometimes misinterpreted as keywords or built-ins instead of variables or attributes.
For example in:

x = match if match else \
    match

The last match was coloured like a keyword instead of a variable.

Summary of changes

  • Updated IDLE's syntax highlighting to properly treat backslash line continuations
  • Added tests for cases involving identifiers following a line continuation backslash

Fixes gh-140334

@python-cla-bot

python-cla-bot Bot commented Oct 19, 2025

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

@StanFromIreland StanFromIreland left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can confirm the fix works on Linux:

image

But during typing that I noticed that the keyword pattern also suffers from this issue:

image

I think this regex approach is getting very messy, the builtin get's quite a bit more complex. I assume that brings significant performance penalties. At a minium I would suggest refactoring some things, rather than having to explain the lookahead each time move it to some constant e.g. LAST_LINE_NO_CONTINUATION. At best I think we could possibly try an reuse the new repl's logic, which uses tokens at not such excessive regexs.

Comment thread Lib/idlelib/colorizer.py
@@ -17,6 +17,7 @@ def any(name, alternates):
def make_pat():
kw = r"\b" + any("KEYWORD", keyword.kwlist) + r"\b"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This pattern also suffers from the issue.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the code str.for will always raise a SyntaxError, colouring in the for like a keyword will help beginners recognise that keywords are reserved and cannot be used as variable names/attributes. I believe that the keyword pattern is working correctly. I updated the match/case patterns since they are soft keywords and are valid variable/attribute names.

Comment thread Misc/NEWS.d/next/IDLE/2025-10-19-18-43-07.gh-issue-140334.3mdyHm.rst Outdated
TheLizzard and others added 2 commits October 19, 2025 19:31
…Hm.rst


Made by @StanFromIreland

Co-authored-by: Stan Ulbrych <89152624+StanFromIreland@users.noreply.github.com>
@StanFromIreland

Copy link
Copy Markdown
Member

Please do not use the Update Branch button unless necessary (e.g. fixing conflicts, jogging the CI, or very old PRs) as it uses valuable resources. For more information see the devguide.

@TheLizzard

Copy link
Copy Markdown
Author

I did some profiling and here are my results:

  • Parsing and iterating over the text tags, IDLE can parse ~46k lines a second.
  • Parsing and adding the tags to the tkinter.Text widget, IDLE can parse ~10k lines a second.
    Both of these measurements were made parsing Lib/tkinter/__init__.py (since it's nearly 5k lines long).

I think that re-writing it with _pyrepl will improve the highlighting (eg. nested f-strings) but won't improve the speed by a lot (since tkinter is the bottleneck). I haven't used _pyrepl but since it was made for a REPL, I don't know if it will create more bugs in IDLE than solve. I can write more test cases since IDLE is sorely lacking in that department.

@github-actions

github-actions Bot commented May 1, 2026

Copy link
Copy Markdown

This PR is stale because it has been open for 30 days with no activity.

@github-actions github-actions Bot added the stale Stale PR or inactive for long period of time. label May 1, 2026
The lazy soft keyword added in pythonGH-142351 gets the same continuation-line
guard as the other soft keywords.

@serhiy-storchaka serhiy-storchaka left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The approach is sound; I verified the cases from the issue and the tests with a display. Two changes and one note:

  1. The builtin pattern guards against starting inside indentation with (?<!\.| ), a space only. With tab indentation the match can start at the second tab, past the backslash check, so self.\ + newline + \t\tset colors set as a builtin again. Use (?<![. \t]) — the tests still pass with it.
  2. "slash" → "backslash" in the comments and the NEWS entry.
  3. A comment ending with a backslash suppresses a soft keyword on the next line (# see \ + newline + match x:). Acceptable for a regex-based colorizer; the tokenizer-based one (gh-140347) would fix it.

I merged main to resolve the conflict with the lazy soft keyword from PEP 810; it now gets the same continuation guard as match and case.

@bedevere-app

bedevere-app Bot commented Sep 15, 2026

Copy link
Copy Markdown

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting changes stale Stale PR or inactive for long period of time.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

IDLE: Wrong highlighting when previous line ends with backslash

4 participants