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

Minor improvement to errors with (no) missing colon #92858

Closed
Labels
interpreter-core(Objects, Python, Grammar, and Parser dirs)type-bugAn unexpected behavior, bug, or error
@wookie184

Description

@wookie184

Some invalidfor statements currently give the specific errorexpected ':' even if there is a colon on the line. I think it would be a slight improvement to raise a more general error in these cases. This is howwhile andif already behave.

Current behaviour:

>>>forxinrange(10)   ...passTraceback (mostrecentcalllast):SyntaxError:expected':'>>>forxinrange10:   ...passTraceback (mostrecentcalllast):SyntaxError:expected':'>>>forxinrange10   ...passTraceback (mostrecentcalllast):SyntaxError:expected':'

Proposed behaviour:

>>>forxinrange(10)   ...passTraceback (mostrecentcalllast):SyntaxError:expected':'>>>forxinrange10:   ...passTraceback (mostrecentcalllast):SyntaxError:invalidsyntax>>>forxinrange10   ...passTraceback (mostrecentcalllast):SyntaxError:invalidsyntax

Proposed Implementation:
Would do it the same waywhile andif do it. For the for loop example it would be something like this:

--- a/Grammar/python.gram+++ b/Grammar/python.gram@@ -379,7 +379,7 @@ while_stmt[stmt_ty]: for_stmt[stmt_ty]:     | invalid_for_stmt-    | 'for' t=star_targets 'in' ~ ex=star_expressions &&':' tc=[TYPE_COMMENT] b=block el=[else_block] {+    | 'for' t=star_targets 'in' ~ ex=star_expressions ':' tc=[TYPE_COMMENT] b=block el=[else_block] {         _PyAST_For(t, ex, b, el, NEW_TYPE_COMMENT(p, tc), EXTRA) }     | ASYNC 'for' t=star_targets 'in' ~ ex=star_expressions &&':' tc=[TYPE_COMMENT] b=block el=[else_block] {         CHECK_VERSION(stmt_ty, 5, "Async for loops are", _PyAST_AsyncFor(t, ex, b, el, NEW_TYPE_COMMENT(p, tc), EXTRA)) }@@ -1295,6 +1295,7 @@ invalid_while_stmt:     | a='while' named_expression ':' NEWLINE !INDENT {         RAISE_INDENTATION_ERROR("expected an indented block after 'while' statement on line %d", a->lineno) } invalid_for_stmt:+    | 'for' star_targets 'in' star_expressions NEWLINE { RAISE_SYNTAX_ERROR("expected ':'") }     | [ASYNC] a='for' star_targets 'in' star_expressions ':' NEWLINE !INDENT {         RAISE_INDENTATION_ERROR("expected an indented block after 'for' statement on line %d", a->lineno) } invalid_def_raw:

I'd also apply this to other suites where it makes sense (e.g.with,match/case, andtry)

Would it be OK for me to submit a PR making these changes? (this is my first time looking into the CPython parser so apologies if I made any obvious mistakes!)

Metadata

Metadata

Assignees

No one assigned

    Labels

    interpreter-core(Objects, Python, Grammar, and Parser dirs)type-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp