Skip to content

[Python] Support constrained discriminated unions - #12689

Merged
l0lawrence merged 6 commits into
microsoft:mainfrom
l0lawrence:l0lawrence-python-spector-coverage
Dec 3, 2026
Merged

l0lawrence merged 6 commits into
microsoft:mainfrom
l0lawrence:l0lawrence-python-spector-coverage

Conversation

@l0lawrence

@l0lawrence l0lawrence commented Nov 30, 2026 •

Copy link
Copy Markdown
Member

Fixes #12374

Newly supported: An inline discriminated union extending a base, where every variant inherits from that base. Python generates a base model, variant subclasses, discriminator dispatch, and a Union[...] input alias.

Other TypeSpec shapes:

  • Plain union: union Pet { cat: Cat, dog: Dog } has no discriminator.
  • Structural-only variants: union Pet extends PetBase where Cat and Dog have the base’s fields but don’t actually extend PetBase. It remains a union.
  • @discriminator model: A model hierarchy such as @discriminator("kind") model PetBase with subclasses, rather than a union declaration.
  • Discriminated union without extends: @discriminated(...) union Pet { cat: Cat, dog: Dog } has a discriminator but no base constraint.

These shapes keep their existing behavior, though the PR also fixes inherited-property duplication and TypedDict field narrowing. Envelope unions and default variants are out of scope.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@microsoft-github-policy-service microsoft-github-policy-service Bot added the emitter:client:python Issue for the Python client emitter: @typespec/http-client-python label Nov 30, 2026
@pkg-pr-new

pkg-pr-new Bot commented Nov 30, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

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

commit: 779f099

@github-actions

github-actions Bot commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

All changed packages have been documented.

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

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

Support inline discriminated unions whose variants extend a base model, including variants that inherit through an intermediate model.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@github-actions

github-actions Bot commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

Python emitter diff

Baseline gh:12694143cef33b80b479f73a3bca4b248b1e8311 vs this PR.

Diff summary: 179 file(s), +20426 / -0

Rendered diff: inline on the run summary, or the emitter-diff-html artifact.

Informational check (eng/emitter-diff); does not block the PR.

@azure-sdk-automation

azure-sdk-automation Bot commented Nov 30, 2026 •

Copy link
Copy Markdown

You can try these changes here

🛝 Playground 🌐 Website 🛝 VSCode Extension

l0lawrence and others added 2 commits December 1, 2026 08:49
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

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

The release note incorrectly claims structural-variant discriminator support, while tests verify those variants remain a combined union; it should describe the implemented nominal-inheritance support instead.

1 open issue
What changed in this PR

Improves Python SDK generation for constrained discriminated unions with inherited variants and adds the Spector coverage requested by #12374.

Changes:

  • Marks converted union bases and variants in the emitter’s code model.
  • Handles transitive discriminator dispatch, cross-namespace imports, inherited properties, and TypedDict generation.
  • Enables union-extends regeneration and adds emitter, generator, and synchronous/asynchronous Spector tests.
File Description
packages/​http-client-python/​tests/​unit/​test_discriminated_union.py Adds generator and runtime regressions for dispatch, inheritance, and TypedDicts.
packages/​http-client-python/​tests/​mock_api/​shared/​test_typetest_union_discriminated_extends.py Adds synchronous Spector coverage for discriminator variants and recursion.
packages/​http-client-python/​tests/​mock_api/​shared/​asynctests/​test_typetest_union_discriminated_extends_async.py Adds equivalent asynchronous Spector coverage.
packages/​http-client-python/​generator/​pygen/​codegen/​templates/​model_dpg.py.jinja2 Emits cross-namespace loading and transitive discriminator registrations.
packages/​http-client-python/​generator/​pygen/​codegen/​serializers/​types_serializer.py Handles cross-namespace union aliases and flattened TypedDict overrides.
packages/​http-client-python/​generator/​pygen/​codegen/​serializers/​model_serializer.py Generates variant imports and ancestor discriminator registrations.
packages/​http-client-python/​generator/​pygen/​codegen/​models/​model_type.py Deduplicates inherited properties and enables input TypedDicts for variants.
packages/​http-client-python/​eng/​scripts/​ci/​regenerate-common.ts Enables union-extends compilation and removes the scenario skip.
packages/​http-client-python/​emitter/​test/​test-host.ts Adds a shared union-extends emitter tester.
packages/​http-client-python/​emitter/​test/​emitter.test.ts Tests inherited conversion, structural fallback, and ordinary discriminator models.
packages/​http-client-python/​emitter/​src/​types.ts Tags converted discriminated-union bases and variants.
.chronus/​changes/​python-discriminated-union-inheritance-2026-11-30-10-11-34.md Adds the Python package release note.

Review effort: Balanced

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings December 2, 2026 18:53

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.

🟢 Approval recommended

No active review issues remain for the Python discriminated-union inheritance changes.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

Copilot AI balanced review requested due to automatic review settings December 2, 2026 20:29

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.

🟢 Approval recommended

No active review issues are reported for the constrained discriminated-union support change.

No open issues 🎉

Review effort: Balanced

@l0lawrence
l0lawrence added this pull request to the merge queue Dec 3, 2026
Merged via the queue into microsoft:main with commit 8dc31ac Dec 3, 2026
37 checks passed
@l0lawrence
l0lawrence deleted the l0lawrence-python-spector-coverage branch December 3, 2026 17:06
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.

[python] add test case for https://github.com/microsoft/typespec/pull/12288

3 participants