Repository navigation
Support del of attributes on normal objects - #912
Conversation
craigbarratt
left a comment
There was a problem hiding this comment.
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.
Signed-off-by: Sean Stoube <[email protected]>
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]>
b0ac63d to
7fe51ff
Compare
|
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. |
|
Thanks @HoneyBearTech, looks great. Merging. |
What was broken
delon an attribute of a normal object raisedNameError, because every dotteddeltarget was treated as a state variable:del self.xinside a method failed the same way.What I changed
ast_deletenow treats a dotted name the same way assignment does: it's a state variable only if its first part isn't defined. In that caseState.deleteis 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, sodel mod.Foois a plain attribute delete too. This also makes targets likedel a[0].xwork.Testing
tests/test_unit_eval.py, compared with CPython:del self.xin a method;AttributeError;del mod.Foofor a class in a pyscript module: the module's own functions see it as gone, andhasattr(mod, "Foo")isFalse, as in Python.del pyscript.var1.attr3) still passes.