Repository navigation
[http-client-python] Declare TypedDict subclasses with total=False - #12641
Minh Triet Nguyen (binnguyenx) wants to merge 2 commits into
Conversation
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: 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. |
There was a problem hiding this comment.
🟢 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=Falseon TypedDict subclasses while preserving required fields throughRequired[...]. - 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
|
Yuchao Yan (@msyyc) could you please comment |
commit: |
|
All changed packages have been documented.
Show changes
|
Python emitter diffBaseline 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. |
Libba Lawrence (l0lawrence)
left a comment
There was a problem hiding this comment.
thanks for the pr!
|
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! |
|
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 ( 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! |
Fixes #12625.
Problem
In
types.py, base TypedDicts are emitted asclass Base(TypedDict, total=False):, but subclasses are emitted asclass 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)requiredproperties,tags,location,identityandsku. So the SDK's own sampleaccount={"location": "West US 2"}fails mypy:Across
azure-sdk-for-pythonthis affects 1,646 subclassed TypedDicts in 137 generatedtypes.pyfiles. 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_modelintypes_serializer.pynow emitsclass Child(Base, total=False):. This is safe becausedeclare_propertyalready wraps every required field inRequired[...], so only the optional fields change. Flat TypedDicts (needs_flat_typeddict) and the functional form already usedtotal=Falseand aren't changed.Tests
test_typeddict_subclass_keeps_own_optional_fields_optional. It serializes aBase/Derivedpair with one optional and one required child field, runs the generated source, and checksDerived.__required_keys__ == {"kind"}and__optional_keys__ == {"id", "location"}.test_typeddict_inheritance_without_requiredness_change_still_subclasses, which asserted the oldclass Derived(Base):output.tests/unit/test_typeddict.pypasses, excepttest_models_mode_typeddict_does_not_write_empty_models_folder, which also fails locally on unmodifiedmain.eng/scripts/ci/config, and the mypy error count is unchanged.I couldn't run the full
npm run regenerate/npm run cilocally (no pnpm or Node 22 here), so CI will be the first full regeneration with this change.🤖 Generated with Claude Code