GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Improve `SyntaxError` message for `case ... as a.b` · Issue #123562 · python/cpython

Improve SyntaxError message for case ... as a.b #123562

New issue
Copy link
New issue
Copy link
Closed
Closed
Improve SyntaxError message for case ... as a.b#123562
Copy link
Assignees
sobolevn
Labels
interpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)topic-parsertype-featureA feature request or enhancementA feature request or enhancement

Description

@sobolevn
sobolevn
opened on Sep 1, 2024
Issue body actions

Feature or enhancement

There's already a similar rule:

cpython/Grammar/python.gram

Line 1378 in 084e0f3

| or_pattern 'as' !NAME a=expression { RAISE_SYNTAX_ERROR_KNOWN_LOCATION(a, "invalid pattern target") }

But, it has two problems:

  1. It does not work for cases like as a.b, because a matches NAME
  2. It does not show rich error message, only a static one: invalid pattern target

My proposed change:

    | or_pattern 'as' a=expression {
        RAISE_SYNTAX_ERROR_KNOWN_LOCATION(
            a, "cannot use pattern target as %s", _PyPegen_get_expr_name(a)) }

Why is it safe?

Here's how the parent rule is defined:

as_pattern[pattern_ty]:
    | pattern=or_pattern 'as' target=pattern_capture_target {
        _PyAST_MatchAs(pattern, target->v.Name.id, EXTRA) }
    | invalid_as_pattern

So, if pattern=or_pattern 'as' target=pattern_capture_target with a valid 'as' NAME is matched, we won't fall to the next invalid_ rule.

Proposed result:

>>> match 1:
...     case x as a.b: ...
...     
  File "<python-input-0>", line 2
    case x as a.b: ...
              ^^^
SyntaxError: cannot use pattern target as attribute

Refs #123440

Linked PRs

  • gh-123562: Improve SyntaxError message for case ... as a.b #123563

Activity

  1. sobolevn
    added
    type-featureA feature request or enhancementA feature request or enhancement
    interpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)
    topic-parser
    on Sep 1, 2024
  2. sobolevn
    self-assigned this
    on Sep 1, 2024
  3. sobolevn
    added a commit that references this issue on Sep 1, 2024

    pythongh-123562: Improve `SyntaxError` message for `case ... as a.b`

    c0cf895
  4. cfbolz commented on Sep 2, 2024

    @cfbolz
    cfbolz
    on Sep 2, 2024
    Contributor
    More actions

    Right, sorry, should have commented here too and not just on the PR. Imo it should be SyntaxError: cannot use attribute as pattern target

    Reacted by sobolevn
  5. lysnikolaou
    added a commit that references this issue on Sep 2, 2024

    gh-123562: Improve `SyntaxError` message for `case ... as a.b` (#123563)

    23f159a
  6. lysnikolaou
    closed this as completedon Sep 2, 2024
  7. lysnikolaou commented on Sep 2, 2024

    @lysnikolaou
    lysnikolaou
    on Sep 2, 2024
    Member
    More actions

    Thanks @sobolevn!

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

Metadata

Metadata

Assignees

  • @sobolevn
    sobolevn

Labels

interpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)topic-parsertype-featureA feature request or enhancementA feature request or enhancement

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    You are visitor 01402503 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.