Skip to content

python: Fix Python exception highlighting - #58176

Merged
MrSubidubi merged 2 commits into
zed-industries:mainfrom
allachance:fix-python-exceptions-highlighting
Jun 5, 2026
Merged

python: Fix Python exception highlighting#58176
MrSubidubi merged 2 commits into
zed-industries:mainfrom
allachance:fix-python-exceptions-highlighting

Conversation

@allachance

@allachance allachance commented May 31, 2026

Copy link
Copy Markdown
Contributor

In the current Python Tree-sitter highlights.scm, exception (https://docs.python.org/3/library/exceptions.html) are highlighted under @type.class which is inconsistent compared to other editors (VS Code, Neovim, Helix).

This PR adds #any-of? with a list of python's exceptions and highlight them under @type.builtin.

Before After
image image

Self-Review Checklist:

  • I've reviewed my own diff for quality, security, and reliability
  • Unsafe blocks (if any) have justifying comments
  • The content is consistent with the UI/UX checklist
  • Tests cover the new/changed behavior
  • Performance impact has been considered and is acceptable

Release Notes:

  • Added the ability to stylize builtin python exceptions and warnings using the type.class.builtin syntax capture.

@cla-bot cla-bot Bot added the cla-signed The user has signed the Contributor License Agreement label May 31, 2026
@zed-community-bot zed-community-bot Bot added the first contribution the author's first pull request to Zed. NOTE: the label application is automated via github actions label May 31, 2026

@MrSubidubi MrSubidubi 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.

As for this one, I am somewhat unsure whether this is desirable or not - I see VSCode does this, but Helix does not.

To me this seems that it introduces some inconcistency based on the error type used which might be unexpected and odd for highlighting, but I am happy to listen to your opinion here.

@MrSubidubi MrSubidubi self-assigned this Jun 1, 2026
@allachance

Copy link
Copy Markdown
Contributor Author

That's a fair point. My reasoning is that these exceptions are part of Python's built-in classes, so highlighting them as @type.builtin seems consistent with how other built-in Python objects are highlighted in highlights.scm.

I would add that putting Python's exceptions under @type.builtin is not unique to VSCode. Helix and nvim-treesitter both explicitly highlight them as @type.builtin as well (See below)

Let me know what you think.

Helix: https://github.com/helix-editor/helix/blob/master/runtime/queries/python/highlights.scm

; Builtin error types
((identifier) @type.builtin ; Has to be after constructors due to broad matching of constructor
  (#any-of? @type.builtin
    "BaseException" "Exception" "ArithmeticError" "BufferError" "LookupError"
    "AssertionError" "AttributeError" "EOFError" "FloatingPointError" "GeneratorExit"
    "ImportError" "ModuleNotFoundError" "IndexError" "KeyError" "KeyboardInterrupt"
    "MemoryError" "NameError" "NotImplementedError" "OSError" "OverflowError"
    "RecursionError" "ReferenceError" "RuntimeError" "StopIteration" "StopAsyncIteration"
    "SyntaxError" "IndentationError" "TabError" "SystemError" "SystemExit" "TypeError"
    "UnboundLocalError" "UnicodeError" "UnicodeEncodeError" "UnicodeDecodeError"
    "UnicodeTranslateError" "ValueError" "ZeroDivisionError" "EnvironmentError"
    "IOError" "WindowsError" "BlockingIOError" "ChildProcessError" "ConnectionError"
    "BrokenPipeError" "ConnectionAbortedError" "ConnectionRefusedError"
    "ConnectionResetError" "FileExistsError" "FileNotFoundError" "InterruptedError"
    "IsADirectoryError" "NotADirectoryError" "PermissionError" "ProcessLookupError"
    "TimeoutError" "Warning" "UserWarning" "DeprecationWarning" "PendingDeprecationWarning"
    "SyntaxWarning" "RuntimeWarning" "FutureWarning" "ImportWarning" "UnicodeWarning"
    "BytesWarning" "ResourceWarning"))

nvim-treesitter: https://github.com/nvim-treesitter/nvim-treesitter/blob/main/runtime/queries/python/highlights.scm

((identifier) @type.builtin
  (#any-of? @type.builtin
    ; https://docs.python.org/3/library/exceptions.html
    "BaseException" "Exception" "ArithmeticError" "BufferError" "LookupError" "AssertionError"
    "AttributeError" "EOFError" "FloatingPointError" "GeneratorExit" "ImportError"
    "ModuleNotFoundError" "IndexError" "KeyError" "KeyboardInterrupt" "MemoryError" "NameError"
    "NotImplementedError" "OSError" "OverflowError" "RecursionError" "ReferenceError" "RuntimeError"
    "StopIteration" "StopAsyncIteration" "SyntaxError" "IndentationError" "TabError" "SystemError"
    "SystemExit" "TypeError" "UnboundLocalError" "UnicodeError" "UnicodeEncodeError"
    "UnicodeDecodeError" "UnicodeTranslateError" "ValueError" "ZeroDivisionError" "EnvironmentError"
    "IOError" "WindowsError" "BlockingIOError" "ChildProcessError" "ConnectionError"
    "BrokenPipeError" "ConnectionAbortedError" "ConnectionRefusedError" "ConnectionResetError"
    "FileExistsError" "FileNotFoundError" "InterruptedError" "IsADirectoryError"
    "NotADirectoryError" "PermissionError" "ProcessLookupError" "TimeoutError" "Warning"
    "UserWarning" "DeprecationWarning" "PendingDeprecationWarning" "SyntaxWarning" "RuntimeWarning"
    "FutureWarning" "ImportWarning" "UnicodeWarning" "BytesWarning" "ResourceWarning"
    ; https://docs.python.org/3/library/stdtypes.html
    "bool" "int" "float" "complex" "list" "tuple" "range" "str" "bytes" "bytearray" "memoryview"
    "set" "frozenset" "dict" "type" "object"))

VSCode (MagicPython): https://github.com/MagicStack/MagicPython/blob/master/grammars/MagicPython.tmLanguage

  (
    Arithmetic | Assertion | Attribute | Buffer | BlockingIO
    | BrokenPipe | ChildProcess
    | (Connection (Aborted | Refused | Reset)?)
    | EOF | Environment | FileExists | FileNotFound
    | FloatingPoint | IO | Import | Indentation | Index | Interrupted
    | IsADirectory | NotADirectory | Permission | ProcessLookup
    | Timeout
    | Key | Lookup | Memory | Name | NotImplemented | OS | Overflow
    | Reference | Runtime | Recursion | Syntax | System
    | Tab | Type | UnboundLocal | Unicode(Encode|Decode|Translate)?
    | Value | Windows | ZeroDivision | ModuleNotFound
  ) Error
|
  ((Pending)?Deprecation | Runtime | Syntax | User | Future | Import
    | Unicode | Bytes | Resource
  )? Warning
|
  SystemExit | Stop(Async)?Iteration
  | KeyboardInterrupt
  | GeneratorExit | (Base)?Exception
)\b
</string>
      </dict>
      <key>builtin-functions</key>
      <dict>
        <key>patterns</key>
        <array>
          <dict>
            <key>name</key>
            <string>support.function.builtin.python</string>
            <key>match</key>
            <string>(?x)

