Skip to content

Commit

Permalink
[3.12] pythongh-110938: Fix error messages for indented blocks with f…
Browse files Browse the repository at this point in the history
…unctions and classes with generic type parameters (pythonGH-110973)

(cherry picked from commit 24e4ec7)

Co-authored-by: Pablo Galindo Salgado <[email protected]>
  • Loading branch information
pablogsal authored and JelleZijlstra committed Oct 17, 2023
1 parent c1e5343 commit 5e570c2
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 22 deletions.
6 changes: 3 additions & 3 deletions Grammar/python.gram
Original file line number Diff line number Diff line change
Expand Up @@ -1369,11 +1369,11 @@ invalid_for_stmt:
| [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:
| [ASYNC] a='def' NAME '(' [params] ')' ['->' expression] ':' NEWLINE !INDENT {
| [ASYNC] a='def' NAME [type_params] '(' [params] ')' ['->' expression] ':' NEWLINE !INDENT {
RAISE_INDENTATION_ERROR("expected an indented block after function definition on line %d", a->lineno) }
invalid_class_def_raw:
| 'class' NAME ['(' [arguments] ')'] NEWLINE { RAISE_SYNTAX_ERROR("expected ':'") }
| a='class' NAME ['(' [arguments] ')'] ':' NEWLINE !INDENT {
| 'class' NAME [type_params] ['(' [arguments] ')'] NEWLINE { RAISE_SYNTAX_ERROR("expected ':'") }
| a='class' NAME [type_params] ['(' [arguments] ')'] ':' NEWLINE !INDENT {
RAISE_INDENTATION_ERROR("expected an indented block after class definition on line %d", a->lineno) }

invalid_double_starred_kvpairs:
Expand Down
10 changes: 10 additions & 0 deletions Lib/test/test_syntax.py
Original file line number Diff line number Diff line change
Expand Up @@ -1446,11 +1446,21 @@
Traceback (most recent call last):
IndentationError: expected an indented block after function definition on line 1
>>> def foo[T](x, /, y, *, z=2):
... pass
Traceback (most recent call last):
IndentationError: expected an indented block after function definition on line 1
>>> class Blech(A):
... pass
Traceback (most recent call last):
IndentationError: expected an indented block after class definition on line 1
>>> class Blech[T](A):
... pass
Traceback (most recent call last):
IndentationError: expected an indented block after class definition on line 1
>>> match something:
... pass
Traceback (most recent call last):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix error messages for indented blocks with functions and classes with
generic type parameters. Patch by Pablo Galindo
50 changes: 31 additions & 19 deletions Parser/parser.c

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5e570c2

Please sign in to comment.