Skip to content

Support super(), Base.__init__ and inherited __init__ in pyscript classes (#891) - #916

Open
HoneyBearTech wants to merge 1 commit into
custom-components:masterfrom
HoneyBearTech:fix-class-init-super-891
Open

HoneyBearTech wants to merge 1 commit into
custom-components:masterfrom
HoneyBearTech:fix-class-init-super-891

Conversation

@HoneyBearTech

Copy link
Copy Markdown
Contributor

Closes #891

What was broken
Several common class patterns failed in pyscript:

class Base:
    def __init__(self, x):
        self.x = x

class A(Base):
    def __init__(self, x, y):
        super().__init__(x)        # RuntimeError: super(): __class__ cell not found
        self.y = y

class B(Base):
    def __init__(self, x):
        Base.__init__(self, x)     # TypeError

class C(Base):
    pass

C(1)                               # TypeError: C() takes no arguments (#891)

class MyError(Exception):
    def __init__(self, msg):
        super().__init__(msg)      # fails, so custom exceptions lose their message

Why

  • ast_classdef renamed a pyscript __init__ and stored None in its place on every class without one. That hid the parent's __init__.
  • Nothing recorded the class a method was defined in, so a zero-argument super() had no __class__ to find.

What I changed

  • Each method remembers the class it was defined in. call_func turns a zero-argument super() into super(cls, first_arg), with Python's error messages for the cases where that isn't possible.
  • Objects are created the way type.__call__ does it:
    • __new__ runs first: a pyscript one, object.__new__(cls), or a native __new__ given the constructor's arguments (which str, namedtuple and Exception bases need);
    • then the first __init__ along the MRO, pyscript or native, if __new__ returned an instance of the class;
    • an __init__ that returns something other than None raises TypeError, as in Python;
    • a metaclass with its own __call__ keeps the old path.
  • Base.__init__, super().__init__, super(C, self).__init__ and obj.__init__ find the pyscript __init__ and bind it the way Python does. A @staticmethod __init__ and an __init__ set on an instance still behave as before.
  • A @pyscript_compile __init__ is left for Python to call. A class statement with a pyscript metaclass runs that metaclass's __init__. raise Cls runs a pyscript __init__.
  • The debug log line in call_func now formats its arguments only when debug logging is on. Before, it ran str() on every argument, which failed for a half-built object (for example logging.Handler before its __init__).

Testing

  • New test_class_init_super_match_python in tests/test_unit_eval.py. It runs 21 scripts (over 60 checked values) in CPython and in pyscript, and compares the results, at INFO and DEBUG log levels. They cover:
    • zero- and two-argument super(), Base.__init__, and inherited __init__ from pyscript and native bases;
    • custom exceptions (str(e), e.args, raise Cls);
    • logging.Handler, threading.Thread, dict, str and namedtuple subclasses;
    • diamond inheritance, mixins, metaclasses and ABCs;
    • a pyscript __new__ (alone, with __init__, and returning another type), static and instance-level __init__, and a compiled __init__;
    • super() in nested functions and closures, classes defined inside functions, and dataclasses;
    • the super() error messages.
  • The full test suite, ruff, pylint, mypy, codespell and sphinx-build -W pass on Python 3.14.

Docs
A new item in Language Limitations says what now works and what still doesn't:

  • instances created by native code (for example defaultdict(MyClass)) don't run a pyscript __init__;
  • zero-argument super() doesn't work in @pyscript_compile functions or lambdas;
  • a metaclass __call__ written in pyscript isn't supported;
  • a pyscript dataclass __post_init__ isn't called.

Note
Open PR #896 (methods as trigger functions) changes the same block of call_func. Whichever merges second needs a small rebase, and I'm happy to do it. My open #910 and #912 also change eval.py, in other functions; if any of them merges first, the others may need a small rebase.

@craigbarratt

Copy link
Copy Markdown
Member

Thanks @HoneyBearTech - this needs updating after #915 was merged.

…onents#891)

A class without __init__ didn't use its parent's, so C(1) failed (custom-components#891),
and Base.__init__(self, ...), super(C, self).__init__() and super()
without arguments all failed. Methods now remember their class, for
super(), and instances (and classes, for a pyscript metaclass) are created
like type.__call__: __new__ with the arguments a native base needs, then
the first pyscript __init__ along the mro. "raise Cls" now calls it too.

Signed-off-by: Sean Stoube <31805425+HoneyBearTech@users.noreply.github.com>
@HoneyBearTech

Copy link
Copy Markdown
Contributor Author

Thanks @craigbarratt, updated for #915 and rebased onto master. With classes now stored directly, three things changed. In ast_classdef, the class is recorded for super() right after it's created, before #915 pending annotations are evaluated and before decorators run. The module unwrap in ast_attribute is gone, along with #915 other unwrapping. And super() now unwraps its first argument itself where it used unwrap_defined, since a method that defines a function still keeps its locals in EvalLocalVars. Everything else is unchanged. The full test suite, pre-commit and the docs build pass on 3.14.

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.

A subclass doesn't inherit its base class's __init__

2 participants