Repository navigation
Support @property and @classmethod on pyscript class methods - #918
craigbarratt merged 1 commit into
Conversation
|
Thanks @HoneyBearTech, this works well, and the CPython comparisons are thorough. Things to update:
|
A property or classmethod wrapping a pyscript method failed with "'EvalFunc' object is not callable", since the wrapped function is async and Python can't call it. Attribute reads now call a pyscript property getter, assignments (including +=) call the setter, del obj.attr calls the deleter, and a classmethod bound to a class is called as a pyscript function. Python's own rules for which attribute wins are kept: a class that overrides __getattribute__, __setattr__ or __delattr__ still gets them called, and an AttributeError from a getter falls back to __getattr__. Tests compare deleters and super() inside a classmethod with CPython. Docs list what still doesn't work, including that the built-in getattr(), setattr() and delattr() don't call a pyscript property. Signed-off-by: Sean Stoube <31805425+HoneyBearTech@users.noreply.github.com>
b24ab7d to
8b9cdfa
Compare
|
Thanks @craigbarratt, all four are done. It's still one commit.
I also updated the PR description. |
craigbarratt
left a comment
There was a problem hiding this comment.
Thanks @HoneyBearTech, all four look good. Approving. A possible follow-up now that #916 is in: super().prop could be supported by looking up the property in the MRO after __thisclass__ when the object is a super.
Thanks @craigbarratt, addressed in a follow-up PR #923 |
What was broken
@propertyand@classmethodon pyscript methods failed withTypeError: 'EvalFunc' object is not callable. Python calls the getter, setter, deleter or class method itself, and it can't call pyscript's async functions:Why
A property, and a classmethod bound to its class, hand the call to Python. Python then calls the pyscript function like a normal function. For a pyscript function that either fails (
'EvalFunc' object is not callable) or returns a coroutine that nobody awaits, so the method's code never runs.What I changed
pyscript_property(obj, attr, hook)finds thepropertythat Python would use forobj.attr, by looking along the class's MRO. It returns nothing for apropertysubclass, or when the class overrideshook(__getattribute__,__setattr__or__delattr__), so Python's own rules still decide in those cases.ast_attribute: when that property's getter is a pyscript function, pyscript awaits it. A getter that raisesAttributeErrorfalls back to the class's__getattr__, bound as Python binds it.EvalAttrSet.setattr_asyncawaits a pyscript setter. All assignment forms go through it, including+=, tuple targets andfor obj.x in .... A read-only property still raises Python's ownAttributeError.EvalAttrSet.delattr_asyncdoes the same for a pyscript deleter.ast_deleteuses it fordel obj.attr. A property with no deleter still raises Python's ownAttributeError, anddelon a state variable (likedel pyscript.x) is unchanged.call_funcawaits a bound method whose__func__is a pyscript function. That covers a class method called through the class, an instance or a subclass, andsuper().make()andsuper(C, cls).make()inside a class method.propertysubclass, which may override__get__,__set__or__delete__;__getattribute__,__setattr__or__delattr__;@pyscript_compile) getter, setter or deleter, and native descriptors.@staticmethodalready worked.Testing
Three tests in
tests/test_unit_eval.py. Each runs short scripts in CPython and in pyscript and compares the results:test_property_classmethod_match_python, 31 scripts:property(fget)forms, setters,+=, read-only properties, inheritance and overrides;self, and as alternate constructors;__getattribute__/__setattr__,propertysubclasses, and__getattr__fallbacks.test_property_deleter_match_python, 12 scripts. It also checks that Python gives no "coroutine was never awaited" warning. The scripts cover:property(fget, fset, fdel), an inherited deleter, and a deleter on a metaclass property;AttributeErrormessage as Python);del a.x, b[0]then leavesbalone, anddel a.x, b[0]deleting in order;__delattr__, a native descriptor with__delete__, a native property's deleter, and plain attributes (present and missing).test_classmethod_super_match_python, 6 scripts:super().make()andsuper(C, cls).make()inside a class method, through two and three classes, called on the class, an instance and a subclass; an alternate constructor that calls the base class's throughsuper(); and a class attribute read throughsuper().Without this change, 7 of the 12 deleter scripts fail (the other 5 check behaviour that must stay the same), and all 6
super()scripts fail.The full test suite, ruff, pylint, mypy, codespell and
sphinx-build -Wpass on Python 3.14. The extra lookup runs on attribute loads that aren't state variables. It cost about 2% on a microbenchmark that does little but attribute loads.Docs
The item in Language Limitations now says:
@property(with@x.setterand@x.deleter, orproperty(fget, fset, fdel)),@classmethodand@staticmethodwork on the methods of a pyscript class, soobj.prop,obj.prop = valueanddel obj.propcall the getter, setter and deleter;getattr(),setattr()anddelattr()don't:getattr(obj, "prop")returns a coroutine instead of the value (sohasattr(obj, "prop")isTrue), andsetattr()anddelattr()returnNonewithout running the setter or deleter, so useobj.prop,obj.prop = valueanddel obj.propin pyscript;functools.cached_property,super().prop, a pyscript getter, setter or deleter on apropertysubclass, and one in a class that overrides__getattribute__,__setattr__or__delattr__.Note
This is rebased onto master after #915 and #916, and is still one commit. My open #919 (lambdas), #920 (
StopIteration) and #921 (trigger expressions) also changeeval.pyor add tests at the end oftests/test_unit_eval.py. Whichever merges later needs a small rebase, and I'm happy to do it.