Repository navigation
Support super(), Base.__init__ and inherited __init__ in pyscript classes (#891) - #916
Open
HoneyBearTech wants to merge 1 commit into
Open
HoneyBearTech wants to merge 1 commit into
HoneyBearTech wants to merge 1 commit into
Conversation
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-homelab-automation
Bot
force-pushed
the
fix-class-init-super-891
branch
from
November 21, 2026 19:00
2835526 to
ae58351
Compare
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #891
What was broken
Several common class patterns failed in pyscript:
Why
ast_classdefrenamed a pyscript__init__and storedNonein its place on every class without one. That hid the parent's__init__.super()had no__class__to find.What I changed
call_functurns a zero-argumentsuper()intosuper(cls, first_arg), with Python's error messages for the cases where that isn't possible.type.__call__does it:__new__runs first: a pyscript one,object.__new__(cls), or a native__new__given the constructor's arguments (whichstr,namedtupleandExceptionbases need);__init__along the MRO, pyscript or native, if__new__returned an instance of the class;__init__that returns something other thanNoneraisesTypeError, as in Python;__call__keeps the old path.Base.__init__,super().__init__,super(C, self).__init__andobj.__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.@pyscript_compile__init__is left for Python to call. A class statement with a pyscript metaclass runs that metaclass's__init__.raise Clsruns a pyscript__init__.call_funcnow formats its arguments only when debug logging is on. Before, it ranstr()on every argument, which failed for a half-built object (for examplelogging.Handlerbefore its__init__).Testing
test_class_init_super_match_pythonintests/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:super(),Base.__init__, and inherited__init__from pyscript and native bases;str(e),e.args,raise Cls);logging.Handler,threading.Thread,dict,strandnamedtuplesubclasses;__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;super()error messages.sphinx-build -Wpass on Python 3.14.Docs
A new item in Language Limitations says what now works and what still doesn't:
defaultdict(MyClass)) don't run a pyscript__init__;super()doesn't work in@pyscript_compilefunctions or lambdas;__call__written in pyscript isn't supported;__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 changeeval.py, in other functions; if any of them merges first, the others may need a small rebase.