Skip to content

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

Closed

Description

@matt-edmondson

What's wrong

PythonGenerator (Coder/Languages/PythonGenerator.cs:471-472) sets synthesiseConstructor whenever the class has a BaseType, even if it has no fields to initialise. It then writes a constructor (:495-505) that takes no arguments:

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

Python constructors are inherited. Without this synthesised __init__, Derived would use Base.__init__ and accept its arguments. With it, Derived takes no arguments at all, and super().__init__() calls the base constructor without its required parameters.

Reproduction (python3, HEAD ad68d48)

Base has a constructor (x). Derived : Base has no members.

Derived(5)
# TypeError: Derived.__init__() takes 1 positional argument but 2 were given

Every subclass that relies on its base's constructor is broken. This came in with e2c5afa.

Suggested fix / acceptance criteria

  • Synthesise __init__ only when there are instance-initialised fields (instanceInitialisedFields.Count > 0). For a bare subclass, let Python inherit the base constructor.
  • When a constructor is synthesised for a subclass, forward the arguments:
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        # field initialisers
  • Add a Python execution test: base with a one-argument constructor, plus a derived class with no members and a derived class with an initialised field. Derived(5) constructs in both cases, and the base field is set.

Activity

  1. added
    bug
    on Oct 5, 2026
  2. matt-edmondson commented on Oct 5, 2026

    @matt-edmondson
    ContributorAuthor

    Triage


    Generated by Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions