Skip to content

Support del of attributes on normal objects - #912

Merged
craigbarratt merged 1 commit into
custom-components:masterfrom
HoneyBearTech:fix-del-object-attr
Nov 21, 2026
Merged

craigbarratt merged 1 commit into
custom-components:masterfrom
HoneyBearTech:fix-del-object-attr

Conversation

@HoneyBearTech

@HoneyBearTech HoneyBearTech commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

What was broken
del on an attribute of a normal object raised NameError, because every dotted del target was treated as a state variable:

class A:
    pass

a = A()
a.x = 1
del a.x        # NameError: name 'a.x' not defined

del self.x inside a method failed the same way.

What I changed
ast_delete now treats a dotted name the same way assignment does: it's a state variable only if its first part isn't defined. In that case State.delete is called as before. Otherwise the base object is evaluated and the attribute is deleted from it. Since #915, a pyscript module holds its classes directly, so del mod.Foo is a plain attribute delete too. This also makes targets like del a[0].x work.

Testing

  • New cases in tests/test_unit_eval.py, compared with CPython:
    • deleting an object attribute;
    • del self.x in a method;
    • a missing attribute raises AttributeError;
    • class and module attributes;
    • del mod.Foo for a class in a pyscript module: the module's own functions see it as gone, and hasattr(mod, "Foo") is False, as in Python.
  • The existing state deletion test (del pyscript.var1.attr3) still passes.
  • The full test suite, ruff, pylint, mypy and codespell pass on Python 3.14.

@craigbarratt craigbarratt left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @HoneyBearTech, treating a dotted del target like assignment is the right fix.

Now that #915 is merged, pyscript modules hold classes directly, without EvalLocalVar, and the ModuleType import is gone. GitHub shows no conflict, but merged onto master this fails with NameError: name 'ModuleType' is not defined on every del obj.attr.

Could you rebase and reduce EvalAttrSet.delattr to delattr(self.obj, self.attr)? With that, the full suite passes, including your del mod_global.Foo test, and the known limit in the description no longer applies.

ast_delete passed every dotted target to State.delete, so del a.x on
a normal object, or del self.x in a method, raised NameError. As with
assignment, a dotted name is now a state only if its first part isn't
defined; otherwise the attribute is deleted from the object, as in
Python, including a class from a pyscript module.

Signed-off-by: Sean Stoube <[email protected]>
@HoneyBearTech

Copy link
Copy Markdown
Contributor Author

Thanks @craigbarratt! Rebased onto master, and EvalAttrSet.delattr is now just delattr(self.obj, self.attr). The del mod_global.Foo test also checks that hasattr(mod_global, "Foo") is False. I've removed the known limit from the description, and the commit message no longer says the class is marked undefined in place. The full suite, pre-commit and the docs build pass on 3.14.

@craigbarratt

Copy link
Copy Markdown
Member

Thanks @HoneyBearTech, looks great. Merging.

@craigbarratt
craigbarratt merged commit 13af32c into custom-components:master Nov 21, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants