Repository navigation
Support super(), Base.__init__ and inherited __init__ in pyscript classes (#891) - #916
HoneyBearTech wants to merge 1 commit into
Conversation
|
Thanks @HoneyBearTech - this needs updating after #915 was merged. |
2835526 to
ae58351
Compare
|
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]>
ae58351 to
32c18f1
Compare
|
@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
left a comment
There was a problem hiding this comment.
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 resettinggenexp_iterinEvalFunc.call. - a
__new__that returns an instance of another pyscript class that has an__init__. This checks that__init__runs only once.
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__.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'sTypeError, andsuper()in the outermost iterable still uses the method'sself.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 23 scripts in CPython and in pyscript and compares every result, 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.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).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__, andgetattr(obj, "__init__")doesn't find it (obj.__init__written out does);super()doesn't work in@pyscript_compilefunctions or lambdas;__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);__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.