Repository navigation
[http-client-python] Escape generated string literals and wire names - #12342
Yuchao Yan (msyyc) wants to merge 12 commits into
Conversation
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: ecfba725-b27d-416c-95a6-bc48a498ca8f
commit: |
|
All changed packages have been documented.
Show changes
|
There was a problem hiding this comment.
🟡 Changes recommended
Targeted verification confirms that downstream template rendering still corrupts escaped values and can produce invalid Python examples.
1 open issue
What changed in this PR
This PR updates the Python client generator to serialize specification strings as Python literals, preserving special characters and treating expression-like text as data.
Changes:
- Replace manual quoting in string declarations, discriminator arguments, initializers, and template values.
- Add regression tests for literal preservation and generated model behavior; adjust TypedDict quotation expectations.
- Add a Python emitter fix changelog.
| File | Description |
|---|---|
packages/http-client-python/tests/unit/test_typeddict.py |
Update two expectations for equivalent literal quoting. |
packages/http-client-python/tests/unit/test_string_literals.py |
Add special-character and discriminator regression coverage. |
packages/http-client-python/generator/pygen/codegen/serializers/model_serializer.py |
Escape discriminator initializers and class arguments. |
packages/http-client-python/generator/pygen/codegen/models/primitive_types.py |
Serialize string declarations with repr. |
packages/http-client-python/generator/pygen/codegen/models/model_type.py |
Escape discriminator values in template representations. |
.chronus/changes/python-discriminator-string-literals-2026-10-22.md |
Document the Python string-literal fix. |
Review effort: Balanced
Python emitter diffBaseline No changes to generated output. Rendered diff: inline on the run summary, or the emitter-diff-html artifact. Informational check (eng/emitter-diff); does not block the PR. |
|
You can try these changes here
|
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: ecfba725-b27d-416c-95a6-bc48a498ca8f
|
CI investigation: the optional Azure/typespec-azure integration check fails on both the original and follow-up heads, outside this Python change. Latest run: https://github.com/microsoft/typespec/actions/runs/39734024339/job/119675416698. The downstream playground imports registerMonacoDefaultWorkersForVite from @typespec/playground, but the current API is exported from @typespec/playground/vite/workers. The downstream import needs updating; this PR does not change playground exports or bypass the check. All other current-head checks have now passed. Tracked in Azure/typespec-azure#5806; monitoring continues. |
| {% for template_line in example_template %} | ||
| {% if template_line %} | ||
| {% set wrap_amount = (template_line | length) - (template_line.lstrip() | length) + 10 %} | ||
| {{ wrap_string(template_line, wrapstring='\n' + " " * wrap_amount, width=(95 - wrap_amount)) }} |
There was a problem hiding this comment.
iscai-msft I remember the code has special effect for specific SDK doc string, so not sure whether it is ok to remove the logic
There was a problem hiding this comment.
we added it to prevent overlong lines in generated docstring examples, while keeping wrapped continuation lines indented inside the sphinx code block .. code-block:: python, it might be one of the agent sdks, i can't remember exactly which sdk it was
can you have copilot have a look to see if we can still preserve this ability and write it in a better way?
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…tion rendering Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
🟡 Changes recommended
A string default containing triple quotes still produces an unparsable generated operation module because parameter descriptions are rendered without escaping quotes.
3 open issues
1 resolved since last review
Review effort: Balanced
…rings Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
iscai-msft
left a comment
There was a problem hiding this comment.
there are a lot of diffs in the generated code, and i think most of them we don't want to change. i believe there's also a testserver case for the "too-long example code", you could see what changes when you remove the code in jinja
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: aaeb82ef-b4f1-4a88-9442-edd2ea4061cf
Merge main to align core dependency catalog references with Azure integration. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: aaeb82ef-b4f1-4a88-9442-edd2ea4061cf
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: aaeb82ef-b4f1-4a88-9442-edd2ea4061cf
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: aaeb82ef-b4f1-4a88-9442-edd2ea4061cf

Summary
Fix Python string-literal generation described in this report and #12405.
Validation
pnpm format, and emitter/generator lint passed.The deselected test,
test_models_mode_typeddict_does_not_write_empty_models_folder, fails identically against an isolated copy of the unchanged PR-head generator. It is unrelated to string serialization and is not changed here. Local Python runtime validation used Python 3.13.