You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
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
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.
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
Priority: High. Every Python instance silently shares a list-initialised or object-initialised field, which gives wrong results with no error in a common pattern. The untyped, uninitialised case separately fails on import with NameError.
Area / suggested assignment:Coder/Languages/PythonGenerator.cs: GenerateFieldDeclaration (L304-331), constructor emission, and WriteInitialiserAssignments (L561).
Notes: Synthesising __init__ has to work alongside existing constructors and base classes, so check the output against PythonGeneratedSourceImportsTests. If a class has a base type, the synthesised __init__ probably needs to call super().__init__().
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.
Activity
matt-edmondson commentedon Sep 30, 2026
Triage
NameError.Coder/Languages/PythonGenerator.cs:GenerateFieldDeclaration(L304-331), constructor emission, andWriteInitialiserAssignments(L561).FieldDeclaration.IsStatic, so the two may share a design decision. Python: a write-only property generates@level.setterwith no getter, so defining the class raises NameError #113 and Python output raises NameError on import when a method or field annotation names its own class (or a class declared later in the file) #102 are other Python class-body correctness bugs. There is no duplicate.__init__has to work alongside existing constructors and base classes, so check the output againstPythonGeneratedSourceImportsTests. If a class has a base type, the synthesised__init__probably needs to callsuper().__init__().Generated by Claude Code
matt-edmondson commentedon Sep 30, 2026
Covered by #145, which redesigns this bug cluster as a whole. Implement it through that issue rather than individually.
Generated by Claude Code
matt-edmondson commentedon Oct 13, 2026
Triage
Already fixed on
main(1783fe6) by the #145 redesign. Closing.__init__, and an__init__is created when the class has none (NeedsConstruction,PythonGenerator.cs:665-672; constructor at:545-595).= None(:446-449).PythonGeneratedSourceImportsTests.MutableFields_ArePerInstanceAndEnumsImportruns the module and assertssecond.items == []andfirst.bare is None. It passes.Generated by Claude Code