Skip to content

Let a Python subclass without a constructor take its base's arguments [patch] - #217

Merged
matt-edmondson merged 1 commit into
mainfrom
fix/149-python-inherited-constructor
Oct 14, 2026
Merged

matt-edmondson merged 1 commit into
mainfrom
fix/149-python-inherited-constructor

Conversation

@matt-edmondson

Copy link
Copy Markdown
Contributor

Fixes #149

What was wrong

PythonGenerator wrote a constructor for every class that had a base type and no constructor of its own:

def __init__(self):
    super().__init__()

That constructor hid the base class's constructor, so with Base(x) and an empty Derived(Base), calling Derived(5) raised TypeError: 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:

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.tags = []
  • A class with no base keeps def __init__(self):.

Tests

  • Added PythonGeneratedSourceImportsTests.SubclassWithoutConstructor_AcceptsTheBaseConstructorsArguments, which runs the generated module under python3. It covers:
    • a base class with a one-argument constructor;
    • a subclass with no members, where Derived(5) sets x;
    • a subclass with an instance-initialised list field, constructed both positionally and by keyword. The base field is set, and each instance gets its own list.
  • With the generator change reverted, the test fails with the TypeError from the issue. With the change, it passes.
  • Full Coder.Test suite: 1154 passed, 0 failed.

🤖 Generated with Claude Code

https://claude.ai/code/session_01Ad87XvcvgZs8kt6MK9mwQE


Generated by Claude Code

… [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
@sonarqubecloud

Copy link
Copy Markdown

@matt-edmondson
matt-edmondson merged commit 8e9254d into main Oct 14, 2026
15 checks passed
@matt-edmondson
matt-edmondson deleted the fix/149-python-inherited-constructor branch October 14, 2026 01:17
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.

Python: a subclass with no constructor gets a synthesised __init__(self) that hides its base class's constructor, so Derived(5) raises TypeError

2 participants