mirror of
https://github.com/python/cpython.git
synced 2025-10-26 11:14:33 +00:00
gh-136616: Improve assert syntax error messages (#136653)
This commit is contained in:
parent
66ef16105a
commit
6bc65c30ff
4 changed files with 594 additions and 400 deletions
|
|
@ -212,7 +212,9 @@ del_stmt[stmt_ty]:
|
||||||
|
|
||||||
yield_stmt[stmt_ty]: y=yield_expr { _PyAST_Expr(y, EXTRA) }
|
yield_stmt[stmt_ty]: y=yield_expr { _PyAST_Expr(y, EXTRA) }
|
||||||
|
|
||||||
assert_stmt[stmt_ty]: 'assert' a=expression b=[',' z=expression { z }] { _PyAST_Assert(a, b, EXTRA) }
|
assert_stmt[stmt_ty]:
|
||||||
|
| invalid_assert_stmt
|
||||||
|
| 'assert' a=expression b=[',' z=expression { z }] { _PyAST_Assert(a, b, EXTRA) }
|
||||||
|
|
||||||
import_stmt[stmt_ty]:
|
import_stmt[stmt_ty]:
|
||||||
| invalid_import
|
| invalid_import
|
||||||
|
|
@ -1302,6 +1304,17 @@ invalid_raise_stmt:
|
||||||
invalid_del_stmt:
|
invalid_del_stmt:
|
||||||
| 'del' a=star_expressions {
|
| 'del' a=star_expressions {
|
||||||
RAISE_SYNTAX_ERROR_INVALID_TARGET(DEL_TARGETS, a) }
|
RAISE_SYNTAX_ERROR_INVALID_TARGET(DEL_TARGETS, a) }
|
||||||
|
invalid_assert_stmt:
|
||||||
|
| 'assert' a=expression '=' b=expression {
|
||||||
|
RAISE_SYNTAX_ERROR_KNOWN_RANGE(
|
||||||
|
a, b,
|
||||||
|
"cannot assign to %s here. Maybe you meant '==' instead of '='?",
|
||||||
|
_PyPegen_get_expr_name(a)) }
|
||||||
|
| 'assert' expression ',' a=expression '=' b=expression {
|
||||||
|
RAISE_SYNTAX_ERROR_KNOWN_RANGE(
|
||||||
|
a, b,
|
||||||
|
"cannot assign to %s here. Maybe you meant '==' instead of '='?",
|
||||||
|
_PyPegen_get_expr_name(a)) }
|
||||||
invalid_block:
|
invalid_block:
|
||||||
| NEWLINE !INDENT { RAISE_INDENTATION_ERROR("expected an indented block") }
|
| NEWLINE !INDENT { RAISE_INDENTATION_ERROR("expected an indented block") }
|
||||||
invalid_comprehension:
|
invalid_comprehension:
|
||||||
|
|
|
||||||
|
|
@ -2686,6 +2686,71 @@ def f(x: *b)
|
||||||
>>> f(x = 5, *:)
|
>>> f(x = 5, *:)
|
||||||
Traceback (most recent call last):
|
Traceback (most recent call last):
|
||||||
SyntaxError: Invalid star expression
|
SyntaxError: Invalid star expression
|
||||||
|
|
||||||
|
Asserts:
|
||||||
|
|
||||||
|
>>> assert (a := 1) # ok
|
||||||
|
>>> # TODO(@sobolevn): improve this message in the next PR
|
||||||
|
>>> assert a := 1
|
||||||
|
Traceback (most recent call last):
|
||||||
|
SyntaxError: invalid syntax
|
||||||
|
|
||||||
|
>>> assert 1 = 2 = 3
|
||||||
|
Traceback (most recent call last):
|
||||||
|
SyntaxError: cannot assign to literal here. Maybe you meant '==' instead of '='?
|
||||||
|
|
||||||
|
>>> assert 1 = 2
|
||||||
|
Traceback (most recent call last):
|
||||||
|
SyntaxError: cannot assign to literal here. Maybe you meant '==' instead of '='?
|
||||||
|
|
||||||
|
>>> assert (1 = 2)
|
||||||
|
Traceback (most recent call last):
|
||||||
|
SyntaxError: cannot assign to literal here. Maybe you meant '==' instead of '='?
|
||||||
|
|
||||||
|
>>> assert 'a' = a
|
||||||
|
Traceback (most recent call last):
|
||||||
|
SyntaxError: cannot assign to literal here. Maybe you meant '==' instead of '='?
|
||||||
|
|
||||||
|
>>> assert x[0] = 1
|
||||||
|
Traceback (most recent call last):
|
||||||
|
SyntaxError: cannot assign to subscript here. Maybe you meant '==' instead of '='?
|
||||||
|
|
||||||
|
>>> assert (yield a) = 2
|
||||||
|
Traceback (most recent call last):
|
||||||
|
SyntaxError: cannot assign to yield expression here. Maybe you meant '==' instead of '='?
|
||||||
|
|
||||||
|
>>> assert a = 2
|
||||||
|
Traceback (most recent call last):
|
||||||
|
SyntaxError: cannot assign to name here. Maybe you meant '==' instead of '='?
|
||||||
|
|
||||||
|
>>> assert (a = 2)
|
||||||
|
Traceback (most recent call last):
|
||||||
|
SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='?
|
||||||
|
|
||||||
|
>>> assert a = b
|
||||||
|
Traceback (most recent call last):
|
||||||
|
SyntaxError: cannot assign to name here. Maybe you meant '==' instead of '='?
|
||||||
|
|
||||||
|
>>> assert 1, 1 = b
|
||||||
|
Traceback (most recent call last):
|
||||||
|
SyntaxError: cannot assign to literal here. Maybe you meant '==' instead of '='?
|
||||||
|
|
||||||
|
>>> assert 1, (1 = b)
|
||||||
|
Traceback (most recent call last):
|
||||||
|
SyntaxError: cannot assign to literal here. Maybe you meant '==' instead of '='?
|
||||||
|
|
||||||
|
>>> assert 1, a = 1
|
||||||
|
Traceback (most recent call last):
|
||||||
|
SyntaxError: cannot assign to name here. Maybe you meant '==' instead of '='?
|
||||||
|
|
||||||
|
>>> assert 1, (a = 1)
|
||||||
|
Traceback (most recent call last):
|
||||||
|
SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='?
|
||||||
|
|
||||||
|
>>> assert 1 = a, a = 1
|
||||||
|
Traceback (most recent call last):
|
||||||
|
SyntaxError: cannot assign to literal here. Maybe you meant '==' instead of '='?
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import re
|
import re
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,2 @@
|
||||||
|
Improve :exc:`SyntaxError` error messages for invalid :keyword:`assert`
|
||||||
|
usages.
|
||||||
912
Parser/parser.c
generated
912
Parser/parser.c
generated
File diff suppressed because it is too large
Load diff
Loading…
Add table
Add a link
Reference in a new issue