Skip to content

Python: an instance field initialised with a list or constructed object becomes a class attribute shared by every instance, and an untyped field with no initialiser raises NameError #132

Closed

Description

@matt-edmondson

What's wrong

PythonGenerator.GenerateFieldDeclaration (Coder/Languages/PythonGenerator.cs:304-331) writes every field as a class-level statement, name[: T][ = value]. It ignores IsStatic, so an instance field and a static field come out identically. That causes two problems.

1. A mutable initial value is shared by every instance. Python evaluates the initial value once, when the class statement runs, and every instance then sees that same object.

var cls = new ClassDeclaration("Box");
cls.Members.Add(new FieldDeclaration("items", new TypeReference("int") { IsArray = true }) { InitialValue = new ConstructionExpression() });
cls.Members.Add(new FieldDeclaration("origin", "Point") { InitialValue = new ConstructionExpression(new TypeReference("Point")) });

This generates items: list[int] = [] and origin: Point = Point(). Running the output:

a = Box(); b = Box(); a.items.append(1)
print(b.items, a.origin is b.origin)   # prints: [1] True

The same AST gives each instance its own list and its own Point in C#, C++, JavaScript, Rust and Go. This is Python's well-known mutable-default trap, and the generator walks straight into it.

2. An untyped field with no initialiser is written as a bare name. new FieldDeclaration("bare") produces a class body line containing only bare. Python evaluates it as an expression when the class is created, so importing the module raises NameError: name 'bare' is not defined.

The remarks at :304-309 say the bare annotation count: int is deliberate, and that is not in question here. Case 2 is a field with no annotation at all.

Suggested fix / acceptance criteria

  • A non-static field whose initial value is not a literal (a ConstructionExpression, CallExpression and so on) should be initialised per instance:
    • keep the class-level annotation items: list[int]
    • emit self.items = [] at the top of the constructor, the way WriteInitialiserAssignments (:561) already writes self.x = …
    • synthesise def __init__(self): when the class has no constructor
  • A field with neither a type nor an initial value is written as bare: object or bare = None, never as a bare name.
  • Add tests:
    • a Python test asserting that two instances do not share a list-initialised field
    • a PythonGeneratedSourceImportsTests case with an untyped, uninitialised field that checks the module imports

Activity

  1. added
    bug
    on Sep 30, 2026
  2. matt-edmondson commented on Sep 30, 2026

    @matt-edmondson
    ContributorAuthor

    Triage


    Generated by Claude Code

  3. matt-edmondson commented on Sep 30, 2026

    @matt-edmondson
    ContributorAuthor

    Covered by #145, which redesigns this bug cluster as a whole. Implement it through that issue rather than individually.


    Generated by Claude Code

  4. matt-edmondson commented on Oct 13, 2026

    @matt-edmondson
    ContributorAuthor

    Triage

    Already fixed on main (1783fe6) by the #145 redesign. Closing.

    • In Python, a field initialised with a non-literal value is now assigned per instance in __init__, and an __init__ is created when the class has none (NeedsConstruction, PythonGenerator.cs:665-672; constructor at :545-595).
    • An untyped field with no value becomes = None (:446-449).
    • Test: PythonGeneratedSourceImportsTests.MutableFields_ArePerInstanceAndEnumsImport runs the module and asserts second.items == [] and first.bare is None. It passes.

    Generated by Claude Code

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions