Skip to content

Fix docstring serialization for special characters#228

Merged
fumitoh merged 2 commits into
mainfrom
claude/fix-issue-227-G02UI
May 11, 2026
Merged

Fix docstring serialization for special characters#228
fumitoh merged 2 commits into
mainfrom
claude/fix-issue-227-G02UI

Conversation

@fumitoh
Copy link
Copy Markdown
Owner

@fumitoh fumitoh commented May 11, 2026

Summary

Fixed an issue where docstrings containing special characters (backslashes, triple quotes, or trailing double quotes) were not properly escaped during model serialization, causing invalid Python syntax in the generated code.

Changes

  • Added _format_docstring() helper function in serializer_6.py that intelligently formats docstrings:
    • Uses triple-quoted form ("""...""") for simple docstrings without special characters
    • Falls back to repr() for docstrings containing backslashes, """, or trailing " to ensure valid Python syntax
  • Updated docstring encoding in three locations to use the new helper:
    • Model docstring encoding
    • Space docstring encoding
    • Cells docstring encoding
  • Added comprehensive test case test_doc_special_characters() covering various problematic docstring patterns:
    • Single and multiple backslashes
    • Triple quotes inside docstrings
    • Trailing double quotes
    • Single quotes
    • Plain text (baseline)

Implementation Details

The solution preserves the readable triple-quoted form for normal docstrings while automatically using Python's repr() for edge cases, ensuring the serialized model code is always syntactically valid and can be properly deserialized.

Fixes #227

https://claude.ai/code/session_0112DJ37cY7YpwU5jov8eKBZ

claude added 2 commits May 11, 2026 12:51
Model, UserSpace, and lambda Cells docstrings were written as raw
triple-quoted strings without escaping, so docs containing backslashes,
embedded triple quotes, or a trailing quote produced invalid Python
that the deserializer could not parse.

Switch to a helper that keeps the readable triple-quoted form for plain
text but falls back to repr() when the content would break it.

Fixes #227

https://claude.ai/code/session_0112DJ37cY7YpwU5jov8eKBZ
The earlier fix fell back to repr() whenever the docstring contained a
backslash, which changed the source format on round-trip for any model
with backslash escapes in its docs (e.g. lifelib's TradLife_A, which
uses LaTeX-style \require / \overline sequences).

Escape backslashes inside the triple-quoted form so the readable
multi-line layout is preserved, and only fall back to repr() when the
content genuinely cannot be expressed as """...""" (embedded triple
quotes or a trailing double quote).

Verified round-trip of lifelib's TradLife_A produces byte-identical
files. Added test_doc_roundtrip_file_stable to lock this in.

https://claude.ai/code/session_0112DJ37cY7YpwU5jov8eKBZ
@fumitoh fumitoh merged commit cee26f8 into main May 11, 2026
22 checks passed
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.

Double backslashes in Model.doc and UserSpace.doc aren't properly serialized and unserialized.

2 participants