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

@HoneyBearTech HoneyBearTech commented Nov 21, 2026 •

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__.
  • Inside a generator expression's element, a zero-argument super() uses the generator's own first argument, its outermost iterator, as Python does. So (super().f() for _ in range(2)) in a method raises Python's TypeError, and super() in the outermost iterable still uses the method's self.
  • 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 23 scripts in CPython and in pyscript and compares every result, 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.
    • super() inside generator expressions: in an element, in a condition, in the outermost iterable, in nested generator expressions, and in a plain function (each result or error message matches CPython).
  • 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__, and getattr(obj, "__init__") doesn't find it (obj.__init__ written out does);
  • zero-argument super() doesn't work in @pyscript_compile functions or lambdas;
  • a metaclass __call__ written in pyscript isn't supported; a compiled one is called without the constructor's arguments, and the pyscript __init__ then runs with them on every call (so a singleton metaclass runs __init__ again each time);
  • 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. This PR is rebased onto master after #910, #912 and #917. My open #911 and #918 also change pyscript's code; #918 builds on this PR and will be rebased once this one merges.

@craigbarratt

Copy link
Copy Markdown
Member

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

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

…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 <[email protected]>
@HoneyBearTech

Copy link
Copy Markdown
Contributor Author

@craigbarratt I've rebased this onto master after #910, #912 and #917; only tests/test_unit_eval.py conflicted. With #917 in, I also fixed one interaction. Inside a generator expression's element, a zero-argument super() used the method's self. It now uses the generator's outermost iterator, as Python does, so (super().f() for _ in range(2)) in a method raises Python's TypeError, and super() in the outermost iterable still uses self. Two new scripts in test_class_init_super_match_python cover it, compared with CPython. The full suite, pre-commit and the docs build pass on 3.14. I've updated the 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, this is a big improvement. I compared another 17 scripts against CPython (cooperative **kwargs chains, exceptions that skip super().__init__, __new__ returning another type, Enum, dataclasses with an explicit __init__, and more) and found no regressions. Approving.

One small request: two lines aren't covered by the tests. Could you add two scripts to CLASS_INIT_SUPER_SOURCES?

  • a method that uses super(), called from a generator expression element, e.g. list(self.g() for _ in range(2)). This covers resetting genexp_iter in EvalFunc.call.
  • a __new__ that returns an instance of another pyscript class that has an __init__. This checks that __init__ runs only once.

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