Skip to content

fix: build a write-only Python property from its setter alone [patch] - #171

Merged
matt-edmondson merged 1 commit into
mainfrom
fix/113-python-write-only-property
Oct 6, 2026
Merged

matt-edmondson merged 1 commit into
mainfrom
fix/113-python-write-only-property

Conversation

@matt-edmondson

Copy link
Copy Markdown
Contributor

Summary

PythonGenerator.GeneratePropertyDeclaration wrote @name.setter for every writable property. That decorator is an attribute of the property object the @property getter creates, so when a property had no getter the class statement raised NameError: name 'level' is not defined.

A write-only property (CanWrite && !CanRead) is now written as:

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

Read/write and read-only properties produce the same output as before.

Tests

  • New PythonGeneratedSourceImportsTests.WriteOnlyProperty_ImportsAndSets generates a class with HasGetter = false, loads it with python3, sets the property, checks that the setter ran, and checks that reading the property raises AttributeError.
  • The new test fails on main with NameError: name 'level' is not defined and passes with this change.
  • Full Coder.Test suite: 1072 passed, 0 failed.

Fixes #113

🤖 Generated with Claude Code

https://claude.ai/code/session_017USYdQkEy3VLP48Dd4JadP


Generated by Claude Code

PythonGenerator wrote `@name.setter` for every writable property, but that
decorator is an attribute of the property object the `@property` getter
creates. A property with no getter therefore named something that did not
exist, and defining the class raised NameError.

A write-only property is now written as a `_set_name` function followed by
`name = property(fset=_set_name)`, which needs no getter. A new test loads
the generated module with python3, sets the property, and checks that
reading it raises AttributeError.

Fixes #113

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017USYdQkEy3VLP48Dd4JadP
@sonarqubecloud

sonarqubecloud Bot commented Oct 6, 2026

Copy link
Copy Markdown

@matt-edmondson
matt-edmondson merged commit c508a47 into main Oct 6, 2026
14 checks passed
@matt-edmondson
matt-edmondson deleted the fix/113-python-write-only-property branch October 6, 2026 06:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

2 participants