Skip to content

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

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

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

    bugSomething isn't workingreadyFully specified; implement as written

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions