Commit a791115
fix(http-client-python): generate valid Python type annotations in types.py (#11638)
The Python client emitter generated `types.py` files with invalid type
annotations for `azure-search-documents`, producing 4 MyPy errors across
three generated files. This fixes the three distinct codegen bugs behind
them. Each bug below shows the generated-code output before and after
the fix.
## Bug 1: inconsistent internal-enum export and reference
`azure/search/documents/types.py`
```diff
from typing import TYPE_CHECKING, Union
from typing_extensions import TypedDict
if TYPE_CHECKING:
- from .models import SemanticQueryRewritesResultType
+ from .models._enums import SemanticQueryRewritesResultType
class SearchDocumentsResult(TypedDict, total=False):
- semanticQueryRewritesResultType: Union[str, "_enums.SemanticQueryRewritesResultType"]
+ semanticQueryRewritesResultType: Union[str, "SemanticQueryRewritesResultType"]
```
Before, the import pulled the bare name from the public `.models`
package, where the internal enum is not re-exported (`Module "...models"
has no attribute "SemanticQueryRewritesResultType" [attr-defined]`), and
the annotation referenced an undefined `_enums.` prefix (`Name "_enums"
is not defined [name-defined]`). After, the enum symbol is imported
directly from the private `._enums` submodule and the annotation uses
the matching bare forward reference. Enums stay private (no change to
the public `__all__`).
## Bug 2: duplicate enum import
`azure/search/documents/knowledgebases/types.py`
```diff
- from typing import TYPE_CHECKING, Union
+ from typing import Union
from typing_extensions import TypedDict
from ..indexes.models._enums import KnowledgeSourceKind
- if TYPE_CHECKING:
- from ..indexes.models import KnowledgeSourceKind
-
class KnowledgeSource(TypedDict, total=False):
kind: Union[str, "KnowledgeSourceKind"]
```
Before, the same symbol was imported at runtime from its `_enums`
submodule and again under `if TYPE_CHECKING:` from the public package
(`Name "KnowledgeSourceKind" already defined [no-redef]`). After, a
dedup pass drops the `TYPE_CHECKING` duplicate whose bound name is
already imported at runtime; the runtime import is sufficient for the
annotations.
## Bug 3: TypedDict requiredness override via inheritance
`azure/search/documents/indexes/types.py`
```diff
class SearchIndexerKnowledgeStoreProjectionSelector(TypedDict, total=False):
referenceKeyName: str
generatedKeyName: str
- class SearchIndexerKnowledgeStoreTableProjectionSelector(
- SearchIndexerKnowledgeStoreProjectionSelector
- ):
+ class SearchIndexerKnowledgeStoreTableProjectionSelector(TypedDict, total=False):
+ referenceKeyName: str
generatedKeyName: Required[str]
tableName: Required[str]
```
Before, the child subclassed the parent and redeclared
`generatedKeyName` as `Required[str]`, which PEP 589 forbids
(`Overwriting TypedDict field "generatedKeyName" while extending
[misc]`). After, the child renders as a flat, non-inheriting `TypedDict`
that lists every field (inherited plus own) directly, so requiredness is
expressed without illegal inheritance.
## Notes for reviewers
- The internal-enum import intentionally imports the bare symbol from
`_enums` rather than the `_enums` module, which also avoids name
collisions when internal enums come from several sibling namespaces.
- Regression tests were added to `tests/unit/test_typeddict.py` covering
all three cases.
## Validation
- `test_typeddict.py` + `test_enums.py`: 50 passed (42 existing + 8
new).
- pylint 10.00/10 on the changed files; black clean under the project
config; mypy introduces no new errors (the pre-existing errors are
unchanged on the baseline).
- Reproduced the fix end to end against the issue's errors: a minimal
`azure/search/documents` package built from the buggy forms reproduces
the `attr-defined`, `name-defined`, and `misc` (TypedDict) errors under
`mypy --python-version 3.10`, and the fixed forms type-check with exit
0. Bug 2's duplicate import is confirmed removed at the serializer level
(baseline emits two imports of `KnowledgeSourceKind`, the fix emits
one).
Fixes: #11626
---------
Co-authored-by: iscai-msft <43154838+iscai-msft@users.noreply.github.com>
Copilot-Session: 1a8cce88-1663-4cc6-a634-2e83e35d04b71 parent 48030ba commit a791115
6 files changed
Lines changed: 356 additions & 94 deletions
File tree
- .chronus/changes
- packages/http-client-python
- generator/pygen/codegen
- models
- serializers
- tests/unit
Lines changed: 7 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
62 | 62 | | |
63 | 63 | | |
64 | 64 | | |
| 65 | + | |
65 | 66 | | |
66 | 67 | | |
67 | 68 | | |
| |||
240 | 241 | | |
241 | 242 | | |
242 | 243 | | |
| 244 | + | |
243 | 245 | | |
244 | 246 | | |
245 | 247 | | |
| |||
Lines changed: 14 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
203 | 203 | | |
204 | 204 | | |
205 | 205 | | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
206 | 210 | | |
207 | 211 | | |
208 | 212 | | |
| |||
305 | 309 | | |
306 | 310 | | |
307 | 311 | | |
308 | | - | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
309 | 321 | | |
310 | | - | |
| 322 | + | |
311 | 323 | | |
312 | 324 | | |
313 | 325 | | |
| |||
Lines changed: 27 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
84 | 84 | | |
85 | 85 | | |
86 | 86 | | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
87 | 113 | | |
88 | 114 | | |
89 | 115 | | |
| |||
106 | 132 | | |
107 | 133 | | |
108 | 134 | | |
| 135 | + | |
109 | 136 | | |
110 | 137 | | |
111 | 138 | | |
| |||
Lines changed: 40 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
243 | 243 | | |
244 | 244 | | |
245 | 245 | | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
246 | 279 | | |
247 | 280 | | |
248 | 281 | | |
| |||
303 | 336 | | |
304 | 337 | | |
305 | 338 | | |
306 | | - | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
307 | 344 | | |
308 | 345 | | |
309 | 346 | | |
| |||
329 | 366 | | |
330 | 367 | | |
331 | 368 | | |
332 | | - | |
| 369 | + | |
333 | 370 | | |
334 | 371 | | |
335 | 372 | | |
| |||
362 | 399 | | |
363 | 400 | | |
364 | 401 | | |
365 | | - | |
| 402 | + | |
366 | 403 | | |
367 | 404 | | |
368 | 405 | | |
| |||
0 commit comments