Repository navigation
[http-client-python] Escape generated discriminator string literals - #12342
Yuchao Yan (msyyc) wants to merge 2 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 Diff summary: 1174 file(s), +5813 / -5821 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
| source = "\n".join( | ||
| [ | ||
| "class Base(_Model):", | ||
| " __mapping__ = {}", | ||
| ' kind: str = rest_discriminator(name="kind")', | ||
| serializer.declare_model(model), |

Summary
Fix Python string-literal generation described in this report.
Validation
The deselected test,
test_models_mode_typeddict_does_not_write_empty_models_folder, fails identically against the unchanged baseline. It is unrelated to string serialization and is not changed here. Local Python runtime validation used Python 3.13.Repository-wide
pnpm formatreports a parse error in the unchangedpackages/http-specs/specs/type/union/discriminated/extends/main.tsp. Python emitter package formatting checks pass.