@autorest/python 5.16.0 → 5.19.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.
Files changed (96) hide show
  1. package/ChangeLog.md +79 -4
  2. package/README.md +30 -4
  3. package/autorest/__init__.py +1 -1
  4. package/autorest/codegen/__init__.py +55 -211
  5. package/autorest/codegen/models/__init__.py +116 -83
  6. package/autorest/codegen/models/base_builder.py +49 -88
  7. package/autorest/codegen/models/base_model.py +1 -1
  8. package/autorest/codegen/models/{base_schema.py → base_type.py} +61 -39
  9. package/autorest/codegen/models/client.py +165 -53
  10. package/autorest/codegen/models/code_model.py +122 -257
  11. package/autorest/codegen/models/combined_type.py +107 -0
  12. package/autorest/codegen/models/{constant_schema.py → constant_type.py} +49 -40
  13. package/autorest/codegen/models/credential_types.py +224 -0
  14. package/autorest/codegen/models/dictionary_type.py +131 -0
  15. package/autorest/codegen/models/enum_type.py +195 -0
  16. package/autorest/codegen/models/imports.py +80 -2
  17. package/autorest/codegen/models/list_type.py +149 -0
  18. package/autorest/codegen/models/lro_operation.py +79 -156
  19. package/autorest/codegen/models/lro_paging_operation.py +28 -11
  20. package/autorest/codegen/models/model_type.py +262 -0
  21. package/autorest/codegen/models/operation.py +331 -298
  22. package/autorest/codegen/models/operation_group.py +54 -91
  23. package/autorest/codegen/models/paging_operation.py +82 -123
  24. package/autorest/codegen/models/parameter.py +289 -396
  25. package/autorest/codegen/models/parameter_list.py +355 -360
  26. package/autorest/codegen/models/primitive_types.py +544 -0
  27. package/autorest/codegen/models/property.py +123 -139
  28. package/autorest/codegen/models/request_builder.py +130 -102
  29. package/autorest/codegen/models/request_builder_parameter.py +112 -100
  30. package/autorest/codegen/models/response.py +325 -0
  31. package/autorest/codegen/models/utils.py +12 -19
  32. package/autorest/codegen/serializers/__init__.py +55 -37
  33. package/autorest/codegen/serializers/builder_serializer.py +695 -1144
  34. package/autorest/codegen/serializers/client_serializer.py +92 -89
  35. package/autorest/codegen/serializers/general_serializer.py +15 -69
  36. package/autorest/codegen/serializers/import_serializer.py +7 -4
  37. package/autorest/codegen/serializers/metadata_serializer.py +15 -104
  38. package/autorest/codegen/serializers/model_base_serializer.py +49 -36
  39. package/autorest/codegen/serializers/model_generic_serializer.py +8 -6
  40. package/autorest/codegen/serializers/model_init_serializer.py +2 -4
  41. package/autorest/codegen/serializers/model_python3_serializer.py +22 -16
  42. package/autorest/codegen/serializers/operation_groups_serializer.py +7 -13
  43. package/autorest/codegen/serializers/parameter_serializer.py +174 -0
  44. package/autorest/codegen/serializers/request_builders_serializer.py +13 -30
  45. package/autorest/codegen/serializers/utils.py +0 -140
  46. package/autorest/codegen/templates/MANIFEST.in.jinja2 +1 -0
  47. package/autorest/codegen/templates/{service_client.py.jinja2 → client.py.jinja2} +10 -7
  48. package/autorest/codegen/templates/config.py.jinja2 +13 -13
  49. package/autorest/codegen/templates/enum.py.jinja2 +4 -4
  50. package/autorest/codegen/templates/enum_container.py.jinja2 +1 -1
  51. package/autorest/codegen/templates/init.py.jinja2 +2 -2
  52. package/autorest/codegen/templates/lro_operation.py.jinja2 +4 -1
  53. package/autorest/codegen/templates/lro_paging_operation.py.jinja2 +4 -1
  54. package/autorest/codegen/templates/metadata.json.jinja2 +33 -33
  55. package/autorest/codegen/templates/model.py.jinja2 +23 -24
  56. package/autorest/codegen/templates/model_container.py.jinja2 +2 -1
  57. package/autorest/codegen/templates/model_init.py.jinja2 +3 -5
  58. package/autorest/codegen/templates/operation.py.jinja2 +6 -8
  59. package/autorest/codegen/templates/operation_group.py.jinja2 +21 -8
  60. package/autorest/codegen/templates/operation_groups_container.py.jinja2 +2 -2
  61. package/autorest/codegen/templates/operation_tools.jinja2 +11 -3
  62. package/autorest/codegen/templates/paging_operation.py.jinja2 +2 -2
  63. package/autorest/codegen/templates/request_builder.py.jinja2 +10 -15
  64. package/autorest/codegen/templates/request_builders.py.jinja2 +1 -1
  65. package/autorest/codegen/templates/serialization.py.jinja2 +2006 -0
  66. package/autorest/codegen/templates/setup.py.jinja2 +13 -3
  67. package/autorest/codegen/templates/vendor.py.jinja2 +11 -1
  68. package/autorest/jsonrpc/server.py +15 -3
  69. package/autorest/m4reformatter/__init__.py +1126 -0
  70. package/autorest/multiapi/models/client.py +12 -2
  71. package/autorest/multiapi/models/code_model.py +1 -1
  72. package/autorest/multiapi/serializers/__init__.py +18 -4
  73. package/autorest/multiapi/templates/multiapi_config.py.jinja2 +3 -3
  74. package/autorest/multiapi/templates/multiapi_init.py.jinja2 +2 -2
  75. package/autorest/multiapi/templates/multiapi_operations_mixin.py.jinja2 +4 -4
  76. package/autorest/multiapi/templates/multiapi_service_client.py.jinja2 +9 -9
  77. package/autorest/postprocess/__init__.py +202 -0
  78. package/autorest/postprocess/get_all.py +19 -0
  79. package/autorest/postprocess/venvtools.py +73 -0
  80. package/autorest/preprocess/__init__.py +210 -0
  81. package/autorest/preprocess/helpers.py +54 -0
  82. package/autorest/{namer → preprocess}/python_mappings.py +21 -16
  83. package/package.json +2 -2
  84. package/autorest/codegen/models/credential_model.py +0 -55
  85. package/autorest/codegen/models/credential_schema.py +0 -95
  86. package/autorest/codegen/models/credential_schema_policy.py +0 -73
  87. package/autorest/codegen/models/dictionary_schema.py +0 -106
  88. package/autorest/codegen/models/enum_schema.py +0 -225
  89. package/autorest/codegen/models/list_schema.py +0 -135
  90. package/autorest/codegen/models/object_schema.py +0 -303
  91. package/autorest/codegen/models/primitive_schemas.py +0 -495
  92. package/autorest/codegen/models/request_builder_parameter_list.py +0 -249
  93. package/autorest/codegen/models/schema_request.py +0 -55
  94. package/autorest/codegen/models/schema_response.py +0 -141
  95. package/autorest/namer/__init__.py +0 -23
  96. package/autorest/namer/name_converter.py +0 -509
