Skip to content

[http-client-python] Declare TypedDict subclasses with total=False - #12641

Open
Minh Triet Nguyen (binnguyenx) wants to merge 2 commits into
microsoft:mainfrom
binnguyenx:python/typeddict-subclass-total-false
Open

Minh Triet Nguyen (binnguyenx) wants to merge 2 commits into
microsoft:mainfrom
binnguyenx:python/typeddict-subclass-total-false

Conversation

@binnguyenx

Copy link
Copy Markdown

Fixes #12625.

Problem

In types.py, base TypedDicts are emitted as class Base(TypedDict, total=False):, but subclasses are emitted as class Child(Base):. TypedDict totality isn't inherited. It only applies to the keys declared in that class body. So every optional field a subclass declares becomes required, and partial request dicts don't type-check.

For example, in azure-mgmt-purview, Account(ProxyResource) required properties, tags, location, identity and sku. So the SDK's own sample account={"location": "West US 2"} fails mypy:

Missing keys ("identity", "properties", "sku", "tags") for TypedDict "Account"

Across azure-sdk-for-python this affects 1,646 subclassed TypedDicts in 137 generated types.py files. A hand fix to the purview package (Azure/azure-sdk-for-python#50095) was closed because regeneration would overwrite it, with a request to fix it here.

Fix

declare_model in types_serializer.py now emits class Child(Base, total=False):. This is safe because declare_property already wraps every required field in Required[...], so only the optional fields change. Flat TypedDicts (needs_flat_typeddict) and the functional form already used total=False and aren't changed.

Tests

  • Added test_typeddict_subclass_keeps_own_optional_fields_optional. It serializes a Base/Derived pair with one optional and one required child field, runs the generated source, and checks Derived.__required_keys__ == {"kind"} and __optional_keys__ == {"id", "location"}.
  • Updated test_typeddict_inheritance_without_requiredness_change_still_subclasses, which asserted the old class Derived(Base): output.
  • Both tests fail without the fix and pass with it. The rest of tests/unit/test_typeddict.py passes, except test_models_mode_typeddict_does_not_write_empty_models_folder, which also fails locally on unmodified main.
  • black and pylint are clean with eng/scripts/ci/config, and the mypy error count is unchanged.

I couldn't run the full npm run regenerate / npm run ci locally (no pnpm or Node 22 here), so CI will be the first full regeneration with this change.

🤖 Generated with Claude Code

TypedDict totality is not inherited: it only applies to the keys declared
in that class body. Base TypedDicts in types.py are emitted with
total=False, but subclasses were emitted as `class Child(Parent):`, so every
optional field a subclass declares became required. Required fields are
already wrapped in Required[...], so adding total=False to subclasses only
changes the optional ones.

Fixes microsoft#12625.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@azure-pipelines

Copy link
Copy Markdown
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.

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 recorded for the change preserving optional fields in TypedDict subclasses.

No open issues 🎉

What changed in this PR

Fixes #12625 in the Python HTTP client emitter so optional fields declared by TypedDict subclasses remain optional, allowing partial request dictionaries to type-check correctly.

Changes:

  • Emits total=False on TypedDict subclasses while preserving required fields through Required[...].
  • Updates inheritance expectations and adds regression coverage for required and optional keys.
  • Adds a Python emitter changelog entry.
File Description
packages/​http-client-python/​tests/​unit/​test_typeddict.py Verifies subclass declarations and required/optional key sets.
packages/​http-client-python/​generator/​pygen/​codegen/​serializers/​types_serializer.py Adds total=False to subclass declarations and documents why.
.chronus/​changes/​python-typeddict-subclass-total-false-2026-10-23-12-0-0.md Records the inherited-model optional-field fix.

Review effort: Balanced

@binnguyenx

Copy link
Copy Markdown
Author

Yuchao Yan (@msyyc) could you please comment /azp run on this PR? One pipeline (likely the Python emitter tests) was filtered out and needs an authorized user to trigger it. Thanks!

Copilot AI balanced review requested due to automatic review settings November 24, 2026 04:55

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 TypedDict subclass optional-field fix.

No open issues 🎉

Review effort: Balanced

@pkg-pr-new

pkg-pr-new Bot commented Nov 24, 2026

Copy link
Copy Markdown

Open in StackBlitz

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

commit: 0aa20f7

@github-actions

Copy link
Copy Markdown
Contributor

All changed packages have been documented.

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

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

Fix optional fields of inherited models becoming required in the generated types.py. TypedDict subclasses are now declared with total=False, like base TypedDicts, because totality is not inherited. Required fields are still marked with Required[...].

@github-actions

Copy link
Copy Markdown
Contributor

Python emitter diff

Baseline gh:0c042af2cedd4acc8430615ad6a30dc0c27b5a56 vs this PR.

Diff summary: 18 file(s), +60 / -56

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

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

thanks for the pr!

@binnguyenx

Copy link
Copy Markdown
Author

Thanks for the review, Libba Lawrence (@l0lawrence)! All checks are passing. Could you merge this when you have a moment? I don't have merge permissions on this repo. Thanks!

@binnguyenx

Copy link
Copy Markdown
Author

Hi Libba Lawrence (@l0lawrence) Yuchao Yan (@msyyc), a quick question while this waits to be merged. I have a few more fixes ready on my fork, but GitHub currently refuses to let me open more PRs on this repo (does not have the correct permissions to execute CreatePullRequest). That started after my second open PR, so I'm guessing there's a limit on open PRs for new contributors.

The branches are ready with tests and changelog entries:

Is there a limit I should work within, for example opening the next one after this PR merges? Or could it be raised? Thanks!

This branch has not been deployed

No deployments
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] TypedDict subclasses in types.py are missing total=False, making inherited-model fields required

3 participants