fix(python): escape quotes in enum documentation - #11897
fix(python): escape quotes in enum documentation#11897RKS (rksharma-owg) wants to merge 2 commits into
Conversation
|
Azure Pipelines: Successfully started running 1 pipeline(s). 1 pipeline(s) were filtered out due to trigger conditions. There may be pipelines that require an authorized user to comment /azp run to run. |
There was a problem hiding this comment.
🟡 Changes recommended
Enum descriptions rendered as Literal[...] in types.py.jinja2 still use unescaped wrap_string inside """...""", leaving the same triple-quote breakout risk in typeddict mode.
Once you've addressed the issues Copilot identified, you can request another Copilot review.
Pull request overview
Fixes a Python emitter correctness issue where enum/enum-member documentation containing """ can prematurely terminate generated docstrings, causing subsequent text to be treated as executable Python at import time.
Changes:
- Add a reusable
wrap_docstringJinja macro that escapes double quotes after existing wrapping/backslash escaping. - Use
wrap_docstringfor enum class and enum member docstrings inenum.py.jinja2. - Add regression tests that parse the formatted Python AST to ensure documentation stays within a string literal, plus a
.chronusfix entry.
File summaries
| File | Description |
|---|---|
| packages/http-client-python/tests/unit/test_enum_docstrings.py | Adds AST-based regressions ensuring enum docs remain string literals even with embedded """, backslashes, and trailing quotes. |
| packages/http-client-python/generator/pygen/codegen/templates/operation_tools.jinja2 | Introduces wrap_docstring macro to escape quotes after wrapping. |
| packages/http-client-python/generator/pygen/codegen/templates/enum.py.jinja2 | Switches enum and enum member docstring emission to the new wrap_docstring helper. |
| .chronus/changes/escape-python-enum-docstrings.md | Records a fix changeset for @typespec/http-client-python. |
Review details
- Files reviewed: 4/4 changed files
- Comments generated: 1
- Review effort level: Lite
💡 Configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| {# Escape quotes after wrapping so wrapping cannot split an escape sequence. #} | ||
| {% macro wrap_docstring(string, wrapstring, width=95) -%} | ||
| {{ wrap_string(string, wrapstring, width) | replace('"', '\\"') }} | ||
| {%- endmacro %} |
Cover Literal enum rendering with the same AST and documentation-preservation checks as class and member docstrings. Assisted-by: OpenAI Codex (model: GPT-6 Astra, autonomous)
|
Posted on behalf of RKS (@rksharma-owg) by OpenAI Codex. Addressed the TypedDict/Literal enum path in b89dbca: Package build/wheel creation, all 23 emitter tests, targeted Pylint/Black checks, and full Azure/unbranded SDK regeneration passed. Regeneration produced no generated-file changes. The focused Python tests use |
There was a problem hiding this comment.
🟢 Approval recommended
The change is narrowly scoped to enum docstring rendering, includes a reusable helper, adds targeted regression tests that would fail on reversion, and includes an appropriate .chronus fix entry.
Review details
- Files reviewed: 5/5 changed files
- Comments generated: 0 new
- Review effort level: Lite
Enum and enum member documentation containing triple quotes can terminate its generated Python docstring. For syntactically valid input, the formatter accepts the following documentation as class-body statements; trailing single quotes can also make member docstrings invalid Python.
Add a reusable docstring-wrapping macro that escapes quotes after the existing prose wrapping and backslash escaping, and use it for enum classes, members, and
Literal[...]enum descriptions in TypedDict output. This preserves ordinary documentation.Fixes #11881. Related to #10784; this change specifically covers quotes at the enum rendering boundary.
Validation:
git diff --checkpass. Linters were invoked directly to work around local optional-dependency and path-with-spaces problems in the wrapper.test_polymorphic_deserialization,test_enumeration_results_blobs_unwrapped, andtest_models_mode_typeddict_does_not_write_empty_models_folder. The local test run uses--confcutdir=tests/unitto avoid starting the unrelated mock API server.