← 返回首页
bpo-40334: produce specialised errors for del by hauntsaninja · Pull Request #19911 · python/cpython · GitHub
Skip to content

Navigation Menu

Toggle navigation
Sign in
Appearance settings
Search or jump to...

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

Provide feedback

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

Include my email address so I can be contacted

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Resetting focus
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .c  (1) .gram  (1) .py  (2) All 3 file types selected Viewed files
Conversations
Failed to load comments. Retry
Loading
Jump to
Jump to file
Failed to load files. Retry
Loading
Diff view
Unified
Split
Hide whitespace
Apply and reload
Show whitespace
Diff view
Unified
Split
Hide whitespace
Apply and reload
13 changes: 10 additions & 3 deletions Grammar/python.gram
Show comments View file Edit file Delete file Open in desktop
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -583,15 +583,19 @@ ann_assign_subscript_attribute_target[expr_ty]:
| a=t_primary '[' b=slices ']' !t_lookahead { _Py_Subscript(a, b, Store, EXTRA) }

del_targets[asdl_seq*]: a=','.del_target+ [','] { a }
# The lookaheads to del_target_end ensure that we don't match expressions where a prefix of the
# expression matches our rule, thereby letting these cases fall through to invalid_del_target.
del_target[expr_ty] (memo):
| a=t_primary '.' b=NAME !t_lookahead { _Py_Attribute(a, b->v.Name.id, Del, EXTRA) }
| a=t_primary '[' b=slices ']' !t_lookahead { _Py_Subscript(a, b, Del, EXTRA) }
| a=t_primary '.' b=NAME &del_target_end { _Py_Attribute(a, b->v.Name.id, Del, EXTRA) }
| a=t_primary '[' b=slices ']' &del_target_end { _Py_Subscript(a, b, Del, EXTRA) }
| del_t_atom
del_t_atom[expr_ty]:
| a=NAME { _PyPegen_set_expr_context(p, a, Del) }
| a=NAME &del_target_end { _PyPegen_set_expr_context(p, a, Del) }
| '(' a=del_target ')' { _PyPegen_set_expr_context(p, a, Del) }
| '(' a=[del_targets] ')' { _Py_Tuple(a, Del, EXTRA) }
| '[' a=[del_targets] ']' { _Py_List(a, Del, EXTRA) }
| invalid_del_target
del_target_end: ')' | ']' | ',' | ';' | NEWLINE
Comment thread
hauntsaninja marked this conversation as resolved.
Outdated
Show resolved Hide resolved

targets[asdl_seq*]: a=','.target+ [','] { a }
target[expr_ty] (memo):
Expand Down Expand Up @@ -649,3 +653,6 @@ invalid_lambda_star_etc:
invalid_double_type_comments:
| TYPE_COMMENT NEWLINE TYPE_COMMENT NEWLINE INDENT {
RAISE_SYNTAX_ERROR("Cannot have two type comments on def") }
invalid_del_target:
| a=star_expression &del_target_end {
RAISE_SYNTAX_ERROR("cannot delete %s", _PyPegen_get_expr_name(a)) }
17 changes: 17 additions & 0 deletions Lib/test/test_grammar.py
Show comments View file Edit file Delete file Open in desktop
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -801,6 +801,23 @@ def test_del_stmt(self):
del abc
del x, y, (z, xyz)

x, y, z = "xyz"
del x
del y,
del (z)
del ()

a, b, c, d, e, f, g = "abcdefg"
del a, (b, c), (d, (e, f))

a, b, c, d, e, f, g = "abcdefg"
del a, [b, c], (d, [e, f])

abcd = list("abcd")
del abcd[1:2]

compile("del a, (b[0].c, (d.e, f.g[1:2])), [h.i.j], ()", "<testcase>", "exec")

def test_pass_stmt(self):
# 'pass'
pass
Expand Down
41 changes: 33 additions & 8 deletions Lib/test/test_syntax.py
Show comments View file Edit file Delete file Open in desktop
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,9 @@
Traceback (most recent call last):
SyntaxError: cannot assign to function call

# Pegen does not support this yet
# >>> del f()
# Traceback (most recent call last):
# SyntaxError: cannot delete function call
>>> del f()
Traceback (most recent call last):
SyntaxError: cannot delete function call

>>> a + 1 = 2
Traceback (most recent call last):
Expand Down Expand Up @@ -665,7 +664,7 @@ def _check_error(self, code, errtext,
self.fail("SyntaxError is not a %s" % subclass.__name__)
mo = re.search(errtext, str(err))
if mo is None:
self.fail("SyntaxError did not contain '%r'" % (errtext,))
self.fail("SyntaxError did not contain %r" % (errtext,))
self.assertEqual(err.filename, filename)
if lineno is not None:
self.assertEqual(err.lineno, lineno)
Expand All @@ -677,10 +676,36 @@ def _check_error(self, code, errtext,
def test_assign_call(self):
self._check_error("f() = 1", "assign")

@support.skip_if_new_parser("Pegen does not produce a specialized error "
"message yet")
def test_assign_del(self):
self._check_error("del f()", "delete")
self._check_error("del (,)", "invalid syntax")
self._check_error("del 1", "delete literal")
self._check_error("del (1, 2)", "delete literal")
self._check_error("del None", "delete None")
self._check_error("del *x", "delete starred")
self._check_error("del (*x)", "delete starred")
self._check_error("del (*x,)", "delete starred")
self._check_error("del [*x,]", "delete starred")
self._check_error("del f()", "delete function call")
self._check_error("del f(a, b)", "delete function call")
self._check_error("del o.f()", "delete function call")
self._check_error("del a[0]()", "delete function call")
self._check_error("del x, f()", "delete function call")
self._check_error("del f(), x", "delete function call")
self._check_error("del [a, b, ((c), (d,), e.f())]", "delete function call")
self._check_error("del (a if True else b)", "delete conditional")
self._check_error("del +a", "delete operator")
self._check_error("del a, +b", "delete operator")
self._check_error("del a + b", "delete operator")
self._check_error("del (a + b, c)", "delete operator")
self._check_error("del (c[0], a + b)", "delete operator")
self._check_error("del a.b.c + 2", "delete operator")
self._check_error("del a.b.c[0] + 2", "delete operator")
self._check_error("del (a, b, (c, d.e.f + 2))", "delete operator")
self._check_error("del [a, b, (c, d.e.f[0] + 2)]", "delete operator")
self._check_error("del (a := 5)", "delete named expression")
# We don't have a special message for this, but make sure we don't
# report "cannot delete name"
self._check_error("del a += b", "invalid syntax")

def test_global_param_err_first(self):
source = """if 1:
Expand Down
Loading
Toggle all file notes Toggle all file annotations

Footer

© 2026 GitHub, Inc.