-
Notifications
You must be signed in to change notification settings - Fork 371
[python] add typeddict models-mode for Python HTTP client emitter
#10439
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
iscai-msft
wants to merge
54
commits into
microsoft:main
Choose a base branch
from
iscai-msft:python/addTypedDict
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,961
−109
Open
Changes from all commits
Commits
Show all changes
54 commits
Select commit
Hold shift + click to select a range
0834a2c
feat: add typeddict models-mode for Python HTTP client emitter
27c8bfb
Enhance Python HTTP client emitter with TypedDict support
iscai-msft c1487f8
add discriminator
02e6f35
Merge branch 'python/addTypedDict' of https://github.com/iscai-msft/t…
2647828
Merge branch 'main' of https://github.com/microsoft/typespec into pyt…
3a125b8
feat: return JSON for typeddict responses, drop NotRequired
e0569db
feat: add wire name mock API tests for typeddict naming spec
3e5c0ef
fix: remove redundant JSON overload for typeddict mode
f76c88d
fix: remove unused _deserialize import in typeddict mode
2401b66
fix: remove all unused imports in typeddict generated code
57bf76a
Merge branch 'main' into python/addTypedDict
iscai-msft 47b5024
format and lint
87ea0a1
Merge branch 'python/addTypedDict' of https://github.com/iscai-msft/t…
95db199
fix: define JSON type alias in TypedDictModelType imports
37806df
switch to always generating typeddicts as typing hints
18786e3
switch to always generating typeddicts as typing hints
8d323ac
Merge branch 'python/addTypedDict' of https://github.com/iscai-msft/t…
db10eeb
move discriminated union to types.py
841b2ee
Merge branch 'main' of https://github.com/microsoft/typespec into pyt…
a206181
format
9572470
add for output as well
cdcedf5
add e2e tests
1d1d7c6
update unions serializer to get around pyright issue
f5e1f7d
fix: resolve pylint and pyright errors in generated types.py
f5f9300
Merge branch 'main' of https://github.com/microsoft/typespec into pyt…
f3be83c
fix pylint and pyright in generated code
6a34daf
Merge branch 'main' of https://github.com/microsoft/typespec into pyt…
bf7ef34
fix: move discriminated union aliases from _unions.py to types.py
e6b1b52
fix: topologically sort discriminated union aliases in types.py
df17af5
fix: skip model imports in types.py regardless of namespace
cedece0
fix: skip imports for discriminated base models in types.py
4497199
fix: sort TypedDict properties to avoid builtin name shadowing
7c8bf77
fix: handle reserved words in TypedDict field names comprehensively
136e247
fix: generate types.py per namespace instead of only at top level
b14f874
fix: correct cross-namespace imports in per-namespace types.py
c67d633
feat: combine regenerate scripts and add typeddict variant packages
1dd8314
feat: add client-naming-typeddict variant package
48d2aed
refactor: remove unnecessary is_response pops from TypedDictModelType
a130bfc
fix: use TypedDict dicts instead of model classes in naming test
6a15774
fix: skip self-import in types.py and unused _deserialize for typed-d…
3789713
fix: skip _models prefix for internal models in types.py annotations
762d694
merge: resolve upstream/main conflict, inline regenerate-common.ts ch…
bd83e9f
Merge branch 'main' of https://github.com/microsoft/typespec into pyt…
6c0ee44
switch to models-mode=typeddict to represent td only, generate union …
6d75f07
don't conflate optional and nullable
27113ce
format and lint
1de57f4
fix: address PR review comments for TypedDict support
a88d471
lint
d9341d0
Merge branch 'main' into python/addTypedDict
iscai-msft ca95971
fix: address review comments on TypedDict PR
177a46c
fix: remove typeddict-specific guard from no-sdk-clients check
a1101da
feat: replace JSON overloads with TypedDict overloads in operations
a99065d
Merge remote-tracking branch 'upstream/main' into python/addTypedDict
3cea12c
fix: restore regenerate-common.ts
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| --- | ||
| # Change versionKind to one of: internal, fix, dependencies, feature, deprecation, breaking | ||
| changeKind: feature | ||
| packages: | ||
| - "@typespec/http-client-python" | ||
| --- | ||
|
|
||
| [python] Always generate `TypedDict` typing hints for input models in the `types.py` file, and named union aliases in the `_unions.py` file |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -233,6 +233,7 @@ function emitProperty( | |
| // Python convert all the type of file part to FileType so clear these models' usage so that they won't be generated | ||
| addDisableGenerationMap(context, property.type); | ||
| } | ||
| const isNullable = !isMultipartFileInput && sourceType.kind === "nullable"; | ||
| return { | ||
| clientName: getClientName(property), | ||
| isExactName: property.isExactName, | ||
|
|
@@ -242,6 +243,7 @@ function emitProperty( | |
| : property.serializationOptions?.json?.name) ?? property.name, | ||
| type: getType(context, sourceType), | ||
| optional: property.optional, | ||
| nullable: isNullable, | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we are conflating nullable and optional. this is one step to help with the conflation, since it's extra important in typeddicts to not type things as optional when we mean nullable |
||
| description: property.summary ? property.summary : property.doc, | ||
| addedOn: getAddedOn(context, property), | ||
| apiVersions: property.apiVersions, | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
According to issue description, we should refuse generation for unsupported type. Then I understand we should add check in
emitterto scan all types and report error diagnostics if there is unsupported type:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you're right, I had forgotten about that. We have added discriminator to the things we are allowed to generate, but for the other ones, now that we've decided to always generate TD, @johanste should we just not generate TD types for these cases and rely on JSON in the overload?