@@ -1,495 +0,0 @@
1
- # -------------------------------------------------------------------------
2
- # Copyright (c) Microsoft Corporation. All rights reserved.
3
- # Licensed under the MIT License. See License.txt in the project root for
4
- # license information.
5
- # --------------------------------------------------------------------------
6
- import logging
7
- import datetime
8
- from enum import Enum
9
- from typing import cast, Any, Dict, List, Optional, Union, TYPE_CHECKING
10
-
11
- from .base_schema import BaseSchema
12
- from .imports import FileImport, ImportType, TypingSection
13
-
14
- if TYPE_CHECKING:
15
- from .code_model import CodeModel
16
-
17
-
18
- _LOGGER = logging.getLogger(__name__)
19
-
20
-
21
- class RawString(object):
22
- def __init__(self, string: str) -> None:
23
- self.string = string
24
-
25
- def __repr__(self) -> str:
26
- return "r'{}'".format(self.string.replace("'", "\\'"))
27
-
28
-
29
- class PrimitiveSchema(BaseSchema):
30
- _TYPE_MAPPINGS = {
31
- "boolean": "bool",
32
- }
33
-
34
- def _to_python_type(self) -> str:
35
- return self._TYPE_MAPPINGS.get(self.yaml_data["type"], "str")
36
-
37
- @property
38
- def serialization_type(self) -> str:
39
- return self._to_python_type()
40
-
41
- @property
42
- def docstring_type(self) -> str:
43
- return self._to_python_type()
44
-
45
- def type_annotation(
46
- self, *, is_operation_file: bool = False # pylint: disable=unused-argument
47
- ) -> str:
48
- return self.docstring_type
49
-
50
- @property
51
- def docstring_text(self) -> str:
52
- return self.docstring_type
53
-
54
- def _add_optional_and_default_value_template_representation(
55
- self,
56
- *,
57
- optional: bool = True,
58
- default_value_declaration: Optional[str] = None,
59
- description: Optional[str] = None,
60
- ):
61
- comment = ""
62
- if optional:
63
- comment += " Optional."
64
- if default_value_declaration:
65
- comment += f" Default value is {default_value_declaration}."
66
- else:
67
- default_value_declaration = self.default_template_representation_declaration
68
- if description:
69
- comment += f" {description}"
70
- if comment:
71
- comment = f"#{comment}"
72
- return f"{default_value_declaration}{comment}"
73
-
74
- def get_json_template_representation(self, **kwargs: Any) -> Any:
75
- if self.default_value:
76
- kwargs["default_value_declaration"] = kwargs.get(
77
- "default_value_declaration", self.get_declaration(self.default_value)
78
- )
79
- return self._add_optional_and_default_value_template_representation(**kwargs)
80
-
81
- @property
82
- def default_template_representation_declaration(self) -> str:
83
- return self.get_declaration(self.docstring_type)
84
-
85
-
86
- class IOSchema(PrimitiveSchema):
87
- def __init__(self, yaml_data: Dict[str, Any], code_model: "CodeModel") -> None:
88
- super().__init__(yaml_data=yaml_data, code_model=code_model)
89
- self.type = "IO"
90
-
91
- @property
92
- def serialization_type(self) -> str:
93
- return self.type
94
-
95
- @property
96
- def docstring_type(self) -> str:
97
- return self.type
98
-
99
- def type_annotation(
100
- self, *, is_operation_file: bool = False # pylint: disable=unused-argument
101
- ) -> str:
102
- return self.docstring_type
103
-
104
- @property
105
- def docstring_text(self) -> str:
106
- return "IO"
107
-
108
- @property
109
- def default_template_representation_declaration(self) -> str:
110
- return self.get_declaration(b"bytes")
111
-
112
- def imports(self) -> FileImport:
113
- file_import = FileImport()
114
- file_import.add_submodule_import(
115
- "typing", "IO", ImportType.STDLIB, TypingSection.CONDITIONAL
116
- )
117
- return file_import
118
-
119
-
120
- class AnySchema(PrimitiveSchema):
121
- @property
122
- def serialization_type(self) -> str:
123
- return "object"
124
-
125
- @property
126
- def docstring_type(self) -> str:
127
- return "any"
128
-
129
- def type_annotation(
130
- self, *, is_operation_file: bool = False # pylint: disable=unused-argument
131
- ) -> str:
132
- return "Any"
133
-
134
- @property
135
- def default_template_representation_declaration(self) -> str:
136
- return self.get_declaration({})
137
-
138
- def imports(self) -> FileImport:
139
- file_import = FileImport()
140
- file_import.add_submodule_import(
141
- "typing", "Any", ImportType.STDLIB, TypingSection.CONDITIONAL
142
- )
143
- return file_import
144
-
145
-
146
- class NumberSchema(PrimitiveSchema):
147
- def __init__(self, yaml_data: Dict[str, Any], code_model: "CodeModel") -> None:
148
- super().__init__(yaml_data=yaml_data, code_model=code_model)
149
- self.precision = cast(int, yaml_data["precision"])
150
- self.multiple = cast(int, yaml_data.get("multipleOf"))
151
- self.maximum = cast(int, yaml_data.get("maximum"))
152
- self.minimum = cast(int, yaml_data.get("minimum"))
153
- self.exclusive_maximum = cast(int, yaml_data.get("exclusiveMaximum"))
154
- self.exclusive_minimum = cast(int, yaml_data.get("exclusiveMinimum"))
155
-
156
- @property
157
- def serialization_constraints(self) -> List[str]:
158
- validation_constraints = [
159
- f"maximum_ex={self.maximum}"
160
- if self.maximum is not None and self.exclusive_maximum
161
- else None,
162
- f"maximum={self.maximum}"
163
- if self.maximum is not None and not self.exclusive_maximum
164
- else None,
165
- f"minimum_ex={self.minimum}"
166
- if self.minimum is not None and self.exclusive_minimum
167
- else None,
168
- f"minimum={self.minimum}"
169
- if self.minimum is not None and not self.exclusive_minimum
170
- else None,
171
- f"multiple={self.multiple}" if self.multiple else None,
172
- ]
173
- return [x for x in validation_constraints if x is not None]
174
-
175
- @property
176
- def validation_map(self) -> Optional[Dict[str, Union[bool, int, str]]]:
177
- validation_map: Dict[str, Union[bool, int, str]] = {}
178
- if self.maximum is not None:
179
- if self.exclusive_maximum:
180
- validation_map["maximum_ex"] = self.maximum
181
- else:
182
- validation_map["maximum"] = self.maximum
183
- if self.minimum is not None:
184
- if self.exclusive_minimum:
185
- validation_map["minimum_ex"] = self.minimum
186
- else:
187
- validation_map["minimum"] = self.minimum
188
- if self.multiple:
189
- validation_map["multiple"] = self.multiple
190
- return validation_map or None
191
-
192
- @property
193
- def serialization_type(self) -> str:
194
- if self.yaml_data["type"] == "integer":
195
- if self.precision == 64:
196
- return "long"
197
- return "int"
198
- return "float"
199
-
200
- @property
201
- def docstring_type(self) -> str:
202
- if self.yaml_data["type"] == "integer":
203
- if self.precision == 64:
204
- return "long"
205
- return "int"
206
- return "float"
207
-
208
- def type_annotation(
209
- self, *, is_operation_file: bool = False # pylint: disable=unused-argument
210
- ) -> str:
211
- python_type = self.docstring_type
212
- if python_type == "long":
213
- return "int"
214
- return python_type
215
-
216
- @property
217
- def default_template_representation_declaration(self) -> str:
218
- default_value = 0 if self.docstring_type == "int" else 0.0
219
- return self.get_declaration(default_value)
220
-
221
-
222
- class StringSchema(PrimitiveSchema):
223
- def __init__(self, yaml_data: Dict[str, Any], code_model: "CodeModel") -> None:
224
- super().__init__(yaml_data=yaml_data, code_model=code_model)
225
- self.max_length = cast(int, yaml_data.get("maxLength"))
226
- self.min_length = cast(
227
- int,
228
- (
229
- yaml_data.get("minLength", 0)
230
- if yaml_data.get("maxLength")
231
- else yaml_data.get("minLength")
232
- ),
233
- )
234
- self.pattern = cast(str, yaml_data.get("pattern"))
235
-
236
- @property
237
- def serialization_constraints(self) -> List[str]:
238
- validation_constraints = [
239
- f"max_length={self.max_length}" if self.max_length is not None else None,
240
- f"min_length={self.min_length}" if self.min_length is not None else None,
241
- f"pattern={RawString(self.pattern)}" if self.pattern else None,
242
- ]
243
- return [x for x in validation_constraints if x is not None]
244
-
245
- @property
246
- def validation_map(self) -> Optional[Dict[str, Union[bool, int, str]]]:
247
- validation_map: Dict[str, Union[bool, int, str]] = {}
248
- if self.max_length is not None:
249
- validation_map["max_length"] = self.max_length
250
- if self.min_length is not None:
251
- validation_map["min_length"] = self.min_length
252
- if self.pattern:
253
- # https://github.com/Azure/autorest.python/issues/407
254
- validation_map["pattern"] = RawString(self.pattern) # type: ignore
255
- return validation_map or None
256
-
257
- def get_declaration(self, value) -> str:
258
- return f'"{value}"'
259
-
260
-
261
- class DatetimeSchema(PrimitiveSchema):
262
- def __init__(self, yaml_data: Dict[str, Any], code_model: "CodeModel") -> None:
263
- super().__init__(yaml_data=yaml_data, code_model=code_model)
264
- self.format = self.Formats(yaml_data["format"])
265
-
266
- class Formats(str, Enum):
267
- datetime = "date-time"
268
- rfc1123 = "date-time-rfc1123"
269
-
270
- @property
271
- def serialization_type(self) -> str:
272
- formats_to_attribute_type = {
273
- self.Formats.datetime: "iso-8601",
274
- self.Formats.rfc1123: "rfc-1123",
275
- }
276
- return formats_to_attribute_type[self.format]
277
-
278
- @property
279
- def docstring_type(self) -> str:
280
- return "~" + self.type_annotation()
281
-
282
- def type_annotation(
283
- self, *, is_operation_file: bool = False # pylint: disable=unused-argument
284
- ) -> str:
285
- return "datetime.datetime"
286
-
287
- @property
288
- def docstring_text(self) -> str:
289
- return "datetime"
290
-
291
- def get_declaration(self, value: datetime.datetime) -> str:
292
- """Could be discussed, since technically I should return a datetime object,
293
- but msrest will do fine.
294
- """
295
- return f'"{value}"'
296
-
297
- def imports(self) -> FileImport:
298
- file_import = FileImport()
299
- file_import.add_import("datetime", ImportType.STDLIB)
300
- return file_import
301
-
302
- @property
303
- def default_template_representation_declaration(self):
304
- return self.get_declaration(datetime.datetime(2020, 2, 20))
305
-
306
-
307
- class TimeSchema(PrimitiveSchema):
308
- @property
309
- def serialization_type(self) -> str:
310
- return "time"
311
-
312
- @property
313
- def docstring_type(self) -> str:
314
- return "~" + self.type_annotation()
315
-
316
- def type_annotation(
317
- self, *, is_operation_file: bool = False # pylint: disable=unused-argument
318
- ) -> str:
319
- return "datetime.time"
320
-
321
- @property
322
- def docstring_text(self) -> str:
323
- return "time"
324
-
325
- def get_declaration(self, value: datetime.time) -> str:
326
- """Could be discussed, since technically I should return a time object,
327
- but msrest will do fine.
328
- """
329
- return f'"{value}"'
330
-
331
- def imports(self) -> FileImport:
332
- file_import = FileImport()
333
- file_import.add_import("datetime", ImportType.STDLIB)
334
- return file_import
335
-
336
- @property
337
- def default_template_representation_declaration(self) -> str:
338
- return self.get_declaration(datetime.time(12, 30, 0))
339
-
340
-
341
- class UnixTimeSchema(PrimitiveSchema):
342
- @property
343
- def serialization_type(self) -> str:
344
- return "unix-time"
345
-
346
- @property
347
- def docstring_type(self) -> str:
348
- return "~" + self.type_annotation()
349
-
350
- def type_annotation(
351
- self, *, is_operation_file: bool = False # pylint: disable=unused-argument
352
- ) -> str:
353
- return "datetime.datetime"
354
-
355
- @property
356
- def docstring_text(self) -> str:
357
- return "datetime"
358
-
359
- def get_declaration(self, value: datetime.datetime) -> str:
360
- """Could be discussed, since technically I should return a datetime object,
361
- but msrest will do fine.
362
- """
363
- return f'"{value}"'
364
-
365
- def imports(self) -> FileImport:
366
- file_import = FileImport()
367
- file_import.add_import("datetime", ImportType.STDLIB)
368
- return file_import
369
-
370
- @property
371
- def default_template_representation_declaration(self) -> str:
372
- return self.get_declaration(datetime.datetime(2020, 2, 20))
373
-
374
-
375
- class DateSchema(PrimitiveSchema):
376
- @property
377
- def serialization_type(self) -> str:
378
- return "date"
379
-
380
- @property
381
- def docstring_type(self) -> str:
382
- return "~" + self.type_annotation()
383
-
384
- def type_annotation(
385
- self, *, is_operation_file: bool = False # pylint: disable=unused-argument
386
- ) -> str:
387
- return "datetime.date"
388
-
389
- @property
390
- def docstring_text(self) -> str:
391
- return "date"
392
-
393
- def get_declaration(self, value: datetime.date) -> str:
394
- """Could be discussed, since technically I should return a datetime object,
395
- but msrest will do fine.
396
- """
397
- return f'"{value}"'
398
-
399
- def imports(self) -> FileImport:
400
- file_import = FileImport()
401
- file_import.add_import("datetime", ImportType.STDLIB)
402
- return file_import
403
-
404
- @property
405
- def default_template_representation_declaration(self) -> str:
406
- return self.get_declaration(datetime.date(2020, 2, 20))
407
-
408
-
409
- class DurationSchema(PrimitiveSchema):
410
- @property
411
- def serialization_type(self) -> str:
412
- return "duration"
413
-
414
- @property
415
- def docstring_type(self) -> str:
416
- return "~" + self.type_annotation()
417
-
418
- def type_annotation(
419
- self, *, is_operation_file: bool = False # pylint: disable=unused-argument
420
- ) -> str:
421
- return "datetime.timedelta"
422
-
423
- @property
424
- def docstring_text(self) -> str:
425
- return "timedelta"
426
-
427
- def get_declaration(self, value: datetime.timedelta) -> str:
428
- """Could be discussed, since technically I should return a datetime object,
429
- but msrest will do fine.
430
- """
431
- return f'"{value}"'
432
-
433
- def imports(self) -> FileImport:
434
- file_import = FileImport()
435
- file_import.add_import("datetime", ImportType.STDLIB)
436
- return file_import
437
-
438
- @property
439
- def default_template_representation_declaration(self) -> str:
440
- return self.get_declaration(datetime.timedelta(1))
441
-
442
-
443
- class ByteArraySchema(PrimitiveSchema):
444
- def __init__(self, yaml_data: Dict[str, Any], code_model: "CodeModel") -> None:
445
- super().__init__(yaml_data=yaml_data, code_model=code_model)
446
- self.format = self.Formats(yaml_data["format"])
447
-
448
- class Formats(str, Enum):
449
- base64url = "base64url"
450
- byte = "byte"
451
-
452
- @property
453
- def serialization_type(self) -> str:
454
- if self.format == ByteArraySchema.Formats.base64url:
455
- return "base64"
456
- return "bytearray"
457
-
458
- @property
459
- def docstring_type(self) -> str:
460
- if self.format == ByteArraySchema.Formats.base64url:
461
- return "bytes"
462
- return "bytearray"
463
-
464
- def get_declaration(self, value: str) -> str:
465
- if self.format == ByteArraySchema.Formats.base64url:
466
- return f'bytes("{value}", encoding="utf-8")'
467
- return f'bytearray("{value}", encoding="utf-8")'
468
-
469
-
470
- def get_primitive_schema(
471
- yaml_data: Dict[str, Any], code_model: "CodeModel"
472
- ) -> "PrimitiveSchema":
473
- mapping = {
474
- "integer": NumberSchema,
475
- "number": NumberSchema,
476
- "string": StringSchema,
477
- "char": StringSchema,
478
- "date-time": DatetimeSchema,
479
- "time": TimeSchema,
480
- "unixtime": UnixTimeSchema,
481
- "date": DateSchema,
482
- "duration": DurationSchema,
483
- "byte-array": ByteArraySchema,
484
- "any": AnySchema,
485
- "any-object": AnySchema,
486
- "binary": IOSchema,
487
- }
488
- schema_type = yaml_data["type"]
489
- primitive_schema = cast(
490
- PrimitiveSchema,
491
- mapping.get(schema_type, PrimitiveSchema).from_yaml(
492
- yaml_data=yaml_data, code_model=code_model
493
- ),
494
- )
495
- return primitive_schema