Fix docstring serialization for special characters#228
Merged
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
_format_docstring()helper function inserializer_6.pythat intelligently formats docstrings:"""...""") for simple docstrings without special charactersrepr()for docstrings containing backslashes,""", or trailing"to ensure valid Python syntaxtest_doc_special_characters()covering various problematic docstring patterns: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