@dinocosta dinocosta added the area:languages/python Python programming language support label Jun 2, 2026
@MrSubidubi

Copy link
Copy Markdown
Member

I very much appreciate you doing your research on these topics and providing the context here, helps a lot with reviewing.

That said, I almost find this to be the most controversial of your PRs to a degree, because I very much dislike the ambiguity this introduces.

How about we meet in the middle and go with type.class.builtin as a capture? This would have the disadvantage of requiring adoption, but would enable users and theme authors to both opt-in and opt-out to this highlighting

@allachance

allachance commented Jun 3, 2026

Copy link
Copy Markdown
Contributor Author

You're welcome !

type.class.builtin works for me, I think it's good middle ground. As long as it aligns with you guys overall design philosophy, I’m fine with it.

I’ve updated the branch, please let me know if anything else is needed here.

Thanks for the review !

@MrSubidubi MrSubidubi 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.

Think this is looking good now. Thanks for this improvement and your input! Appreciate it.

@MrSubidubi
MrSubidubi added this pull request to the merge queue Jun 5, 2026
Merged via the queue into zed-industries:main with commit 3c0f5a0 Jun 5, 2026
34 checks passed
TomPlanche pushed a commit to TomPlanche/zed that referenced this pull request Jun 8, 2026
In the current Python Tree-sitter `highlights.scm`, exception
(https://docs.python.org/3/library/exceptions.html) are highlighted
under `@type.class` which is inconsistent compared to other editors (VS
Code, Neovim, Helix).

This PR adds `#any-of?` with a list of python's exceptions and highlight
them under `@type.builtin`.

| Before | After |
|
:---------------------------------------------------------------------------------------------------------------------------------:
|
:--------------------------------------------------------------------------------------------------------------------------------:
|
| <img width="1237" height="1160" alt="image"
src="https://github.com/user-attachments/assets/05e1ae34-7637-413a-9785-ad696c2bf751"
/> | <img width="1237" height="1154" alt="image"
src="https://github.com/user-attachments/assets/105c61ca-b6a7-4c26-acc8-3e300f044510"
/> |


Self-Review Checklist:

- [x] I've reviewed my own diff for quality, security, and reliability
- [x] Unsafe blocks (if any) have justifying comments
- [x] The content is consistent with the [UI/UX
checklist](https://github.com/zed-industries/zed/blob/main/CONTRIBUTING.md#uiux-checklist)
- [ ] Tests cover the new/changed behavior
- [x] Performance impact has been considered and is acceptable

Release Notes:

- Added the ability to stylize builtin python exceptions and warnings
using the `type.class.builtin` syntax capture.
This was referenced Jun 18, 2026
jonx pushed a commit to jonx/zed-aros that referenced this pull request Jul 17, 2026
In the current Python Tree-sitter `highlights.scm`, exception
(https://docs.python.org/3/library/exceptions.html) are highlighted
under `@type.class` which is inconsistent compared to other editors (VS
Code, Neovim, Helix).

This PR adds `#any-of?` with a list of python's exceptions and highlight
them under `@type.builtin`.

| Before | After |
|
:---------------------------------------------------------------------------------------------------------------------------------:
|
:--------------------------------------------------------------------------------------------------------------------------------:
|
| <img width="1237" height="1160" alt="image"
src="https://github.com/user-attachments/assets/05e1ae34-7637-413a-9785-ad696c2bf751"
/> | <img width="1237" height="1154" alt="image"
src="https://github.com/user-attachments/assets/105c61ca-b6a7-4c26-acc8-3e300f044510"
/> |


Self-Review Checklist:

- [x] I've reviewed my own diff for quality, security, and reliability
- [x] Unsafe blocks (if any) have justifying comments
- [x] The content is consistent with the [UI/UX
checklist](https://github.com/zed-industries/zed/blob/main/CONTRIBUTING.md#uiux-checklist)
- [ ] Tests cover the new/changed behavior
- [x] Performance impact has been considered and is acceptable

Release Notes:

- Added the ability to stylize builtin python exceptions and warnings
using the `type.class.builtin` syntax capture.
jolutz pushed a commit to jolutz/zed that referenced this pull request Aug 8, 2026
In the current Python Tree-sitter `highlights.scm`, exception
(https://docs.python.org/3/library/exceptions.html) are highlighted
under `@type.class` which is inconsistent compared to other editors (VS
Code, Neovim, Helix).

This PR adds `#any-of?` with a list of python's exceptions and highlight
them under `@type.builtin`.

| Before | After |
|
:---------------------------------------------------------------------------------------------------------------------------------:
|
:--------------------------------------------------------------------------------------------------------------------------------:
|
| <img width="1237" height="1160" alt="image"
src="https://github.com/user-attachments/assets/05e1ae34-7637-413a-9785-ad696c2bf751"
/> | <img width="1237" height="1154" alt="image"
src="https://github.com/user-attachments/assets/105c61ca-b6a7-4c26-acc8-3e300f044510"
/> |


Self-Review Checklist:

- [x] I've reviewed my own diff for quality, security, and reliability
- [x] Unsafe blocks (if any) have justifying comments
- [x] The content is consistent with the [UI/UX
checklist](https://github.com/zed-industries/zed/blob/main/CONTRIBUTING.md#uiux-checklist)
- [ ] Tests cover the new/changed behavior
- [x] Performance impact has been considered and is acceptable

Release Notes:

- Added the ability to stylize builtin python exceptions and warnings
using the `type.class.builtin` syntax capture.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:languages/python Python programming language support cla-signed The user has signed the Contributor License Agreement first contribution the author's first pull request to Zed. NOTE: the label application is automated via github actions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants