Skip to content

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

Open

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

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