Skip to content

Python: a write-only property generates @level.setter with no getter, so defining the class raises NameError #113

Closed

Description

@matt-edmondson

What's wrong

PythonGenerator.GeneratePropertyDeclaration (Coder/Languages/PythonGenerator.cs:125-137) writes @{name}.setter whenever CanWrite is true. That decorator needs a name property object to already exist in the class body, and one only exists if the @property getter was written first, which happens only when CanRead is true. For a write-only property, the decorator refers to a name that was never defined.

Repro

ClassDeclaration cls = new("Sink");
PropertyDeclaration p = new("level", "int") { HasGetter = false, HasSetter = true };
p.SetterBody.Add(new ExpressionStatement(new CallExpression("record")));
cls.Members.Add(p);
new PythonGenerator().Generate(cls);

Output:

class Sink:
    @level.setter
    def level(self, value: int) -> None:
        record()

Running it with python3 fails when the class is defined: NameError: name 'level' is not defined. The other targets all emit a valid write-only property: C# set { ... }, JavaScript set level(value), and a set_level function for C, C++, Go and Rust.

Suggested fix / acceptance criteria

When !CanRead, emit a setter-only property without the decorator form:

def _set_level(self, value: int) -> None:
    record()
level = property(fset=_set_level)

Add a Python test with HasGetter = false. No existing test covers that case.

Activity

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

    @matt-edmondson
    ContributorAuthor

    Triage

    • Category: Bug
    • Priority: Medium. The generated Python fails loudly with NameError when the class is defined, so nothing wrong ships silently. It only affects write-only properties, which are uncommon.
    • Area / suggested owner: PythonGenerator.GeneratePropertyDeclaration
    • Duplicates / in progress: None found. No open PR references this issue.
    • Notes: Small, self-contained fix. The suggested property(fset=...) form is correct. Apply the same handling to static or class-level properties if the generator has a separate path for them.

    Generated by Claude Code

  3. added
    ready
    on Sep 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

bugready

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions