Support the `del` statement
Implements del, which the parser previously rejected with NotImplementedError.
Earlier attempts were #68 (subscripts only) and #309 (closed after a performance regression stalled).
What works
- Names: locals, globals (declared or module level), cells and
nonlocal, class bodies, REPL feeds andexec()snippets.
Errors match CPython:NameErrorat module and class level,UnboundLocalErrorin functions. - Subscripts: dict, list (including extended slices with any step) and deque.
- Attributes: sandbox instances and classes, host class instances.
Frozen dataclasses raiseFrozenInstanceError: cannot delete field 'x'. - Target groups:
del a, (b[0], c.d)deletes left to right.
Implementation notes
- New opcodes
DeleteSubscr,DeleteAttrandDeleteClassLocal(125ā127).
Load opcodes and their dispatch are untouched, so code withoutdelruns the same instructions as before. DUMP_VERSION13 ā 14, the first bump since v1.1.0, anddump_v14.binreplacesdump_v13.bin.- Class-body
del namedrops the name from the assembled namespace.
Class bodies have no control flow, so this is decided once after parsing the body. - List slice deletion uses
Vec::extract_if, so no second value buffer is allocated.
Tests
test_cases/del__statement.pycovers all of the above with exact exception messages and runs against CPython and Monty.- Removed the stale
delentries fromlimitations/(language, index, classes, collections, dataclasses, eval_exec).
I have not run the benchmarks (make bench); since no existing opcode changed, I don't expect a regression, but that is not measured.
š¤ Generated with Claude Code
Summary by cubic
Implements the del statement, which previously failed at parse time with NotImplementedError. Deletion now works for names (locals, globals, cells, nonlocal, class bodies, REPL feeds, and exec() snippets), subscripts (dict, list including extended slices, deque), and attributes (instances, classes, host classes), with error messages matching CPython ā including FrozenInstanceError for frozen dataclass fields, RuntimeError for deque mutation during iteration, TypeError for immutable-type attributes, and the sequence-path wording for unsupported item deletion.
Implementation notes
- Adds three new opcodes (
DeleteSubscr,DeleteAttr,DeleteClassLocal) without changing existing load opcodes, and bumpsDUMP_VERSION13 ā 14 (the first since v1.1.0) with the dump fixture regenerated. - On sequence-slot types, unsupported item deletion converts an index-protocol key via its
__index__first (running user code, propagating its exception, or raisingIndexErroron overflow) before reporting the CPython wording. - A class body binds
__doc__up front only when a docstring is present, sodel __doc__without one raisesNameError;type()'sNonedefault is applied after the body. - Adds
test_cases/del__statement.pycovering all targets and error paths, removes staledelentries fromlimitations/docs, and documents thatdel C.__annotations__diverges from CPython 3.14. - Benchmarks were not run; no existing opcode changed, so no regression is expected.