Skip to content

Python: _ styled as a keyword in case[_]: int = 1 #242

Closed as not planned
Closed as not planned

Description

@ArtMin96

Applies to the PyCaseWildcardPattern added in #236 (not on main yet).

When a snippet has no enclosing block, the pattern accepts any line that looks like a case clause. case[_]: int = 1 at the top level fits that shape, so its _ gets keyword styling even though Python reads the line as an annotated assignment to case[_].

Steps to reproduce

use Tempest\Highlight\Highlighter;

echo (new Highlighter())->parse('case[_]: int = 1', 'python');

Expected

_ stays plain. Python's ast parses the line as AnnAssign(target=Subscript(value=Name('case'), slice=Name('_'))).

Actual

case[<span class="hl-keyword">_</span>]: ...

Notes

  • This only happens at a snippet's top level. Inside an if, def or any block other than match, the line is left alone.
  • Top-level snippets are still accepted on purpose, because docs often show case _: without its match line. Telling the two apart would need something like "only = after the colon means an annotation", which a real case body such as case [x]: y = 1 breaks.

Activity

  1. brendt commented on Oct 27, 2026

    @brendt
    Member

    Is this still an issue?

  2. Amoifr commented on Oct 27, 2026

    @Amoifr
    Contributor

    For what it's worth: this one is about PyCaseWildcardPattern from #236, which was closed so it could be split into smaller PRs. On current main (fca3aa7) there is no wildcard highlighting at all (case _: stays plain, and so does case[_]: int = 1), so it doesn't reproduce there. It would only matter again if the pattern comes back in one of the split PRs.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions