@typespec/http-client-python 0.35.1 → 0.36.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +6 -0
- package/dist/emitter/code-model.d.ts +1 -1
- package/dist/emitter/code-model.d.ts.map +1 -1
- package/dist/emitter/code-model.js.map +1 -1
- package/dist/emitter/emitter.d.ts +2 -2
- package/dist/emitter/emitter.d.ts.map +1 -1
- package/dist/emitter/emitter.js +23 -2
- package/dist/emitter/emitter.js.map +1 -1
- package/dist/emitter/external-process.d.ts +1 -1
- package/dist/emitter/external-process.d.ts.map +1 -1
- package/dist/emitter/external-process.js.map +1 -1
- package/dist/emitter/http.d.ts +2 -2
- package/dist/emitter/http.d.ts.map +1 -1
- package/dist/emitter/http.js +1 -1
- package/dist/emitter/http.js.map +1 -1
- package/dist/emitter/index.d.ts +2 -1
- package/dist/emitter/index.d.ts.map +1 -1
- package/dist/emitter/index.js.map +1 -1
- package/dist/emitter/lib.d.ts +3 -2
- package/dist/emitter/lib.d.ts.map +1 -1
- package/dist/emitter/lib.js +6 -1
- package/dist/emitter/lib.js.map +1 -1
- package/dist/emitter/node-runner.browser.d.ts +1 -1
- package/dist/emitter/node-runner.browser.d.ts.map +1 -1
- package/dist/emitter/node-runner.browser.js.map +1 -1
- package/dist/emitter/node-runner.d.ts +3 -3
- package/dist/emitter/node-runner.d.ts.map +1 -1
- package/dist/emitter/node-runner.js.map +1 -1
- package/dist/emitter/system-requirements.d.ts.map +1 -1
- package/dist/emitter/system-requirements.js.map +1 -1
- package/dist/emitter/types.d.ts +4 -4
- package/dist/emitter/types.d.ts.map +1 -1
- package/dist/emitter/types.js +15 -3
- package/dist/emitter/types.js.map +1 -1
- package/dist/emitter/utils.d.ts +2 -2
- package/dist/emitter/utils.d.ts.map +1 -1
- package/dist/emitter/utils.js +1 -1
- package/dist/emitter/utils.js.map +1 -1
- package/emitter/src/code-model.ts +4 -2
- package/emitter/src/emitter.ts +30 -6
- package/emitter/src/external-process.ts +2 -1
- package/emitter/src/http.ts +5 -5
- package/emitter/src/index.ts +2 -1
- package/emitter/src/lib.ts +11 -6
- package/emitter/src/node-runner.browser.ts +2 -1
- package/emitter/src/node-runner.ts +6 -3
- package/emitter/src/system-requirements.ts +2 -1
- package/emitter/src/types.ts +23 -8
- package/emitter/src/utils.ts +5 -4
- package/emitter/temp/tsconfig.tsbuildinfo +1 -1
- package/emitter/test/browser-pyodide.test.ts +25 -0
- package/emitter/test/types.test.ts +72 -0
- package/eng/scripts/ci/dev_requirements.txt +1 -1
- package/eng/scripts/ci/regenerate-common.ts +6 -2
- package/eng/scripts/ci/regenerate.ts +1 -2
- package/eng/scripts/ci/run-tests.ts +2 -1
- package/eng/scripts/setup/__pycache__/package_manager.cpython-311.pyc +0 -0
- package/eng/scripts/setup/__pycache__/venvtools.cpython-311.pyc +0 -0
- package/eng/scripts/setup/system-requirements.ts +2 -1
- package/generator/build/lib/pygen/__init__.py +24 -5
- package/generator/build/lib/pygen/codegen/models/__init__.py +3 -1
- package/generator/build/lib/pygen/codegen/models/code_model.py +7 -1
- package/generator/build/lib/pygen/codegen/models/enum_type.py +7 -7
- package/generator/build/lib/pygen/codegen/models/list_type.py +1 -0
- package/generator/build/lib/pygen/codegen/models/model_type.py +1 -3
- package/generator/build/lib/pygen/codegen/models/operation.py +7 -2
- package/generator/build/lib/pygen/codegen/models/parameter.py +3 -1
- package/generator/build/lib/pygen/codegen/models/utils.py +14 -0
- package/generator/build/lib/pygen/codegen/serializers/__init__.py +6 -6
- package/generator/build/lib/pygen/codegen/serializers/builder_serializer.py +25 -14
- package/generator/build/lib/pygen/codegen/serializers/client_serializer.py +13 -6
- package/generator/build/lib/pygen/codegen/serializers/model_serializer.py +4 -1
- package/generator/build/lib/pygen/codegen/serializers/types_serializer.py +4 -9
- package/generator/build/lib/pygen/preprocess/__init__.py +68 -17
- package/generator/build/lib/pygen/utils.py +15 -0
- package/generator/component-detection-pip-report.json +5 -5
- package/generator/dist/pygen-0.1.0-py3-none-any.whl +0 -0
- package/generator/pygen/__init__.py +24 -5
- package/generator/pygen/codegen/models/__init__.py +3 -1
- package/generator/pygen/codegen/models/code_model.py +7 -1
- package/generator/pygen/codegen/models/enum_type.py +7 -7
- package/generator/pygen/codegen/models/list_type.py +1 -0
- package/generator/pygen/codegen/models/model_type.py +1 -3
- package/generator/pygen/codegen/models/operation.py +7 -2
- package/generator/pygen/codegen/models/parameter.py +3 -1
- package/generator/pygen/codegen/models/utils.py +14 -0
- package/generator/pygen/codegen/serializers/__init__.py +6 -6
- package/generator/pygen/codegen/serializers/builder_serializer.py +25 -14
- package/generator/pygen/codegen/serializers/client_serializer.py +13 -6
- package/generator/pygen/codegen/serializers/model_serializer.py +4 -1
- package/generator/pygen/codegen/serializers/types_serializer.py +4 -9
- package/generator/pygen/preprocess/__init__.py +68 -17
- package/generator/pygen/utils.py +15 -0
- package/generator/pygen.egg-info/PKG-INFO +1 -1
- package/generator/pygen.egg-info/requires.txt +1 -1
- package/generator/setup.py +1 -1
- package/package.json +33 -33
|
@@ -32,7 +32,7 @@ from ..models import (
|
|
|
32
32
|
ParameterListType,
|
|
33
33
|
ByteArraySchema,
|
|
34
34
|
)
|
|
35
|
-
from ..models.utils import NamespaceType
|
|
35
|
+
from ..models.utils import NamespaceType, escape_sphinx_field_name
|
|
36
36
|
from .parameter_serializer import ParameterSerializer, PopKwargType, check_body_optional
|
|
37
37
|
from ..models.parameter_list import ParameterType
|
|
38
38
|
from . import utils
|
|
@@ -311,16 +311,15 @@ class _BuilderBaseSerializer(Generic[BuilderType]):
|
|
|
311
311
|
or param.method_location == ParameterMethodLocation.KWARG
|
|
312
312
|
):
|
|
313
313
|
continue
|
|
314
|
+
escaped_name = escape_sphinx_field_name(param.client_name)
|
|
314
315
|
description_list.extend(
|
|
315
|
-
f":{param.description_keyword} {
|
|
316
|
-
"\n"
|
|
317
|
-
)
|
|
316
|
+
f":{param.description_keyword} {escaped_name}: {param.description}".replace("\n", "\n ").split("\n")
|
|
318
317
|
)
|
|
319
318
|
docstring_type = param.docstring_type(
|
|
320
319
|
async_mode=self.async_mode,
|
|
321
320
|
serialize_namespace=self.serialize_namespace,
|
|
322
321
|
)
|
|
323
|
-
description_list.append(f":{param.docstring_type_keyword} {
|
|
322
|
+
description_list.append(f":{param.docstring_type_keyword} {escaped_name}: {docstring_type}")
|
|
324
323
|
return description_list
|
|
325
324
|
|
|
326
325
|
def param_description_and_response_docstring(self, builder: BuilderType) -> list[str]:
|
|
@@ -715,7 +714,9 @@ class _OperationSerializer(_BuilderBaseSerializer[OperationType]):
|
|
|
715
714
|
send_xml = builder.parameters.body_parameter.type.is_xml
|
|
716
715
|
xml_serialization_ctxt = body_param.type.xml_serialization_ctxt if send_xml else None
|
|
717
716
|
ser_ctxt_name = "serialization_ctxt"
|
|
718
|
-
if xml_serialization_ctxt and
|
|
717
|
+
if xml_serialization_ctxt and (
|
|
718
|
+
self.code_model.options["models-mode"] or self.code_model.generate_typeddict_only
|
|
719
|
+
):
|
|
719
720
|
retval.append(f'{ser_ctxt_name} = {{"xml": {{{xml_serialization_ctxt}}}}}')
|
|
720
721
|
if self.code_model.options["models-mode"] == "msrest":
|
|
721
722
|
is_xml_cmd = _xml_config(send_xml, builder.parameters.body_parameter.content_types)
|
|
@@ -725,7 +726,7 @@ class _OperationSerializer(_BuilderBaseSerializer[OperationType]):
|
|
|
725
726
|
f"_{body_kwarg_name} = self._serialize.body({body_param.client_name}, "
|
|
726
727
|
f"'{serialization_type}'{is_xml_cmd}{serialization_ctxt_cmd})"
|
|
727
728
|
)
|
|
728
|
-
elif self.code_model.
|
|
729
|
+
elif self.code_model.generate_typeddict_only:
|
|
729
730
|
# TypedDict-only models are plain dicts — no serialization needed
|
|
730
731
|
create_body_call = f"_{body_kwarg_name} = {body_param.client_name}"
|
|
731
732
|
elif self.code_model.options["models-mode"] == "dpg":
|
|
@@ -844,7 +845,9 @@ class _OperationSerializer(_BuilderBaseSerializer[OperationType]):
|
|
|
844
845
|
overload.request_builder.parameters.body_parameter.client_name for overload in builder.overloads
|
|
845
846
|
]
|
|
846
847
|
all_dpg_model_overloads = False
|
|
847
|
-
if
|
|
848
|
+
if (
|
|
849
|
+
self.code_model.options["models-mode"] == "dpg" or self.code_model.generate_typeddict_only
|
|
850
|
+
) and builder.overloads:
|
|
848
851
|
all_dpg_model_overloads = all(
|
|
849
852
|
_is_dpg_or_typeddict_body(o.parameters.body_parameter) for o in builder.overloads
|
|
850
853
|
)
|
|
@@ -1131,8 +1134,8 @@ class _OperationSerializer(_BuilderBaseSerializer[OperationType]):
|
|
|
1131
1134
|
retval.extend([f" {l}" for l in response_read])
|
|
1132
1135
|
retval.append(" map_error(status_code=response.status_code, response=response, error_map=error_map)")
|
|
1133
1136
|
error_model = ""
|
|
1134
|
-
if ( # pylint: disable=too-many-nested-blocks
|
|
1135
|
-
|
|
1137
|
+
if builder.non_default_errors and ( # pylint: disable=too-many-nested-blocks
|
|
1138
|
+
self.code_model.options["models-mode"] or self.code_model.generate_typeddict_only
|
|
1136
1139
|
):
|
|
1137
1140
|
error_model = ", model=error"
|
|
1138
1141
|
condition = "if"
|
|
@@ -1214,7 +1217,9 @@ class _OperationSerializer(_BuilderBaseSerializer[OperationType]):
|
|
|
1214
1217
|
condition = "elif"
|
|
1215
1218
|
# default error handling
|
|
1216
1219
|
default_error_deserialization = builder.default_error_deserialization(self.serialize_namespace)
|
|
1217
|
-
if default_error_deserialization and
|
|
1220
|
+
if default_error_deserialization and (
|
|
1221
|
+
self.code_model.options["models-mode"] or self.code_model.generate_typeddict_only
|
|
1222
|
+
):
|
|
1218
1223
|
error_model = ", model=error"
|
|
1219
1224
|
indent = " " if builder.non_default_errors else " "
|
|
1220
1225
|
if builder.non_default_errors:
|
|
@@ -1293,7 +1298,11 @@ class _OperationSerializer(_BuilderBaseSerializer[OperationType]):
|
|
|
1293
1298
|
else:
|
|
1294
1299
|
retval.extend(self.response_headers_and_deserialization(builder, builder.responses[0]))
|
|
1295
1300
|
retval.append("")
|
|
1296
|
-
if
|
|
1301
|
+
if (
|
|
1302
|
+
builder.has_optional_return_type
|
|
1303
|
+
or self.code_model.options["models-mode"]
|
|
1304
|
+
or self.code_model.generate_typeddict_only
|
|
1305
|
+
):
|
|
1297
1306
|
deserialized = "deserialized"
|
|
1298
1307
|
else:
|
|
1299
1308
|
deserialized = f"cast({builder.response_type_annotation(async_mode=self.async_mode)}, deserialized)"
|
|
@@ -1329,7 +1338,9 @@ class _OperationSerializer(_BuilderBaseSerializer[OperationType]):
|
|
|
1329
1338
|
|
|
1330
1339
|
def error_map(self, builder: OperationType) -> list[str]:
|
|
1331
1340
|
retval = ["error_map: MutableMapping = {"]
|
|
1332
|
-
if builder.non_default_errors and
|
|
1341
|
+
if builder.non_default_errors and (
|
|
1342
|
+
self.code_model.options["models-mode"] or self.code_model.generate_typeddict_only
|
|
1343
|
+
):
|
|
1333
1344
|
# TODO: we should decide whether to add the build-in error map when there is a customized default error type
|
|
1334
1345
|
if self._need_specific_error_map(401, builder):
|
|
1335
1346
|
retval.append(" 401: ClientAuthenticationError,")
|
|
@@ -1693,7 +1704,7 @@ class _LROOperationSerializer(_OperationSerializer[LROOperationType]):
|
|
|
1693
1704
|
if builder.lro_response.headers:
|
|
1694
1705
|
retval.append(" response_headers = {}")
|
|
1695
1706
|
if (
|
|
1696
|
-
not self.code_model.options["models-mode"]
|
|
1707
|
+
(not self.code_model.options["models-mode"] and not self.code_model.generate_typeddict_only)
|
|
1697
1708
|
or self.code_model.options["models-mode"] == "dpg"
|
|
1698
1709
|
or builder.lro_response.headers
|
|
1699
1710
|
):
|
|
@@ -8,6 +8,7 @@ from typing import cast
|
|
|
8
8
|
from . import utils
|
|
9
9
|
from ..models import Client, ParameterMethodLocation, Parameter, ParameterLocation
|
|
10
10
|
from .parameter_serializer import ParameterSerializer, PopKwargType
|
|
11
|
+
from ..models.utils import escape_sphinx_field_name
|
|
11
12
|
from ...utils import build_policies
|
|
12
13
|
|
|
13
14
|
|
|
@@ -61,13 +62,16 @@ class ClientSerializer:
|
|
|
61
62
|
retval: list[str] = []
|
|
62
63
|
operations_folder = ".aio.operations." if async_mode else ".operations."
|
|
63
64
|
for og in [og for og in self.client.operation_groups if not og.is_mixin]:
|
|
64
|
-
retval.append(f":ivar {og.property_name}: {og.class_name} operations")
|
|
65
|
+
retval.append(f":ivar {escape_sphinx_field_name(og.property_name)}: {og.class_name} operations")
|
|
65
66
|
property_type = f"{self.client.code_model.namespace}{operations_folder}{og.class_name}"
|
|
66
|
-
retval.append(f":vartype {og.property_name}: {property_type}")
|
|
67
|
+
retval.append(f":vartype {escape_sphinx_field_name(og.property_name)}: {property_type}")
|
|
67
68
|
for param in self.client.parameters.method:
|
|
68
|
-
retval.append(f":{param.description_keyword} {param.client_name}: {param.description}")
|
|
69
69
|
retval.append(
|
|
70
|
-
f":{param.
|
|
70
|
+
f":{param.description_keyword} {escape_sphinx_field_name(param.client_name)}: {param.description}"
|
|
71
|
+
)
|
|
72
|
+
retval.append(
|
|
73
|
+
f":{param.docstring_type_keyword} {escape_sphinx_field_name(param.client_name)}: "
|
|
74
|
+
f"{param.docstring_type(async_mode=async_mode)}"
|
|
71
75
|
)
|
|
72
76
|
if self.client.has_public_lro_operations:
|
|
73
77
|
retval.append(
|
|
@@ -315,7 +319,10 @@ class ConfigSerializer:
|
|
|
315
319
|
def property_descriptions(self, async_mode: bool) -> list[str]:
|
|
316
320
|
retval: list[str] = []
|
|
317
321
|
for p in self.client.config.parameters.method:
|
|
318
|
-
retval.append(f":{p.description_keyword} {p.client_name}: {p.description}")
|
|
319
|
-
retval.append(
|
|
322
|
+
retval.append(f":{p.description_keyword} {escape_sphinx_field_name(p.client_name)}: {p.description}")
|
|
323
|
+
retval.append(
|
|
324
|
+
f":{p.docstring_type_keyword} {escape_sphinx_field_name(p.client_name)}: "
|
|
325
|
+
f"{p.docstring_type(async_mode=async_mode)}"
|
|
326
|
+
)
|
|
320
327
|
retval.append('"""')
|
|
321
328
|
return retval
|
|
@@ -23,7 +23,7 @@ from ..models.primitive_types import (
|
|
|
23
23
|
)
|
|
24
24
|
from .import_serializer import FileImportSerializer
|
|
25
25
|
from .base_serializer import BaseSerializer
|
|
26
|
-
from ..models.utils import NamespaceType, add_to_pylint_disable
|
|
26
|
+
from ..models.utils import NamespaceType, add_to_pylint_disable, escape_sphinx_field_name
|
|
27
27
|
|
|
28
28
|
|
|
29
29
|
def _get_xml_deserializer_name(prop: Property) -> Optional[str]: # pylint: disable=too-many-return-statements
|
|
@@ -80,6 +80,9 @@ def _documentation_string(
|
|
|
80
80
|
doc_name = (
|
|
81
81
|
prop.wire_name if kwargs.get("serialize_namespace_type") == NamespaceType.TYPES_FILE else prop.client_name
|
|
82
82
|
)
|
|
83
|
+
# Escape names that Sphinx would otherwise misinterpret in the info field target
|
|
84
|
+
# (e.g. a wire name with a leading "@" such as "@search.facets").
|
|
85
|
+
doc_name = escape_sphinx_field_name(doc_name)
|
|
83
86
|
sphinx_prefix = f":{description_keyword} {doc_name}:"
|
|
84
87
|
description = prop.description(is_operation_file=False).replace("\\", "\\\\")
|
|
85
88
|
retval.append(f"{sphinx_prefix} {description}" if description else sphinx_prefix)
|
|
@@ -103,21 +103,16 @@ class TypesSerializer(BaseSerializer):
|
|
|
103
103
|
|
|
104
104
|
TypedDicts (and discriminated-base union aliases) in ``types.py`` describe request-body
|
|
105
105
|
(*input*) shapes. Output-only models already render as classes in ``models/`` and are
|
|
106
|
-
referenced via ``_models.*``, so a
|
|
107
|
-
dead code.
|
|
108
|
-
|
|
109
|
-
* a typeddict copy (``base == "typeddict"``) — these only exist as input body overloads
|
|
110
|
-
(their ``usage`` may carry ``Spread``/``Json`` rather than ``Input``), or
|
|
111
|
-
* ``is_typed_dict_only`` — includes every model in full ``typeddict`` mode (responses too),
|
|
112
|
-
and input-only anonymous bodies, or
|
|
113
|
-
* used as input (``is_usage_input``) — e.g. a model shared between request and response.
|
|
106
|
+
referenced via ``_models.*``, so a model that no operation references through ``types.py``
|
|
107
|
+
would be dead code. TypedDict copies and models in full TypedDict mode volunteer through
|
|
108
|
+
``is_used_in_operations_via_types``.
|
|
114
109
|
|
|
115
110
|
The full set rendered in types.py is the transitive closure of these seeds over base
|
|
116
111
|
classes, discriminated subtypes and property types (see :attr:`_types_file_model_names`), so
|
|
117
112
|
an output-only model that *is* referenced by an input model (e.g. ARM ``SystemData`` on
|
|
118
113
|
``Resource``) is still rendered.
|
|
119
114
|
"""
|
|
120
|
-
return m.
|
|
115
|
+
return m.is_used_in_operations_via_types
|
|
121
116
|
|
|
122
117
|
@staticmethod
|
|
123
118
|
def _iter_referenced_models(base_type: Any):
|
|
@@ -81,8 +81,17 @@ def add_overload(yaml_data: dict[str, Any], body_type: dict[str, Any], for_flatt
|
|
|
81
81
|
return overload
|
|
82
82
|
|
|
83
83
|
|
|
84
|
-
def add_overloads_for_body_param(yaml_data: dict[str, Any]) -> None:
|
|
85
|
-
"""If we added a body parameter type, add overloads for that type
|
|
84
|
+
def add_overloads_for_body_param(yaml_data: dict[str, Any], skip_single_body_json: bool = False) -> None:
|
|
85
|
+
"""If we added a body parameter type, add overloads for that type.
|
|
86
|
+
|
|
87
|
+
``skip_single_body_json`` is the authoritative signal, computed by
|
|
88
|
+
``add_body_param_type``, for whether a TypedDict-style overload was inserted
|
|
89
|
+
to replace the single-body raw-JSON overload on the spread (``base: json``)
|
|
90
|
+
path. It is True for both models-mode: dpg (generate-typeddict on) and
|
|
91
|
+
the TypedDict-only mode (models-mode: none with generate-typeddict on), and
|
|
92
|
+
False when TypedDict generation is disabled (in which case the single-body
|
|
93
|
+
raw-JSON overload is kept, matching pre-TypedDict behavior).
|
|
94
|
+
"""
|
|
86
95
|
body_parameter = yaml_data["bodyParameter"]
|
|
87
96
|
if not (
|
|
88
97
|
body_parameter["type"]["type"] == "combined"
|
|
@@ -94,8 +103,8 @@ def add_overloads_for_body_param(yaml_data: dict[str, Any]) -> None:
|
|
|
94
103
|
continue
|
|
95
104
|
if body_type.get("type") == "model" and body_type.get("base") == "json":
|
|
96
105
|
yaml_data["overloads"].append(add_overload(yaml_data, body_type, for_flatten_params=True))
|
|
97
|
-
|
|
98
|
-
|
|
106
|
+
if skip_single_body_json:
|
|
107
|
+
continue
|
|
99
108
|
yaml_data["overloads"].append(add_overload(yaml_data, body_type))
|
|
100
109
|
content_type_param = next(p for p in yaml_data["parameters"] if p["wireName"].lower() == "content-type")
|
|
101
110
|
content_type_param["inOverload"] = False
|
|
@@ -306,6 +315,10 @@ class PreProcessPlugin(YamlUpdatePlugin):
|
|
|
306
315
|
def is_tsp(self) -> bool:
|
|
307
316
|
return self.options.get("tsp_file", False)
|
|
308
317
|
|
|
318
|
+
@property
|
|
319
|
+
def generate_typeddict(self) -> bool:
|
|
320
|
+
return self.options.get("generate-typeddict", True)
|
|
321
|
+
|
|
309
322
|
@staticmethod
|
|
310
323
|
def _find_existing_typeddict(
|
|
311
324
|
code_model: dict[str, Any],
|
|
@@ -394,11 +407,39 @@ class PreProcessPlugin(YamlUpdatePlugin):
|
|
|
394
407
|
if not existing_td:
|
|
395
408
|
code_model["types"].append(td_elem)
|
|
396
409
|
|
|
410
|
+
@staticmethod
|
|
411
|
+
def _insert_json_overload(
|
|
412
|
+
body_parameter: dict[str, Any],
|
|
413
|
+
origin_type: str,
|
|
414
|
+
) -> None:
|
|
415
|
+
"""Insert a raw-JSON (any-object) type into the body parameter's combined types.
|
|
416
|
+
|
|
417
|
+
This restores the pre-TypedDict dict-body overload used when TypedDict
|
|
418
|
+
generation is disabled.
|
|
419
|
+
"""
|
|
420
|
+
if origin_type == "model":
|
|
421
|
+
body_parameter["type"]["types"].insert(1, KNOWN_TYPES["any-object"])
|
|
422
|
+
else:
|
|
423
|
+
# dict or list: copy the original container type and swap its element
|
|
424
|
+
# type for the raw-JSON any-object.
|
|
425
|
+
any_obj_list_or_dict = copy.deepcopy(body_parameter["type"]["types"][0])
|
|
426
|
+
any_obj_list_or_dict["elementType"] = KNOWN_TYPES["any-object"]
|
|
427
|
+
body_parameter["type"]["types"].insert(1, any_obj_list_or_dict)
|
|
428
|
+
|
|
397
429
|
def add_body_param_type(
|
|
398
430
|
self,
|
|
399
431
|
code_model: dict[str, Any],
|
|
400
432
|
body_parameter: dict[str, Any],
|
|
401
|
-
):
|
|
433
|
+
) -> bool:
|
|
434
|
+
"""Build the combined body-parameter type and its overload variants.
|
|
435
|
+
|
|
436
|
+
Returns whether a TypedDict-style overload was inserted in place of the
|
|
437
|
+
single-body raw-JSON overload on the spread (``base: json``) path. The
|
|
438
|
+
caller passes this to ``add_overloads_for_body_param`` as
|
|
439
|
+
``skip_single_body_json``. It is False when TypedDict generation is
|
|
440
|
+
disabled, so the pre-TypedDict single-body raw-JSON overload is kept.
|
|
441
|
+
"""
|
|
442
|
+
skip_single_body_json = False
|
|
402
443
|
# For a binary `bytes` body (e.g. content type application/octet-stream or a custom
|
|
403
444
|
# binary media type), add an IO overload alongside the `bytes` one. This keeps backward
|
|
404
445
|
# compatibility for services migrating from swagger, whose binary bodies were typed as IO.
|
|
@@ -408,7 +449,7 @@ class PreProcessPlugin(YamlUpdatePlugin):
|
|
|
408
449
|
body_parameter
|
|
409
450
|
and body_parameter["type"]["type"] == "bytes"
|
|
410
451
|
and is_binary_body
|
|
411
|
-
and self.options
|
|
452
|
+
and not self.options.generate_typeddict_only
|
|
412
453
|
and not any(t for t in ["flattened", "groupedBy"] if body_parameter.get(t))
|
|
413
454
|
):
|
|
414
455
|
body_parameter["type"] = {
|
|
@@ -416,7 +457,7 @@ class PreProcessPlugin(YamlUpdatePlugin):
|
|
|
416
457
|
"types": [body_parameter["type"], KNOWN_TYPES["binary"]],
|
|
417
458
|
}
|
|
418
459
|
code_model["types"].append(body_parameter["type"])
|
|
419
|
-
return
|
|
460
|
+
return skip_single_body_json
|
|
420
461
|
|
|
421
462
|
# only add overload for special content type
|
|
422
463
|
if ( # pylint: disable=too-many-boolean-expressions
|
|
@@ -432,7 +473,7 @@ class PreProcessPlugin(YamlUpdatePlugin):
|
|
|
432
473
|
)
|
|
433
474
|
is_dpg_model = model_type.get("base") == "dpg"
|
|
434
475
|
is_json_model = model_type.get("base") == "json"
|
|
435
|
-
is_typeddict_only = self.options
|
|
476
|
+
is_typeddict_only = self.options.generate_typeddict_only
|
|
436
477
|
|
|
437
478
|
body_parameter["type"] = {
|
|
438
479
|
"type": "combined",
|
|
@@ -442,11 +483,16 @@ class PreProcessPlugin(YamlUpdatePlugin):
|
|
|
442
483
|
if not (self.is_tsp and has_multi_part_content_type(body_parameter)) and not is_typeddict_only:
|
|
443
484
|
body_parameter["type"]["types"].append(KNOWN_TYPES["binary"])
|
|
444
485
|
|
|
445
|
-
# Add
|
|
486
|
+
# Add the dict-body overload for non-spread dpg models: a TypedDict when
|
|
487
|
+
# enabled, otherwise the raw-JSON overload (pre-TypedDict behavior).
|
|
446
488
|
if self.options["models-mode"] == "dpg" and is_dpg_model:
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
489
|
+
if self.generate_typeddict:
|
|
490
|
+
cross_lang_id = model_type.get("crossLanguageDefinitionId")
|
|
491
|
+
existing_td = self._find_existing_typeddict(code_model, cross_lang_id, model_type.get("name"))
|
|
492
|
+
self._insert_typeddict_overload(code_model, body_parameter, model_type, origin_type, existing_td)
|
|
493
|
+
skip_single_body_json = True
|
|
494
|
+
else:
|
|
495
|
+
self._insert_json_overload(body_parameter, origin_type)
|
|
450
496
|
|
|
451
497
|
# For spread bodies (json base), add a typeddict overload that references
|
|
452
498
|
# the original model. This replaces the JSON single-body overload.
|
|
@@ -456,17 +502,20 @@ class PreProcessPlugin(YamlUpdatePlugin):
|
|
|
456
502
|
|
|
457
503
|
if is_typeddict_only and original:
|
|
458
504
|
# In typeddict-only mode, the original dpg model already renders
|
|
459
|
-
# as a TypedDict — reference it directly, no copy needed.
|
|
505
|
+
# as a TypedDict — reference it directly, no copy needed. It also
|
|
506
|
+
# replaces the single-body JSON overload.
|
|
507
|
+
skip_single_body_json = True
|
|
460
508
|
if origin_type == "model":
|
|
461
509
|
body_parameter["type"]["types"].insert(1, original)
|
|
462
510
|
else:
|
|
463
511
|
td_list_or_dict = copy.deepcopy(body_parameter["type"]["types"][0])
|
|
464
512
|
td_list_or_dict["elementType"] = original
|
|
465
513
|
body_parameter["type"]["types"].insert(1, td_list_or_dict)
|
|
466
|
-
|
|
514
|
+
elif self.generate_typeddict:
|
|
467
515
|
source = original or model_type
|
|
468
516
|
existing_td = self._find_existing_typeddict(code_model, cross_lang_id, source.get("name"))
|
|
469
517
|
self._insert_typeddict_overload(code_model, body_parameter, source, origin_type, existing_td)
|
|
518
|
+
skip_single_body_json = True
|
|
470
519
|
|
|
471
520
|
if len(body_parameter["type"]["types"]) == 1:
|
|
472
521
|
# Only one body variant remains (e.g. typeddict-only mode where the
|
|
@@ -474,10 +523,12 @@ class PreProcessPlugin(YamlUpdatePlugin):
|
|
|
474
523
|
# wrapper back to the single type so we don't emit a lone
|
|
475
524
|
# ``@overload`` (mypy rejects a single overload definition).
|
|
476
525
|
body_parameter["type"] = body_parameter["type"]["types"][0]
|
|
477
|
-
return
|
|
526
|
+
return skip_single_body_json
|
|
478
527
|
|
|
479
528
|
code_model["types"].append(body_parameter["type"])
|
|
480
529
|
|
|
530
|
+
return skip_single_body_json
|
|
531
|
+
|
|
481
532
|
def pad_reserved_words(self, name: str, pad_type: PadType, yaml_type: dict[str, Any]) -> str:
|
|
482
533
|
# we want to pad hidden variables as well
|
|
483
534
|
if not name:
|
|
@@ -649,8 +700,8 @@ class PreProcessPlugin(YamlUpdatePlugin):
|
|
|
649
700
|
response["discriminator"] = "operation"
|
|
650
701
|
if body_parameter and not is_overload:
|
|
651
702
|
# if we have a JSON body, we add a binary overload
|
|
652
|
-
self.add_body_param_type(code_model, body_parameter)
|
|
653
|
-
add_overloads_for_body_param(yaml_data)
|
|
703
|
+
skip_single_body_json = self.add_body_param_type(code_model, body_parameter)
|
|
704
|
+
add_overloads_for_body_param(yaml_data, skip_single_body_json=skip_single_body_json)
|
|
654
705
|
|
|
655
706
|
def _update_lro_operation_helper(self, yaml_data: dict[str, Any]) -> None:
|
|
656
707
|
for response in yaml_data.get("responses", []):
|
package/generator/pygen/utils.py
CHANGED
|
@@ -28,6 +28,21 @@ def description_ends_with_code_block(description: str) -> bool:
|
|
|
28
28
|
return all(not line.strip() or line.startswith((" ", "\t")) for line in lines[directives[-1] + 1 :])
|
|
29
29
|
|
|
30
30
|
|
|
31
|
+
def is_typeddict_only(options: Any) -> bool:
|
|
32
|
+
"""Whether generation is TypedDict-only.
|
|
33
|
+
|
|
34
|
+
True for TypeSpec input where no concrete models mode is selected (``models-mode: none``,
|
|
35
|
+
normalized to a falsy value by :class:`OptionsDict`) and ``generate-typeddict`` is enabled.
|
|
36
|
+
Swagger ``models-mode: none`` is left as a plain no-models mode because it never sets
|
|
37
|
+
``tsp_file``.
|
|
38
|
+
"""
|
|
39
|
+
return (
|
|
40
|
+
bool(options.get("tsp_file"))
|
|
41
|
+
and not options.get("models-mode")
|
|
42
|
+
and bool(options.get("generate-typeddict", True))
|
|
43
|
+
)
|
|
44
|
+
|
|
45
|
+
|
|
31
46
|
def update_enum_value(name: str, value: Any, description: str, enum_type: dict[str, Any]) -> dict[str, Any]:
|
|
32
47
|
return {
|
|
33
48
|
"name": name,
|
|
@@ -16,7 +16,7 @@ Classifier: Programming Language :: Python :: 3.14
|
|
|
16
16
|
Classifier: License :: OSI Approved :: MIT License
|
|
17
17
|
Description-Content-Type: text/markdown
|
|
18
18
|
License-File: LICENSE
|
|
19
|
-
Requires-Dist: black==
|
|
19
|
+
Requires-Dist: black==26.3.1
|
|
20
20
|
Requires-Dist: docutils>=0.20.1
|
|
21
21
|
Requires-Dist: Jinja2==3.1.6
|
|
22
22
|
Requires-Dist: PyYAML==6.0.1
|
package/generator/setup.py
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@typespec/http-client-python",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.36.0",
|
|
4
4
|
"author": "Microsoft Corporation",
|
|
5
5
|
"description": "TypeSpec emitter for Python SDKs",
|
|
6
6
|
"homepage": "https://typespec.io",
|
|
@@ -66,20 +66,20 @@
|
|
|
66
66
|
"emitter"
|
|
67
67
|
],
|
|
68
68
|
"peerDependencies": {
|
|
69
|
-
"@azure-tools/typespec-autorest": ">=0.
|
|
70
|
-
"@azure-tools/typespec-azure-core": ">=0.
|
|
71
|
-
"@azure-tools/typespec-azure-resource-manager": ">=0.
|
|
72
|
-
"@azure-tools/typespec-azure-rulesets": ">=0.
|
|
73
|
-
"@azure-tools/typespec-client-generator-core": ">=0.
|
|
74
|
-
"@typespec/compiler": "^1.
|
|
75
|
-
"@typespec/http": "^1.
|
|
76
|
-
"@typespec/openapi": "^1.
|
|
77
|
-
"@typespec/rest": ">=0.
|
|
78
|
-
"@typespec/versioning": ">=0.
|
|
79
|
-
"@typespec/events": ">=0.
|
|
80
|
-
"@typespec/sse": ">=0.
|
|
81
|
-
"@typespec/streams": ">=0.
|
|
82
|
-
"@typespec/xml": ">=0.
|
|
69
|
+
"@azure-tools/typespec-autorest": ">=0.71.0 <1.0.0",
|
|
70
|
+
"@azure-tools/typespec-azure-core": ">=0.71.0 <1.0.0",
|
|
71
|
+
"@azure-tools/typespec-azure-resource-manager": ">=0.71.0 <1.0.0",
|
|
72
|
+
"@azure-tools/typespec-azure-rulesets": ">=0.71.0 <1.0.0",
|
|
73
|
+
"@azure-tools/typespec-client-generator-core": ">=0.71.0 <1.0.0",
|
|
74
|
+
"@typespec/compiler": "^1.15.0",
|
|
75
|
+
"@typespec/http": "^1.15.0",
|
|
76
|
+
"@typespec/openapi": "^1.15.0",
|
|
77
|
+
"@typespec/rest": ">=0.85.0 <1.0.0",
|
|
78
|
+
"@typespec/versioning": ">=0.85.0 <1.0.0",
|
|
79
|
+
"@typespec/events": ">=0.85.0 <1.0.0",
|
|
80
|
+
"@typespec/sse": ">=0.85.0 <1.0.0",
|
|
81
|
+
"@typespec/streams": ">=0.85.0 <1.0.0",
|
|
82
|
+
"@typespec/xml": ">=0.85.0 <1.0.0"
|
|
83
83
|
},
|
|
84
84
|
"playgroundConfig": {
|
|
85
85
|
"assets": [
|
|
@@ -104,24 +104,24 @@
|
|
|
104
104
|
"tsx": "^4.21.0"
|
|
105
105
|
},
|
|
106
106
|
"devDependencies": {
|
|
107
|
-
"@azure-tools/typespec-autorest": "~0.
|
|
108
|
-
"@azure-tools/typespec-azure-core": "~0.
|
|
109
|
-
"@azure-tools/typespec-azure-resource-manager": "~0.
|
|
110
|
-
"@azure-tools/typespec-azure-rulesets": "~0.
|
|
111
|
-
"@azure-tools/typespec-client-generator-core": "~0.
|
|
112
|
-
"@azure-tools/azure-http-specs": "0.1.0-alpha.
|
|
113
|
-
"@typespec/compiler": "^1.
|
|
114
|
-
"@typespec/http": "^1.
|
|
115
|
-
"@typespec/openapi": "^1.
|
|
116
|
-
"@typespec/rest": "~0.
|
|
117
|
-
"@typespec/versioning": "~0.
|
|
118
|
-
"@typespec/events": "~0.
|
|
119
|
-
"@typespec/spector": "0.1.0-alpha.
|
|
120
|
-
"@typespec/spec-api": "0.1.0-alpha.
|
|
121
|
-
"@typespec/sse": "~0.
|
|
122
|
-
"@typespec/streams": "~0.
|
|
123
|
-
"@typespec/xml": "~0.
|
|
124
|
-
"@typespec/http-specs": "0.1.0-alpha.
|
|
107
|
+
"@azure-tools/typespec-autorest": "~0.71.0",
|
|
108
|
+
"@azure-tools/typespec-azure-core": "~0.71.0",
|
|
109
|
+
"@azure-tools/typespec-azure-resource-manager": "~0.71.0",
|
|
110
|
+
"@azure-tools/typespec-azure-rulesets": "~0.71.0",
|
|
111
|
+
"@azure-tools/typespec-client-generator-core": "~0.71.0",
|
|
112
|
+
"@azure-tools/azure-http-specs": "0.1.0-alpha.44",
|
|
113
|
+
"@typespec/compiler": "^1.15.0",
|
|
114
|
+
"@typespec/http": "^1.15.0",
|
|
115
|
+
"@typespec/openapi": "^1.15.0",
|
|
116
|
+
"@typespec/rest": "~0.85.0",
|
|
117
|
+
"@typespec/versioning": "~0.85.0",
|
|
118
|
+
"@typespec/events": "~0.85.0",
|
|
119
|
+
"@typespec/spector": "0.1.0-alpha.28",
|
|
120
|
+
"@typespec/spec-api": "0.1.0-alpha.16",
|
|
121
|
+
"@typespec/sse": "~0.85.0",
|
|
122
|
+
"@typespec/streams": "~0.85.0",
|
|
123
|
+
"@typespec/xml": "~0.85.0",
|
|
124
|
+
"@typespec/http-specs": "0.1.0-alpha.41",
|
|
125
125
|
"@types/js-yaml": "~4.0.5",
|
|
126
126
|
"@types/node": "~25.0.2",
|
|
127
127
|
"@types/semver": "7.5.8",
|