Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions custom_components/pyscript/eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,44 @@ def pyscript_property(obj, attr, hook):
return None


# a super object forwards attribute reads to the classes it searches, and a metaclass can
# intercept a class's, so these read the built-in members directly, as Python does
SUPER_SELF = super.__dict__["__self__"].__get__
SUPER_SELF_CLASS = super.__dict__["__self_class__"].__get__
SUPER_THISCLASS = super.__dict__["__thisclass__"].__get__
TYPE_MRO = type.__dict__["__mro__"].__get__
TYPE_DICT = type.__dict__["__dict__"].__get__


def super_property(sup, attr):
"""
Return the property whose getter Python would call for sup.attr, otherwise None.

Like Python, this searches the mro of sup.__self_class__ after sup.__thisclass__, and
stops at the first class that has attr. Python's super doesn't use the class's own
__getattribute__ or __getattr__, never searches for __class__, and returns the property
itself rather than calling its getter when sup.__self__ is sup.__self_class__, as for
super(C, cls) on a class.
"""
self_class = SUPER_SELF_CLASS(sup)
if self_class is None or SUPER_SELF(sup) is self_class or attr == "__class__":
return None
mro = TYPE_MRO(self_class)
thisclass = SUPER_THISCLASS(sup)
for start, klass in enumerate(mro):
if klass is thisclass:
break
else:
# __thisclass__ is no longer in the mro (e.g. __bases__ changed); Python raises its own error
return None
for klass in mro[start + 1 :]:
klass_dict = TYPE_DICT(klass)
if attr in klass_dict:
value = klass_dict[attr]
return value if type(value) is property else None # pylint: disable=unidiomatic-typecheck
return None


class EvalName:
"""Identifier that hasn't yet been resolved."""

Expand Down Expand Up @@ -1696,6 +1734,12 @@ async def ast_attribute(self, arg):
return EvalAttrSet(val, arg.attr)
if arg.attr == "__init__" and (init_func := self.init_attr(val)) is not None:
return init_func
if type(val) is super: # pylint: disable=unidiomatic-typecheck
prop = super_property(val, arg.attr)
# check the getter's real type, since isinstance() would read a native getter's __class__
if prop is not None and issubclass(type(prop.fget), (EvalFunc, EvalFuncVar)):
# as in Python, an AttributeError from the getter isn't passed to __getattr__
return await prop.fget.call(self, SUPER_SELF(val))
prop = pyscript_property(val, arg.attr, "__getattribute__")
if prop is not None and is_pyscript_func(prop.fget):
try:
Expand Down
12 changes: 8 additions & 4 deletions docs/reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2449,10 +2449,14 @@ A handful of language features are not supported:
``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
(Python then warns that a coroutine was never awaited). So in pyscript, use ``obj.prop``,
``obj.prop = value`` and ``del obj.prop``. Several things still don't work: ``functools.cached_property``,
``super().prop``, a pyscript getter, setter or deleter on a subclass of ``property``, and a pyscript
getter, setter or deleter in a class that overrides ``__getattribute__``, ``__setattr__`` or
``__delattr__`` respectively.
``obj.prop = value`` and ``del obj.prop``. ``super().prop`` and ``super(Cls, self).prop`` call a parent
class's getter, so a subclass's property can extend its parent's. As in Python, ``super().prop = value``
and ``del super().prop`` raise ``AttributeError``; ``super(Cls, type(self)).prop.fset(self, value)``
calls a parent class's setter. Several things still don't work: ``functools.cached_property``, a
pyscript getter, setter or deleter on a subclass of ``property`` (also through ``super()``), and a
pyscript getter, setter or deleter in a class that overrides ``__getattribute__``, ``__setattr__`` or
``__delattr__`` respectively. Reading a property through ``super()`` isn't affected by a class's own
``__getattribute__``, since ``super()`` doesn't use it, as in Python.

The typical work-around for places where pyscript falls short is to move that code into a native Python module,
and then import that module into pyscript. Pyscript can call Python modules and packages, so you could
Expand Down
Loading
Loading