Repository navigation
Let a Python subclass without a constructor take its base's arguments [patch] - #217
Merged
Merged
Conversation
… [patch] PythonGenerator wrote def __init__(self): super().__init__() for every class with a base type and no constructor, which hid the base's constructor, so Derived(5) raised TypeError. A constructor is now written only when there are instance-initialised fields, so a bare subclass inherits the base's. One written for a subclass takes *args and **kwargs and passes them to super().__init__ before the field initialisers. Fixes #149 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Ad87XvcvgZs8kt6MK9mwQE
|
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.



Fixes #149
What was wrong
PythonGeneratorwrote a constructor for every class that had a base type and no constructor of its own:That constructor hid the base class's constructor, so with
Base(x)and an emptyDerived(Base), callingDerived(5)raisedTypeError: Derived.__init__() takes 1 positional argument but 2 were given.Change
A constructor is written only when the class has instance-initialised fields. A bare subclass writes none, so it inherits the base's constructor.
When a constructor is written for a subclass, it forwards its arguments and then runs the field initialisers:
A class with no base keeps
def __init__(self):.Tests
PythonGeneratedSourceImportsTests.SubclassWithoutConstructor_AcceptsTheBaseConstructorsArguments, which runs the generated module underpython3. It covers:Derived(5)setsx;TypeErrorfrom the issue. With the change, it passes.Coder.Testsuite: 1154 passed, 0 failed.🤖 Generated with Claude Code
https://claude.ai/code/session_01Ad87XvcvgZs8kt6MK9mwQE
Generated by Claude Code