@typespec/http-client-python 0.32.0 → 0.34.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/dist/emitter/emitter.js +10 -2
- package/dist/emitter/emitter.js.map +1 -1
- package/dist/emitter/lib.d.ts +6 -0
- package/dist/emitter/lib.d.ts.map +1 -1
- package/dist/emitter/lib.js +29 -0
- package/dist/emitter/lib.js.map +1 -1
- package/dist/emitter/types.d.ts.map +1 -1
- package/dist/emitter/types.js +2 -0
- package/dist/emitter/types.js.map +1 -1
- package/emitter/src/emitter.ts +9 -1
- package/emitter/src/lib.ts +36 -0
- package/emitter/src/types.ts +2 -0
- package/emitter/temp/tsconfig.tsbuildinfo +1 -1
- package/eng/scripts/ci/regenerate-common.ts +40 -15
- package/eng/scripts/setup/__pycache__/package_manager.cpython-311.pyc +0 -0
- package/eng/scripts/setup/__pycache__/venvtools.cpython-311.pyc +0 -0
- package/generator/build/lib/pygen/__init__.py +4 -3
- package/generator/build/lib/pygen/codegen/models/__init__.py +4 -2
- package/generator/build/lib/pygen/codegen/models/code_model.py +27 -5
- package/generator/build/lib/pygen/codegen/models/combined_type.py +3 -3
- package/generator/build/lib/pygen/codegen/models/enum_type.py +75 -25
- package/generator/build/lib/pygen/codegen/models/list_type.py +10 -4
- package/generator/build/lib/pygen/codegen/models/model_type.py +145 -4
- package/generator/build/lib/pygen/codegen/models/operation.py +1 -0
- package/generator/build/lib/pygen/codegen/models/paging_operation.py +5 -1
- package/generator/build/lib/pygen/codegen/models/parameter.py +2 -2
- package/generator/build/lib/pygen/codegen/models/primitive_types.py +30 -6
- package/generator/build/lib/pygen/codegen/models/property.py +21 -8
- package/generator/build/lib/pygen/codegen/models/response.py +16 -3
- package/generator/build/lib/pygen/codegen/models/utils.py +12 -10
- package/generator/build/lib/pygen/codegen/serializers/__init__.py +46 -11
- package/generator/build/lib/pygen/codegen/serializers/builder_serializer.py +48 -15
- package/generator/build/lib/pygen/codegen/serializers/general_serializer.py +32 -2
- package/generator/build/lib/pygen/codegen/serializers/model_init_serializer.py +1 -1
- package/generator/build/lib/pygen/codegen/serializers/model_serializer.py +127 -3
- package/generator/build/lib/pygen/codegen/serializers/types_serializer.py +263 -12
- package/generator/build/lib/pygen/codegen/serializers/unions_serializer.py +44 -0
- package/generator/build/lib/pygen/codegen/templates/model_container.py.jinja2 +1 -1
- package/generator/build/lib/pygen/codegen/templates/packaging_templates/pyproject.toml.jinja2 +30 -2
- package/generator/build/lib/pygen/codegen/templates/types.py.jinja2 +51 -2
- package/generator/build/lib/pygen/codegen/templates/unions.py.jinja2 +9 -0
- package/generator/build/lib/pygen/preprocess/__init__.py +104 -13
- package/generator/build/lib/pygen/utils.py +16 -0
- package/generator/dist/pygen-0.1.0-py3-none-any.whl +0 -0
- package/generator/pygen/__init__.py +4 -3
- package/generator/pygen/codegen/models/__init__.py +4 -2
- package/generator/pygen/codegen/models/code_model.py +27 -5
- package/generator/pygen/codegen/models/combined_type.py +3 -3
- package/generator/pygen/codegen/models/enum_type.py +75 -25
- package/generator/pygen/codegen/models/list_type.py +10 -4
- package/generator/pygen/codegen/models/model_type.py +145 -4
- package/generator/pygen/codegen/models/operation.py +1 -0
- package/generator/pygen/codegen/models/paging_operation.py +5 -1
- package/generator/pygen/codegen/models/parameter.py +2 -2
- package/generator/pygen/codegen/models/primitive_types.py +30 -6
- package/generator/pygen/codegen/models/property.py +21 -8
- package/generator/pygen/codegen/models/response.py +16 -3
- package/generator/pygen/codegen/models/utils.py +12 -10
- package/generator/pygen/codegen/serializers/__init__.py +46 -11
- package/generator/pygen/codegen/serializers/builder_serializer.py +48 -15
- package/generator/pygen/codegen/serializers/general_serializer.py +32 -2
- package/generator/pygen/codegen/serializers/model_init_serializer.py +1 -1
- package/generator/pygen/codegen/serializers/model_serializer.py +127 -3
- package/generator/pygen/codegen/serializers/types_serializer.py +263 -12
- package/generator/pygen/codegen/serializers/unions_serializer.py +44 -0
- package/generator/pygen/codegen/templates/model_container.py.jinja2 +1 -1
- package/generator/pygen/codegen/templates/packaging_templates/pyproject.toml.jinja2 +30 -2
- package/generator/pygen/codegen/templates/types.py.jinja2 +51 -2
- package/generator/pygen/codegen/templates/unions.py.jinja2 +9 -0
- package/generator/pygen/preprocess/__init__.py +104 -13
- package/generator/pygen/utils.py +16 -0
- package/generator/pygen.egg-info/SOURCES.txt +2 -0
- package/package.json +5 -5
|
@@ -172,9 +172,15 @@ export const AZURE_EMITTER_OPTIONS: Record<
|
|
|
172
172
|
"package-name": "client-structure-twooperationgroup",
|
|
173
173
|
namespace: "client.structure.twooperationgroup",
|
|
174
174
|
},
|
|
175
|
-
"client/naming":
|
|
176
|
-
|
|
177
|
-
|
|
175
|
+
"client/naming": [
|
|
176
|
+
{
|
|
177
|
+
namespace: "client.naming.main",
|
|
178
|
+
},
|
|
179
|
+
{
|
|
180
|
+
"package-name": "client-naming-typeddict",
|
|
181
|
+
namespace: "client.naming.typeddict",
|
|
182
|
+
},
|
|
183
|
+
],
|
|
178
184
|
"client/overload": {
|
|
179
185
|
namespace: "client.overload",
|
|
180
186
|
},
|
|
@@ -271,14 +277,26 @@ export const EMITTER_OPTIONS: Record<string, Record<string, string> | Record<str
|
|
|
271
277
|
"package-name": "typetest-model-nesteddiscriminator",
|
|
272
278
|
namespace: "typetest.model.nesteddiscriminator",
|
|
273
279
|
},
|
|
274
|
-
"type/model/inheritance/not-discriminated":
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
280
|
+
"type/model/inheritance/not-discriminated": [
|
|
281
|
+
{
|
|
282
|
+
"package-name": "typetest-model-notdiscriminated",
|
|
283
|
+
namespace: "typetest.model.notdiscriminated",
|
|
284
|
+
},
|
|
285
|
+
{
|
|
286
|
+
"package-name": "typetest-model-notdiscriminated-typeddict",
|
|
287
|
+
namespace: "typetest.model.notdiscriminated.typeddict",
|
|
288
|
+
},
|
|
289
|
+
],
|
|
290
|
+
"type/model/inheritance/single-discriminator": [
|
|
291
|
+
{
|
|
292
|
+
"package-name": "typetest-model-singlediscriminator",
|
|
293
|
+
namespace: "typetest.model.singlediscriminator",
|
|
294
|
+
},
|
|
295
|
+
{
|
|
296
|
+
"package-name": "typetest-model-singlediscriminator-typeddict",
|
|
297
|
+
namespace: "typetest.model.singlediscriminator.typeddict",
|
|
298
|
+
},
|
|
299
|
+
],
|
|
282
300
|
"type/model/inheritance/recursive": [
|
|
283
301
|
{
|
|
284
302
|
"package-name": "typetest-model-recursive",
|
|
@@ -292,10 +310,17 @@ export const EMITTER_OPTIONS: Record<string, Record<string, string> | Record<str
|
|
|
292
310
|
"clear-output-folder": "true",
|
|
293
311
|
},
|
|
294
312
|
],
|
|
295
|
-
"type/model/usage":
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
313
|
+
"type/model/usage": [
|
|
314
|
+
{
|
|
315
|
+
"package-name": "typetest-model-usage",
|
|
316
|
+
namespace: "typetest.model.usage",
|
|
317
|
+
},
|
|
318
|
+
{
|
|
319
|
+
"package-name": "typetest-model-usage-typeddictonly",
|
|
320
|
+
namespace: "typetest.model.usage.typeddictonly",
|
|
321
|
+
"models-mode": "typeddict",
|
|
322
|
+
},
|
|
323
|
+
],
|
|
299
324
|
"type/model/visibility": [
|
|
300
325
|
{
|
|
301
326
|
"package-name": "typetest-model-visibility",
|
|
Binary file
|
|
Binary file
|
|
@@ -29,6 +29,7 @@ class OptionsDict(MutableMapping):
|
|
|
29
29
|
"from-typespec": False,
|
|
30
30
|
"generate-sample": False,
|
|
31
31
|
"keep-setup-py": False,
|
|
32
|
+
"keep-pyproject-fields": "",
|
|
32
33
|
"generate-test": False,
|
|
33
34
|
"head-as-boolean": True,
|
|
34
35
|
"keep-version-file": False,
|
|
@@ -167,10 +168,10 @@ class OptionsDict(MutableMapping):
|
|
|
167
168
|
if key == "models-mode" and value == "none":
|
|
168
169
|
value = False # switch to falsy value for easier code writing
|
|
169
170
|
|
|
170
|
-
if key == "models-mode" and value not in ["msrest", "dpg", False]:
|
|
171
|
+
if key == "models-mode" and value not in ["msrest", "dpg", "typeddict", False]:
|
|
171
172
|
raise ValueError(
|
|
172
|
-
"--models-mode can only be 'msrest', 'dpg' or 'none'. "
|
|
173
|
-
"Pass in 'msrest' if you want msrest models, or "
|
|
173
|
+
"--models-mode can only be 'msrest', 'dpg', 'typeddict', or 'none'. "
|
|
174
|
+
"Pass in 'msrest' if you want msrest models, 'typeddict' for TypedDict models, or "
|
|
174
175
|
"'none' if you don't want any."
|
|
175
176
|
)
|
|
176
177
|
if key == "package-mode":
|
|
@@ -9,7 +9,7 @@ from .base import BaseModel
|
|
|
9
9
|
from .base_builder import BaseBuilder, ParameterListType
|
|
10
10
|
from .code_model import CodeModel
|
|
11
11
|
from .client import Client
|
|
12
|
-
from .model_type import ModelType, JSONModelType, DPGModelType, MsrestModelType
|
|
12
|
+
from .model_type import ModelType, JSONModelType, DPGModelType, MsrestModelType, TypedDictModelType
|
|
13
13
|
from .dictionary_type import DictionaryType
|
|
14
14
|
from .list_type import ListType
|
|
15
15
|
from .combined_type import CombinedType
|
|
@@ -167,7 +167,9 @@ def build_type(yaml_data: dict[str, Any], code_model: CodeModel) -> BaseType:
|
|
|
167
167
|
response: Optional[BaseType] = None
|
|
168
168
|
if yaml_data["type"] == "model":
|
|
169
169
|
# need to special case model to avoid recursion
|
|
170
|
-
if yaml_data["base"] == "
|
|
170
|
+
if yaml_data["base"] == "typeddict":
|
|
171
|
+
model_type = TypedDictModelType # type: ignore
|
|
172
|
+
elif yaml_data["base"] == "json" or not code_model.options["models-mode"]:
|
|
171
173
|
model_type = JSONModelType
|
|
172
174
|
elif yaml_data["base"] == "dpg":
|
|
173
175
|
model_type = DPGModelType # type: ignore
|
|
@@ -152,16 +152,28 @@ class CodeModel: # pylint: disable=too-many-public-methods, disable=too-many-in
|
|
|
152
152
|
return result
|
|
153
153
|
return f"{result}{module_name}" if result.endswith(".") else f"{result}.{module_name}"
|
|
154
154
|
|
|
155
|
-
def
|
|
155
|
+
def _get_unique_import_alias(self, serialize_namespace: str, imported_namespace: str, module_name: str) -> str:
|
|
156
156
|
if not self.has_subnamespace:
|
|
157
|
-
return "
|
|
157
|
+
return f"_{module_name}"
|
|
158
158
|
relative_path = self.get_relative_import_path(
|
|
159
159
|
serialize_namespace, self.get_imported_namespace_for_model(imported_namespace)
|
|
160
160
|
)
|
|
161
161
|
dot_num = max(relative_path.count(".") - 1, 0)
|
|
162
|
-
|
|
162
|
+
path_parts = [p for p in relative_path.split(".") if p]
|
|
163
|
+
# For "models", keep existing format: _<path_parts><dot_num> (e.g. _models1, _firstnamespace_models2)
|
|
164
|
+
# For other modules like "types", prefix with module name: _types_<path_parts><dot_num>
|
|
165
|
+
if module_name == "models":
|
|
166
|
+
parts = [""] + (path_parts or [module_name])
|
|
167
|
+
else:
|
|
168
|
+
parts = [f"_{module_name}"] + (path_parts or [])
|
|
163
169
|
return "_".join(parts) + (str(dot_num) if dot_num > 0 else "")
|
|
164
170
|
|
|
171
|
+
def get_unique_models_alias(self, serialize_namespace: str, imported_namespace: str) -> str:
|
|
172
|
+
return self._get_unique_import_alias(serialize_namespace, imported_namespace, "models")
|
|
173
|
+
|
|
174
|
+
def get_unique_types_alias(self, serialize_namespace: str, imported_namespace: str) -> str:
|
|
175
|
+
return self._get_unique_import_alias(serialize_namespace, imported_namespace, "types")
|
|
176
|
+
|
|
165
177
|
@property
|
|
166
178
|
def client_namespace_types(self) -> dict[str, ClientNamespaceType]:
|
|
167
179
|
if not self._client_namespace_types:
|
|
@@ -174,6 +186,14 @@ class CodeModel: # pylint: disable=too-many-public-methods, disable=too-many-in
|
|
|
174
186
|
if model.client_namespace not in self._client_namespace_types:
|
|
175
187
|
self._client_namespace_types[model.client_namespace] = ClientNamespaceType()
|
|
176
188
|
self._client_namespace_types[model.client_namespace].models.append(model)
|
|
189
|
+
# TypedDict copies (base="typeddict") are excluded from model_types to keep
|
|
190
|
+
# them out of _models.py, but they need to be in the namespace model list
|
|
191
|
+
# so the TypesSerializer can render them in types.py.
|
|
192
|
+
for t in self.types_map.values():
|
|
193
|
+
if isinstance(t, ModelType) and t.base == "typeddict" and t.usage != UsageFlags.Default.value:
|
|
194
|
+
if t.client_namespace not in self._client_namespace_types:
|
|
195
|
+
self._client_namespace_types[t.client_namespace] = ClientNamespaceType()
|
|
196
|
+
self._client_namespace_types[t.client_namespace].models.append(t)
|
|
177
197
|
for enum in self.enums:
|
|
178
198
|
if enum.client_namespace not in self._client_namespace_types:
|
|
179
199
|
self._client_namespace_types[enum.client_namespace] = ClientNamespaceType()
|
|
@@ -339,7 +359,9 @@ class CodeModel: # pylint: disable=too-many-public-methods, disable=too-many-in
|
|
|
339
359
|
"""All of the model types in this class"""
|
|
340
360
|
if not self._model_types:
|
|
341
361
|
self._model_types = [
|
|
342
|
-
t
|
|
362
|
+
t
|
|
363
|
+
for t in self.types_map.values()
|
|
364
|
+
if isinstance(t, ModelType) and t.usage != UsageFlags.Default.value and t.base != "typeddict"
|
|
343
365
|
]
|
|
344
366
|
return self._model_types
|
|
345
367
|
|
|
@@ -349,7 +371,7 @@ class CodeModel: # pylint: disable=too-many-public-methods, disable=too-many-in
|
|
|
349
371
|
|
|
350
372
|
@staticmethod
|
|
351
373
|
def get_public_model_types(models: list[ModelType]) -> list[ModelType]:
|
|
352
|
-
return [m for m in models if not m.internal and not m.base == "json"]
|
|
374
|
+
return [m for m in models if not m.internal and not m.base == "json" and not m.is_typed_dict_only]
|
|
353
375
|
|
|
354
376
|
@property
|
|
355
377
|
def public_model_types(self) -> list[ModelType]:
|
|
@@ -66,7 +66,7 @@ class CombinedType(BaseType):
|
|
|
66
66
|
|
|
67
67
|
def type_annotation(self, **kwargs: Any) -> str:
|
|
68
68
|
if self.name:
|
|
69
|
-
return f'"
|
|
69
|
+
return f'"_unions.{self.name}"'
|
|
70
70
|
return self.type_definition(**kwargs)
|
|
71
71
|
|
|
72
72
|
def type_definition(self, **kwargs: Any) -> str:
|
|
@@ -116,10 +116,10 @@ class CombinedType(BaseType):
|
|
|
116
116
|
file_import = FileImport(self.code_model)
|
|
117
117
|
serialize_namespace = kwargs.get("serialize_namespace", self.code_model.namespace)
|
|
118
118
|
serialize_namespace_type = kwargs.get("serialize_namespace_type")
|
|
119
|
-
if self.name and serialize_namespace_type != NamespaceType.
|
|
119
|
+
if self.name and serialize_namespace_type != NamespaceType.UNIONS_FILE:
|
|
120
120
|
file_import.add_submodule_import(
|
|
121
121
|
self.code_model.get_relative_import_path(serialize_namespace),
|
|
122
|
-
"
|
|
122
|
+
"_unions",
|
|
123
123
|
ImportType.LOCAL,
|
|
124
124
|
TypingSection.TYPING,
|
|
125
125
|
)
|
|
@@ -167,6 +167,10 @@ class EnumType(BaseType):
|
|
|
167
167
|
enum_description = f"Known values are: {possible_values_str}."
|
|
168
168
|
return enum_description
|
|
169
169
|
|
|
170
|
+
@property
|
|
171
|
+
def is_typeddict_mode(self) -> bool:
|
|
172
|
+
return self.code_model.options["models-mode"] == "typeddict"
|
|
173
|
+
|
|
170
174
|
def type_annotation(self, **kwargs: Any) -> str:
|
|
171
175
|
"""The python type used for type annotation
|
|
172
176
|
|
|
@@ -174,9 +178,20 @@ class EnumType(BaseType):
|
|
|
174
178
|
:rtype: str
|
|
175
179
|
"""
|
|
176
180
|
if self.code_model.options["models-mode"]:
|
|
181
|
+
if self.is_typeddict_mode:
|
|
182
|
+
# In typeddict mode, enums are Literal aliases defined in types.py
|
|
183
|
+
serialize_namespace_type = kwargs.get("serialize_namespace_type")
|
|
184
|
+
if serialize_namespace_type == NamespaceType.TYPES_FILE:
|
|
185
|
+
# Same file — just the name, no module prefix
|
|
186
|
+
return self.name
|
|
187
|
+
# From operation/client files, use types.EnumName (matching model pattern)
|
|
188
|
+
return f"types.{self.name}"
|
|
177
189
|
|
|
178
190
|
module_name = ""
|
|
179
|
-
|
|
191
|
+
serialize_namespace_type = kwargs.get("serialize_namespace_type")
|
|
192
|
+
if serialize_namespace_type == NamespaceType.TYPES_FILE:
|
|
193
|
+
pass # no module prefix for types.py
|
|
194
|
+
elif kwargs.get("need_model_alias", True):
|
|
180
195
|
serialize_namespace = kwargs.get("serialize_namespace", self.code_model.namespace)
|
|
181
196
|
model_alias = self.code_model.get_unique_models_alias(serialize_namespace, self.client_namespace)
|
|
182
197
|
module_name = f"{model_alias}."
|
|
@@ -240,31 +255,66 @@ class EnumType(BaseType):
|
|
|
240
255
|
file_import = FileImport(self.code_model)
|
|
241
256
|
file_import.merge(self.value_type.imports(**kwargs))
|
|
242
257
|
if self.code_model.options["models-mode"]:
|
|
243
|
-
|
|
258
|
+
if self.is_typeddict_mode:
|
|
259
|
+
# In typeddict mode, enums are Literal aliases in types.py — no Union needed
|
|
260
|
+
serialize_namespace_type = kwargs.get("serialize_namespace_type")
|
|
261
|
+
if serialize_namespace_type == NamespaceType.TYPES_FILE:
|
|
262
|
+
# Same file — no import needed for same-namespace enums
|
|
263
|
+
serialize_namespace = kwargs.get("serialize_namespace", self.code_model.namespace)
|
|
264
|
+
if self.client_namespace != serialize_namespace:
|
|
265
|
+
# Cross-namespace: import from sibling types module
|
|
266
|
+
relative_path = self.code_model.get_relative_import_path(
|
|
267
|
+
serialize_namespace, self.client_namespace
|
|
268
|
+
)
|
|
269
|
+
file_import.add_submodule_import(
|
|
270
|
+
f"{relative_path}types" if relative_path != "." else ".types",
|
|
271
|
+
self.name,
|
|
272
|
+
ImportType.LOCAL,
|
|
273
|
+
typing_section=TypingSection.REGULAR,
|
|
274
|
+
)
|
|
275
|
+
elif serialize_namespace_type in [NamespaceType.OPERATION, NamespaceType.CLIENT]:
|
|
276
|
+
# Import types module directly (matching model pattern)
|
|
277
|
+
serialize_namespace = kwargs.get("serialize_namespace", self.code_model.namespace)
|
|
278
|
+
relative_path = self.code_model.get_relative_import_path(serialize_namespace, self.client_namespace)
|
|
279
|
+
file_import.add_submodule_import(
|
|
280
|
+
relative_path,
|
|
281
|
+
"types",
|
|
282
|
+
ImportType.LOCAL,
|
|
283
|
+
)
|
|
284
|
+
else:
|
|
285
|
+
file_import.add_submodule_import("typing", "Union", ImportType.STDLIB, TypingSection.REGULAR)
|
|
244
286
|
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
287
|
+
serialize_namespace = kwargs.get("serialize_namespace", self.code_model.namespace)
|
|
288
|
+
relative_path = self.code_model.get_relative_import_path(serialize_namespace, self.client_namespace)
|
|
289
|
+
alias = self.code_model.get_unique_models_alias(serialize_namespace, self.client_namespace)
|
|
290
|
+
serialize_namespace_type = kwargs.get("serialize_namespace_type")
|
|
291
|
+
called_by_property = kwargs.get("called_by_property", False)
|
|
292
|
+
if serialize_namespace_type in [NamespaceType.OPERATION, NamespaceType.CLIENT]:
|
|
293
|
+
file_import.add_submodule_import(
|
|
294
|
+
relative_path,
|
|
295
|
+
"models",
|
|
296
|
+
ImportType.LOCAL,
|
|
297
|
+
alias=alias,
|
|
298
|
+
typing_section=TypingSection.REGULAR,
|
|
299
|
+
)
|
|
300
|
+
elif serialize_namespace_type == NamespaceType.TYPES_FILE:
|
|
301
|
+
# Import enum name directly to avoid dotted forward refs in TypedDict annotations
|
|
302
|
+
file_import.add_submodule_import(
|
|
303
|
+
f"{relative_path}models" if relative_path != "." else ".models",
|
|
304
|
+
self.name,
|
|
305
|
+
ImportType.LOCAL,
|
|
306
|
+
typing_section=TypingSection.TYPING,
|
|
307
|
+
)
|
|
308
|
+
elif serialize_namespace_type == NamespaceType.UNIONS_FILE or (
|
|
309
|
+
serialize_namespace_type == NamespaceType.MODEL and called_by_property
|
|
310
|
+
):
|
|
311
|
+
file_import.add_submodule_import(
|
|
312
|
+
relative_path,
|
|
313
|
+
"models",
|
|
314
|
+
ImportType.LOCAL,
|
|
315
|
+
alias=alias,
|
|
316
|
+
typing_section=TypingSection.TYPING,
|
|
317
|
+
)
|
|
268
318
|
|
|
269
319
|
file_import.merge(self.value_type.imports(**kwargs))
|
|
270
320
|
return file_import
|
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
from typing import Any, Optional, Union, TYPE_CHECKING
|
|
7
7
|
from .base import BaseType
|
|
8
8
|
from .imports import FileImport
|
|
9
|
+
from .utils import NamespaceType
|
|
9
10
|
|
|
10
11
|
if TYPE_CHECKING:
|
|
11
12
|
from .code_model import CodeModel
|
|
@@ -41,12 +42,17 @@ class ListType(BaseType):
|
|
|
41
42
|
# this means we're version tolerant XML, we just return the XML element
|
|
42
43
|
return self.element_type.type_annotation(**kwargs)
|
|
43
44
|
|
|
44
|
-
# if there is a function
|
|
45
|
+
# if there is a function named `list` we have to make sure there's no conflict with the built-in `list`
|
|
46
|
+
# in operation files. The operation_groups_serializer defines `List = list` alias for this case.
|
|
47
|
+
serialize_namespace_type = kwargs.get("serialize_namespace_type")
|
|
45
48
|
is_operation_file = kwargs.get("is_operation_file", False)
|
|
46
|
-
|
|
47
|
-
|
|
49
|
+
in_operation_context = (
|
|
50
|
+
serialize_namespace_type in (NamespaceType.OPERATION, NamespaceType.CLIENT) or is_operation_file
|
|
48
51
|
)
|
|
49
|
-
|
|
52
|
+
if in_operation_context and self.code_model.has_operation_named_list:
|
|
53
|
+
list_type = "List"
|
|
54
|
+
else:
|
|
55
|
+
list_type = "list"
|
|
50
56
|
return f"{list_type}[{self.element_type.type_annotation(**kwargs)}]"
|
|
51
57
|
|
|
52
58
|
def description(self, *, is_operation_file: bool) -> str:
|
|
@@ -77,11 +77,19 @@ class ModelType(BaseType): # pylint: disable=too-many-instance-attributes, too-
|
|
|
77
77
|
self.cross_language_definition_id: Optional[str] = self.yaml_data.get("crossLanguageDefinitionId")
|
|
78
78
|
self.usage: int = self.yaml_data.get("usage", UsageFlags.Input.value | UsageFlags.Output.value)
|
|
79
79
|
self.client_namespace: str = self.yaml_data.get("clientNamespace", code_model.namespace)
|
|
80
|
+
self.is_typed_dict_only: bool = (
|
|
81
|
+
self.yaml_data.get("typedDictOnly", False) or code_model.options["models-mode"] == "typeddict"
|
|
82
|
+
)
|
|
80
83
|
|
|
81
84
|
@property
|
|
82
85
|
def is_usage_output(self) -> bool:
|
|
83
86
|
return bool(self.usage & UsageFlags.Output.value)
|
|
84
87
|
|
|
88
|
+
@property
|
|
89
|
+
def is_used_in_operations_via_types(self) -> bool:
|
|
90
|
+
"""Whether this model would be imported from types.py (not models) in operations."""
|
|
91
|
+
return False
|
|
92
|
+
|
|
85
93
|
@property
|
|
86
94
|
def flattened_property(self) -> Optional[Property]:
|
|
87
95
|
try:
|
|
@@ -275,12 +283,20 @@ class GeneratedModelType(ModelType):
|
|
|
275
283
|
def type_annotation(self, **kwargs: Any) -> str:
|
|
276
284
|
is_operation_file = kwargs.pop("is_operation_file", False)
|
|
277
285
|
skip_quote = kwargs.get("skip_quote", False)
|
|
286
|
+
serialize_namespace_type = kwargs.get("serialize_namespace_type")
|
|
278
287
|
module_name = ""
|
|
279
|
-
|
|
288
|
+
# In types.py, use bare name to avoid pyright "variable in type expression" errors
|
|
289
|
+
if serialize_namespace_type == NamespaceType.TYPES_FILE:
|
|
290
|
+
pass # no module prefix, no internal file prefix
|
|
291
|
+
elif kwargs.get("need_model_alias", True):
|
|
280
292
|
serialize_namespace = kwargs.get("serialize_namespace", self.code_model.namespace)
|
|
281
293
|
model_alias = self.code_model.get_unique_models_alias(serialize_namespace, self.client_namespace)
|
|
282
294
|
module_name = f"{model_alias}."
|
|
283
|
-
file_name =
|
|
295
|
+
file_name = (
|
|
296
|
+
f"{self.code_model.models_filename}."
|
|
297
|
+
if self.internal and serialize_namespace_type != NamespaceType.TYPES_FILE
|
|
298
|
+
else ""
|
|
299
|
+
)
|
|
284
300
|
retval = module_name + file_name + self.name
|
|
285
301
|
return retval if is_operation_file or skip_quote else f'"{retval}"'
|
|
286
302
|
|
|
@@ -305,7 +321,7 @@ class GeneratedModelType(ModelType):
|
|
|
305
321
|
alias = self.code_model.get_unique_models_alias(serialize_namespace, self.client_namespace)
|
|
306
322
|
serialize_namespace_type = kwargs.get("serialize_namespace_type")
|
|
307
323
|
called_by_property = kwargs.get("called_by_property", False)
|
|
308
|
-
# add import for models in operations or
|
|
324
|
+
# add import for models in operations, types, or unions file
|
|
309
325
|
if serialize_namespace_type in [NamespaceType.OPERATION, NamespaceType.CLIENT]:
|
|
310
326
|
file_import.add_submodule_import(
|
|
311
327
|
relative_path,
|
|
@@ -320,7 +336,33 @@ class GeneratedModelType(ModelType):
|
|
|
320
336
|
ImportType.LOCAL,
|
|
321
337
|
alias="_Model",
|
|
322
338
|
)
|
|
323
|
-
elif serialize_namespace_type == NamespaceType.TYPES_FILE
|
|
339
|
+
elif serialize_namespace_type == NamespaceType.TYPES_FILE:
|
|
340
|
+
# Don't import models that will be defined in this namespace's types.py —
|
|
341
|
+
# either as TypedDict classes (non-discriminated) or as Union aliases (discriminated bases).
|
|
342
|
+
# Only same-namespace non-json models are in the same types.py file.
|
|
343
|
+
same_namespace = relative_path == "."
|
|
344
|
+
will_be_in_types_file = self.base != "json" and same_namespace
|
|
345
|
+
if not will_be_in_types_file:
|
|
346
|
+
if same_namespace:
|
|
347
|
+
# json models from same namespace — import from .models (or .models._models for internal)
|
|
348
|
+
import_path = f".models.{self.code_model.models_filename}" if self.internal else ".models"
|
|
349
|
+
file_import.add_submodule_import(
|
|
350
|
+
import_path,
|
|
351
|
+
self.name,
|
|
352
|
+
ImportType.LOCAL,
|
|
353
|
+
typing_section=TypingSection.TYPING,
|
|
354
|
+
)
|
|
355
|
+
else:
|
|
356
|
+
# Cross-namespace model — import from sibling namespace's types module
|
|
357
|
+
file_import.add_submodule_import(
|
|
358
|
+
self.code_model.get_relative_import_path(
|
|
359
|
+
serialize_namespace, self.client_namespace, module_name="types"
|
|
360
|
+
),
|
|
361
|
+
self.name,
|
|
362
|
+
ImportType.LOCAL,
|
|
363
|
+
typing_section=TypingSection.TYPING,
|
|
364
|
+
)
|
|
365
|
+
elif serialize_namespace_type == NamespaceType.UNIONS_FILE or (
|
|
324
366
|
serialize_namespace_type == NamespaceType.MODEL and called_by_property
|
|
325
367
|
):
|
|
326
368
|
file_import.add_submodule_import(
|
|
@@ -352,6 +394,33 @@ class MsrestModelType(GeneratedModelType):
|
|
|
352
394
|
class DPGModelType(GeneratedModelType):
|
|
353
395
|
base = "dpg"
|
|
354
396
|
|
|
397
|
+
@property
|
|
398
|
+
def is_used_in_operations_via_types(self) -> bool:
|
|
399
|
+
return self.is_typed_dict_only
|
|
400
|
+
|
|
401
|
+
def type_annotation(self, **kwargs: Any) -> str:
|
|
402
|
+
if self.is_typed_dict_only:
|
|
403
|
+
is_operation_file = kwargs.pop("is_operation_file", False)
|
|
404
|
+
skip_quote = kwargs.get("skip_quote", False)
|
|
405
|
+
serialize_namespace_type = kwargs.get("serialize_namespace_type")
|
|
406
|
+
# Within types.py, use bare name (no module prefix)
|
|
407
|
+
if serialize_namespace_type == NamespaceType.TYPES_FILE:
|
|
408
|
+
retval = self.name
|
|
409
|
+
else:
|
|
410
|
+
serialize_namespace = kwargs.get("serialize_namespace", self.code_model.namespace)
|
|
411
|
+
types_alias = self.code_model.get_unique_types_alias(serialize_namespace, self.client_namespace)
|
|
412
|
+
retval = f"{types_alias}.{self.name}"
|
|
413
|
+
return retval if is_operation_file or skip_quote else f'"{retval}"'
|
|
414
|
+
return super().type_annotation(**kwargs)
|
|
415
|
+
|
|
416
|
+
def docstring_type(self, **kwargs: Any) -> str:
|
|
417
|
+
if self.is_typed_dict_only:
|
|
418
|
+
client_namespace = self.client_namespace
|
|
419
|
+
if self.code_model.options.get("generation-subdir"):
|
|
420
|
+
client_namespace += f".{self.code_model.options['generation-subdir']}"
|
|
421
|
+
return f"~{client_namespace}.types.{self.name}"
|
|
422
|
+
return super().docstring_type(**kwargs)
|
|
423
|
+
|
|
355
424
|
def serialization_type(self, **kwargs: Any) -> str:
|
|
356
425
|
return (
|
|
357
426
|
self.type_annotation(skip_quote=True, **kwargs)
|
|
@@ -364,7 +433,79 @@ class DPGModelType(GeneratedModelType):
|
|
|
364
433
|
return "isinstance({}, " + f"_models.{self.name})"
|
|
365
434
|
|
|
366
435
|
def imports(self, **kwargs: Any) -> FileImport:
|
|
436
|
+
if self.is_typed_dict_only:
|
|
437
|
+
file_import = FileImport(self.code_model)
|
|
438
|
+
serialize_namespace_type = kwargs.get("serialize_namespace_type")
|
|
439
|
+
serialize_namespace = kwargs.get("serialize_namespace", self.code_model.namespace)
|
|
440
|
+
relative_path = self.code_model.get_relative_import_path(serialize_namespace, self.client_namespace)
|
|
441
|
+
alias = self.code_model.get_unique_types_alias(serialize_namespace, self.client_namespace)
|
|
442
|
+
same_namespace = relative_path == "."
|
|
443
|
+
if serialize_namespace_type in [NamespaceType.OPERATION, NamespaceType.CLIENT]:
|
|
444
|
+
file_import.add_submodule_import(
|
|
445
|
+
relative_path,
|
|
446
|
+
"types",
|
|
447
|
+
ImportType.LOCAL,
|
|
448
|
+
alias=alias,
|
|
449
|
+
)
|
|
450
|
+
elif serialize_namespace_type == NamespaceType.TYPES_FILE and same_namespace:
|
|
451
|
+
pass # model is defined in this types.py — no import needed
|
|
452
|
+
elif serialize_namespace_type in [NamespaceType.TYPES_FILE, NamespaceType.UNIONS_FILE] or (
|
|
453
|
+
serialize_namespace_type == NamespaceType.MODEL and kwargs.get("called_by_property", False)
|
|
454
|
+
):
|
|
455
|
+
file_import.add_submodule_import(
|
|
456
|
+
relative_path,
|
|
457
|
+
"types",
|
|
458
|
+
ImportType.LOCAL,
|
|
459
|
+
alias=alias,
|
|
460
|
+
typing_section=TypingSection.TYPING,
|
|
461
|
+
)
|
|
462
|
+
return file_import
|
|
367
463
|
file_import = super().imports(**kwargs)
|
|
368
464
|
if self.flattened_property:
|
|
369
465
|
file_import.add_submodule_import("typing", "Any", ImportType.STDLIB)
|
|
370
466
|
return file_import
|
|
467
|
+
|
|
468
|
+
|
|
469
|
+
class TypedDictModelType(DPGModelType):
|
|
470
|
+
base = "typeddict"
|
|
471
|
+
|
|
472
|
+
@property
|
|
473
|
+
def is_used_in_operations_via_types(self) -> bool:
|
|
474
|
+
return True
|
|
475
|
+
|
|
476
|
+
def type_annotation(self, **kwargs: Any) -> str:
|
|
477
|
+
is_operation_file = kwargs.pop("is_operation_file", False)
|
|
478
|
+
skip_quote = kwargs.get("skip_quote", False)
|
|
479
|
+
serialize_namespace_type = kwargs.get("serialize_namespace_type")
|
|
480
|
+
if serialize_namespace_type == NamespaceType.TYPES_FILE:
|
|
481
|
+
retval = self.name
|
|
482
|
+
else:
|
|
483
|
+
serialize_namespace = kwargs.get("serialize_namespace", self.code_model.namespace)
|
|
484
|
+
types_alias = self.code_model.get_unique_types_alias(serialize_namespace, self.client_namespace)
|
|
485
|
+
retval = f"{types_alias}.{self.name}"
|
|
486
|
+
return retval if is_operation_file or skip_quote else f'"{retval}"'
|
|
487
|
+
|
|
488
|
+
def docstring_type(self, **kwargs: Any) -> str:
|
|
489
|
+
client_namespace = self.client_namespace
|
|
490
|
+
if self.code_model.options.get("generation-subdir"):
|
|
491
|
+
client_namespace += f".{self.code_model.options['generation-subdir']}"
|
|
492
|
+
return f"~{client_namespace}.types.{self.name}"
|
|
493
|
+
|
|
494
|
+
@property
|
|
495
|
+
def instance_check_template(self) -> str:
|
|
496
|
+
return "isinstance({}, MutableMapping)"
|
|
497
|
+
|
|
498
|
+
def imports(self, **kwargs: Any) -> FileImport:
|
|
499
|
+
file_import = FileImport(self.code_model)
|
|
500
|
+
serialize_namespace_type = kwargs.get("serialize_namespace_type")
|
|
501
|
+
serialize_namespace = kwargs.get("serialize_namespace", self.code_model.namespace)
|
|
502
|
+
relative_path = self.code_model.get_relative_import_path(serialize_namespace, self.client_namespace)
|
|
503
|
+
alias = self.code_model.get_unique_types_alias(serialize_namespace, self.client_namespace)
|
|
504
|
+
if serialize_namespace_type in [NamespaceType.OPERATION, NamespaceType.CLIENT]:
|
|
505
|
+
file_import.add_submodule_import(
|
|
506
|
+
relative_path,
|
|
507
|
+
"types",
|
|
508
|
+
ImportType.LOCAL,
|
|
509
|
+
alias=alias,
|
|
510
|
+
)
|
|
511
|
+
return file_import
|
|
@@ -458,6 +458,7 @@ class OperationBase( # pylint: disable=too-many-public-methods,too-many-instanc
|
|
|
458
458
|
r.type
|
|
459
459
|
and not isinstance(r.type, BinaryIteratorType)
|
|
460
460
|
and not xml_serializable(str(r.default_content_type))
|
|
461
|
+
and not (isinstance(r.type, ModelType) and r.type.is_typed_dict_only)
|
|
461
462
|
for r in self.responses
|
|
462
463
|
):
|
|
463
464
|
file_import.add_submodule_import(relative_path, "_deserialize", ImportType.LOCAL)
|
|
@@ -186,7 +186,11 @@ class PagingOperationBase(OperationBase[PagingResponseType]):
|
|
|
186
186
|
serialize_namespace, module_name="_utils.model_base"
|
|
187
187
|
)
|
|
188
188
|
file_import.merge(self.item_type.imports(**kwargs))
|
|
189
|
-
|
|
189
|
+
need_deserialize_import = self.default_error_deserialization(serialize_namespace) or (
|
|
190
|
+
self.need_deserialize
|
|
191
|
+
and not (isinstance(self.item_type, ModelType) and self.item_type.is_typed_dict_only)
|
|
192
|
+
)
|
|
193
|
+
if need_deserialize_import:
|
|
190
194
|
file_import.add_submodule_import(relative_path, "_deserialize", ImportType.LOCAL)
|
|
191
195
|
if self.is_xml_paging:
|
|
192
196
|
file_import.add_submodule_import("xml.etree", "ElementTree", ImportType.STDLIB, alias="ET")
|
|
@@ -180,7 +180,7 @@ class _ParameterBase(BaseModel, abc.ABC): # pylint: disable=too-many-instance-a
|
|
|
180
180
|
if isinstance(self.type, CombinedType) and self.type.name:
|
|
181
181
|
file_import.add_submodule_import(
|
|
182
182
|
self.code_model.get_relative_import_path(serialize_namespace),
|
|
183
|
-
"
|
|
183
|
+
"_unions",
|
|
184
184
|
ImportType.LOCAL,
|
|
185
185
|
TypingSection.TYPING,
|
|
186
186
|
)
|
|
@@ -338,7 +338,7 @@ class Parameter(_ParameterBase):
|
|
|
338
338
|
) -> ParameterMethodLocation:
|
|
339
339
|
if not self.in_method_signature:
|
|
340
340
|
raise ValueError(f"Parameter '{self.client_name}' is not in the method.")
|
|
341
|
-
if self.code_model.options["models-mode"]
|
|
341
|
+
if self.code_model.options["models-mode"] in ("dpg", "typeddict") and self.in_flattened_body:
|
|
342
342
|
return ParameterMethodLocation.KEYWORD_ONLY
|
|
343
343
|
if self.grouper:
|
|
344
344
|
return ParameterMethodLocation.POSITIONAL
|