Skip to content

[http-client-python] Escape generated discriminator string literals - #12341

Closed
Yuchao Yan (msyyc) wants to merge 1 commit into
microsoft:mainfrom
msyyc:fix/python-discriminator-string-literals
Closed

Yuchao Yan (msyyc) wants to merge 1 commit into
microsoft:mainfrom
msyyc:fix/python-discriminator-string-literals

Conversation

@msyyc

@msyyc Yuchao Yan (msyyc) commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

No description provided.

Co-authored-by: Copilot <[email protected]>
Copilot-Session: ecfba725-b27d-416c-95a6-bc48a498ca8f
@pkg-pr-new

pkg-pr-new Bot commented Oct 22, 2026

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/@typespec/http-client-python@12341

commit: 405aee9

@github-actions

Copy link
Copy Markdown
Contributor

All changed packages have been documented.

  • ✅ @typespec/http-client-python
Show changes

@typespec/http-client-python - fix ✏️

Escape string literals in generated Python discriminator declarations, initializers, and templates to preserve specification values as data.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Targeted verification confirms that downstream template formatting still corrupts escaped discriminator values or produces invalid Python.

1 open issue
What changed in this PR

This PR improves Python client generation by serializing specification strings as Python literals rather than manually adding quotes.

Changes:

  • Use repr() for string constants, discriminator declarations, initializers, and template values.
  • Add regression coverage for special characters and expression-like strings.
  • Update quotation-style expectations and add a fix changelog.
File Description
packages/​http-client-python/​tests/​unit/​test_typeddict.py Adjust expected literal quotation style.
packages/​http-client-python/​tests/​unit/​test_string_literals.py Add literal and discriminator regression tests.
packages/​http-client-python/​generator/​pygen/​codegen/​serializers/​model_serializer.py Escape discriminator class arguments and initializers.
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 Serialize discriminator template values with repr().
.chronus/​changes/​python-discriminator-string-literals-2026-10-22.md Document the Python emitter fix.

Review effort: Balanced

}
if self.discriminator and self.discriminator_value:
representation[f'"{self.discriminator.wire_name}"'] = f'"{self.discriminator_value}"'
representation[f'"{self.discriminator.wire_name}"'] = repr(self.discriminator_value)
@azure-sdk-automation

Copy link
Copy Markdown

You can try these changes here

🛝 Playground 🌐 Website 🛝 VSCode Extension

@github-actions

Copy link
Copy Markdown
Contributor

Python emitter diff

Baseline gh:721154b2ab55bfc78ab4850e86c40487f66241f2 vs this PR.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

emitter:client:python Issue for the Python client emitter: @typespec/http-client-python

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants