Skip to content

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

Merged
craigbarratt merged 1 commit into
custom-components:masterfrom
HoneyBearTech:fix-property-classmethod
Nov 22, 2026
Merged

craigbarratt merged 1 commit into
custom-components:masterfrom
HoneyBearTech:fix-property-classmethod

Conversation

@HoneyBearTech

@HoneyBearTech HoneyBearTech commented Nov 21, 2026 •

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, deleter 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)

    @target.deleter
    def target(self):
        self._target = 20

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

Thermostat.default()       # TypeError

t = Thermostat()
t.target += 1              # TypeError
del t.target               # does nothing: the deleter's coroutine is never awaited

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 the property that Python would use for obj.attr, by looking along the class's MRO. It returns nothing for a property subclass, or when the class overrides hook (__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 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.
  • EvalAttrSet.delattr_async does the same for a pyscript deleter. ast_delete uses it for del obj.attr. A property with no deleter still raises Python's own AttributeError, and del on a state variable (like del pyscript.x) is unchanged.
  • 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, and super().make() and super(C, cls).make() inside a class method.
  • Anything else is left to Python as before:
    • a property subclass, which may override __get__, __set__ or __delete__;
    • a class that overrides __getattribute__, __setattr__ or __delattr__;
    • a compiled (@pyscript_compile) getter, setter or deleter, and native descriptors.

@staticmethod already 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:
    • the decorator and property(fget) 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.
  • test_property_deleter_match_python, 12 scripts. It also checks that Python gives no "coroutine was never awaited" warning. The scripts cover:
    • a deleter with side effects, at module level and inside a function;
    • property(fget, fset, fdel), an inherited deleter, and a deleter on a metaclass property;
    • a property with no deleter (same AttributeError message as Python);
    • a deleter that raises, where del a.x, b[0] then leaves b alone, and del a.x, b[0] deleting in order;
    • a class with its own __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() and super(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 through super(); and a class attribute read through super().

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 -W pass 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.setter and @x.deleter, or property(fget, fset, fdel)), @classmethod and @staticmethod work on the methods of a pyscript class, so obj.prop, obj.prop = value and del obj.prop call the getter, setter and deleter;
  • the built-in getattr(), setattr() and delattr() don't: getattr(obj, "prop") returns a coroutine instead of the value (so hasattr(obj, "prop") is True), and setattr() and delattr() return None without running the setter or deleter, so use obj.prop, obj.prop = value and del obj.prop in pyscript;
  • what still doesn't work: functools.cached_property, super().prop, a pyscript getter, setter or deleter on a property subclass, 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 change eval.py or add tests at the end of tests/test_unit_eval.py. Whichever merges later needs a small rebase, and I'm happy to do it.

@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.

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>
@HoneyBearTech

Copy link
Copy Markdown
Contributor Author

Thanks @craigbarratt, all four are done. It's still one commit.

  1. Rebase. Rebased onto master after Support super(), Base.__init__ and inherited __init__ in pyscript classes (#891) #916. As you said, I dropped unwrap_defined, the ModuleType import and the module branch in ast_attribute. The property lookup in ast_attribute now comes after Support super(), Base.__init__ and inherited __init__ in pyscript classes (#891) #916's __init__ check. In call_func, the classmethod case sits right after the EvalFunc check, before Support super(), Base.__init__ and inherited __init__ in pyscript classes (#891) #916's super() handling. In tests/test_unit_eval.py, this PR's tests now come after test_class_init_super_match_python.

  2. Deleter. I added EvalAttrSet.delattr_async next to setattr_async. It finds the property with pyscript_property(obj, attr, "__delattr__") and awaits a pyscript fdel. Anything else goes through delattr as before. ast_delete uses it for del obj.attr. del on a state variable is unchanged. The new test_property_deleter_match_python compares 12 scripts with CPython and checks that there's no "never awaited" warning. The scripts cover:

    • a deleter with side effects, at module level and inside a function;
    • property(fget, fset, fdel), an inherited deleter, and a metaclass property's deleter;
    • a property with no deleter, which raises the same AttributeError as Python;
    • a deleter that raises: del a.x, b[0] then leaves b alone. Without the error, the targets are deleted in order;
    • a class with its own __delattr__, a native __delete__ descriptor, a native property's deleter, and plain attributes.

    7 of the 12 fail without the change. The other 5 check behaviour that should stay the same.

  3. Docs. I rewrote the @property item in Language Limitations (docs/reference.rst). It now says that obj.prop, obj.prop = value and del obj.prop call the getter, setter and deleter. It also says that the built-in getattr(), setattr() and delattr() don't: getattr(obj, "prop") returns a coroutine instead of the value, hasattr(obj, "prop") is True, and setattr()/delattr() return None without running the setter or deleter. I took the deleter off the list of things that don't work, and added property subclasses and classes that override __delattr__ to it.

  4. super() in a classmethod. The new test_classmethod_super_match_python compares 6 scripts with CPython. They use super().make() and super(C, cls).make() inside a classmethod, through two and three classes, called on the class, an instance and a subclass. They also cover an alternate constructor that calls the base class's through super(), and a class attribute read through super(). No interpreter change was needed. All 6 fail if the classmethod case in call_func is removed.

I also updated the PR description.

@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, 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.

@craigbarratt
craigbarratt merged commit 5820c4f into custom-components:master Nov 22, 2026
6 checks passed
@HoneyBearTech

Copy link
Copy Markdown
Contributor Author

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

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