fix(http-client-python): generate valid Python type annotations in types.py - #11627
Closed
Libba Lawrence (l0lawrence) wants to merge 1 commit into
Closed
Conversation
…pes.py Fixes three codegen bugs producing invalid annotations in generated types.py: - Internal enums used as TypedDict fields are imported as a bare symbol from their private _enums submodule, and the annotation no longer emits an undefined _enums. prefix. - Deduplicate runtime and TYPE_CHECKING imports of the same symbol to avoid mypy no-redef errors. - Emit a flat (non-inheriting) TypedDict when a child overrides an inherited field's requiredness, satisfying PEP 589. Adds regression tests for all three cases. Fixes: microsoft#11626 Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: 1a8cce88-1663-4cc6-a634-2e83e35d04b7
commit: |
Contributor
|
All changed packages have been documented.
Show changes
|
|
You can try these changes here
|
Member
Author
|
Reopening as a same-repo PR (branch pushed to microsoft/typespec) so the emitter-diff CI check runs. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The Python client emitter generated
types.pyfiles with invalid type annotations forazure-search-documents, producing 4 MyPy errors across three generated files. This fixes the three distinct codegen bugs behind them.What was wrong and how it's fixed
1. Internal enum annotation/import mismatch. An internal enum used as a TypedDict field was annotated as
_enums.Namewhile_enumswas never imported (and the enum is not re-exported from the publicmodelspackage), yieldingName '_enums' is not definedandModule has no attributeerrors. The enum is now imported as a bare symbol from its private_enumssubmodule, and theTYPES_FILEannotation drops the undefined_enums.prefix. Enums stay private (no change to the public__all__).2. Duplicate enum import. The same symbol could be imported once at runtime and again under
if TYPE_CHECKING:(from different modules), triggering mypyno-redef. Added a dedup pass that drops aTYPE_CHECKINGsubmodule import when its bound name is already imported at runtime; the runtime import is sufficient for the annotations.3. TypedDict requiredness override via inheritance. A child TypedDict that redeclared an inherited field with different requiredness produced
Overwriting TypedDict field ... while extending, which PEP 589 forbids. Such models are now emitted as a flat, non-inheriting TypedDict that lists every field (inherited plus own) directly, so requiredness is expressed without subclassing.Notes for reviewers
_enumsrather than the_enumsmodule, which also avoids name collisions when internal enums come from several sibling namespaces.tests/unit/test_typeddict.pycovering all three cases.Validation
test_typeddict.py+test_enums.py: 50 passed (42 existing + 8 new).Fixes: #11626