Skip to content

Support @property and @classmethod on pyscript class methods - #918

Open
HoneyBearTech wants to merge 1 commit into
custom-components:masterfrom
HoneyBearTech:fix-property-classmethod
Open

HoneyBearTech wants to merge 1 commit into
custom-components:masterfrom
HoneyBearTech:fix-property-classmethod

Conversation

@HoneyBearTech

Copy link
Copy Markdown
Contributor

What was broken
@property and @classmethod on pyscript methods failed with TypeError: 'EvalFunc' object is not callable. Python calls the getter, setter or class method itself, and it can't call pyscript's async functions:

class Thermostat:
    def __init__(self):
        self._target = 20

    @property
    def target(self):
        return self._target

    @target.setter
    def target(self, value):
        self._target = max(10, value)

    @classmethod
    def default(cls):
        return cls()

t = Thermostat.default()   # TypeError
t.target += 1              # TypeError

What I changed

  • ast_attribute: when the class's MRO resolves the name to a property whose getter is a pyscript function, the getter is awaited. A getter that raises AttributeError falls back to the class's __getattr__, bound as Python binds it.
  • EvalAttrSet.setattr_async awaits a pyscript setter. All assignment forms go through it, including +=, tuple targets and for obj.x in .... A read-only property still raises Python's own AttributeError.
  • call_func awaits a bound method whose __func__ is a pyscript function. That covers a class method called through the class, an instance or a subclass.
  • Anything else is left to Python as before:
    • a property subclass, which may override __get__ or __set__;
    • a class that overrides __getattribute__ or __setattr__;
    • a compiled getter or setter.

@staticmethod already worked.

Testing

  • New test_property_classmethod_match_python in tests/test_unit_eval.py: 31 sources, each compared with CPython. They cover:
    • the decorator and property(fget, fset) forms, setters, +=, read-only properties, inheritance and overrides;
    • attribute chains, metaclass properties, and native getters;
    • class methods through the class, an instance, a subclass and self, and as alternate constructors;
    • classes with their own __getattribute__/__setattr__, property subclasses, and __getattr__ fallbacks.
  • The full test suite, ruff, pylint, mypy, codespell and sphinx-build -W pass on Python 3.14.
  • The extra lookup only runs on attribute loads from non-module objects. It cost about 3% on a microbenchmark that does nothing but attribute loads.

Docs
Language Limitations now says @property, @classmethod and @staticmethod work on methods of a pyscript class, and lists what still doesn't:

  • getattr()/hasattr() on a pyscript property;
  • functools.cached_property;
  • super().prop;
  • a property deleter;
  • properties on classes that override __getattribute__ or __setattr__.

Note
This changes a few lines of call_func next to the PR for #891 (classes and super()). If that PR merges first, this one needs a small rebase, and I'm happy to do it.

My open #910 and #912 also change eval.py (#912 adds EvalAttrSet.delattr, next to the new setattr_async); a small rebase may be needed there too.

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, 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__ or
__setattr__ still gets them called, and an AttributeError from a getter
falls back to __getattr__. Docs list what still doesn't work.

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

Copy link
Copy Markdown
Member

Thanks @HoneyBearTech, this works well, and the CPython comparisons are thorough. Things to update:

  1. Please rebase onto master after Support super(), Base.__init__ and inherited __init__ in pyscript classes (#891) #916 merges. The conflicts are small: Store classes themselves in module and class namespaces #915 removed unwrap_defined, the ModuleType import and the module branch in ast_attribute.
  2. Now that Support del of attributes on normal objects #912 supports del obj.attr, del a.x with a pyscript @x.deleter silently does nothing: Python calls the deleter and gets back a coroutine that's never awaited. Could you handle fdel the same way as the setter, with an async delattr that awaits a pyscript deleter, plus a test?
  3. Since Keep a decorator's own body when it uses functools.wraps (#808) #910, getattr(a, "x") on a pyscript property returns a coroutine rather than raising, and hasattr returns True. Please update the docs to say so.
  4. After the rebase onto Support super(), Base.__init__ and inherited __init__ in pyscript classes (#891) #916, please add a test for super() inside a classmethod.

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