Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

ImproveSyntaxError message forcase ... as a.b #123562

Closed
Assignees
sobolevn
Labels
@sobolevn

Description

@sobolevn

Feature or enhancement

There's already a similar rule:

| 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 likeas a.b, becausea matchesNAME
  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, ifpattern=or_pattern 'as' target=pattern_capture_target with a valid'as' NAME is matched, we won't fall to the nextinvalid_ rule.

Proposed result:

>>>match1:...casexasa.b: ......File"<python-input-0>",line2casexasa.b: ...^^^SyntaxError:cannotusepatterntargetasattribute

Refs#123440

Linked PRs

Metadata

Metadata

Assignees

Labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions


    [8]ページ先頭

    ©2009-2025 Movatter.jp