@autorest/python 5.14.0 → 5.17.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 (120) hide show
  1. package/ChangeLog.md +91 -2
  2. package/README.md +30 -4
  3. package/autorest/__init__.py +2 -3
  4. package/autorest/black/__init__.py +12 -5
  5. package/autorest/codegen/__init__.py +130 -179
  6. package/autorest/codegen/models/__init__.py +122 -78
  7. package/autorest/codegen/models/base_builder.py +70 -72
  8. package/autorest/codegen/models/base_model.py +7 -5
  9. package/autorest/codegen/models/{base_schema.py → base_type.py} +62 -49
  10. package/autorest/codegen/models/client.py +195 -36
  11. package/autorest/codegen/models/code_model.py +165 -299
  12. package/autorest/codegen/models/combined_type.py +107 -0
  13. package/autorest/codegen/models/constant_type.py +122 -0
  14. package/autorest/codegen/models/credential_types.py +224 -0
  15. package/autorest/codegen/models/dictionary_type.py +116 -0
  16. package/autorest/codegen/models/enum_type.py +195 -0
  17. package/autorest/codegen/models/imports.py +95 -41
  18. package/autorest/codegen/models/list_type.py +134 -0
  19. package/autorest/codegen/models/lro_operation.py +90 -133
  20. package/autorest/codegen/models/lro_paging_operation.py +28 -12
  21. package/autorest/codegen/models/model_type.py +239 -0
  22. package/autorest/codegen/models/operation.py +415 -241
  23. package/autorest/codegen/models/operation_group.py +82 -88
  24. package/autorest/codegen/models/paging_operation.py +101 -117
  25. package/autorest/codegen/models/parameter.py +307 -322
  26. package/autorest/codegen/models/parameter_list.py +366 -357
  27. package/autorest/codegen/models/primitive_types.py +544 -0
  28. package/autorest/codegen/models/property.py +122 -134
  29. package/autorest/codegen/models/request_builder.py +138 -86
  30. package/autorest/codegen/models/request_builder_parameter.py +122 -79
  31. package/autorest/codegen/models/response.py +325 -0
  32. package/autorest/codegen/models/utils.py +17 -1
  33. package/autorest/codegen/serializers/__init__.py +242 -118
  34. package/autorest/codegen/serializers/builder_serializer.py +863 -1027
  35. package/autorest/codegen/serializers/client_serializer.py +148 -82
  36. package/autorest/codegen/serializers/general_serializer.py +44 -47
  37. package/autorest/codegen/serializers/import_serializer.py +96 -31
  38. package/autorest/codegen/serializers/metadata_serializer.py +39 -79
  39. package/autorest/codegen/serializers/model_base_serializer.py +65 -29
  40. package/autorest/codegen/serializers/model_generic_serializer.py +9 -10
  41. package/autorest/codegen/serializers/model_init_serializer.py +4 -2
  42. package/autorest/codegen/serializers/model_python3_serializer.py +29 -22
  43. package/autorest/codegen/serializers/operation_groups_serializer.py +21 -18
  44. package/autorest/codegen/serializers/operations_init_serializer.py +23 -11
  45. package/autorest/codegen/serializers/parameter_serializer.py +174 -0
  46. package/autorest/codegen/serializers/patch_serializer.py +14 -2
  47. package/autorest/codegen/serializers/request_builders_serializer.py +57 -0
  48. package/autorest/codegen/serializers/utils.py +0 -103
  49. package/autorest/codegen/templates/MANIFEST.in.jinja2 +1 -0
  50. package/autorest/codegen/templates/{service_client.py.jinja2 → client.py.jinja2} +7 -7
  51. package/autorest/codegen/templates/config.py.jinja2 +13 -13
  52. package/autorest/codegen/templates/enum.py.jinja2 +4 -4
  53. package/autorest/codegen/templates/enum_container.py.jinja2 +1 -2
  54. package/autorest/codegen/templates/init.py.jinja2 +9 -6
  55. package/autorest/codegen/templates/keywords.jinja2 +14 -1
  56. package/autorest/codegen/templates/lro_operation.py.jinja2 +6 -5
  57. package/autorest/codegen/templates/lro_paging_operation.py.jinja2 +6 -5
  58. package/autorest/codegen/templates/metadata.json.jinja2 +36 -35
  59. package/autorest/codegen/templates/model.py.jinja2 +23 -29
  60. package/autorest/codegen/templates/model_container.py.jinja2 +2 -1
  61. package/autorest/codegen/templates/model_init.py.jinja2 +9 -8
  62. package/autorest/codegen/templates/operation.py.jinja2 +10 -15
  63. package/autorest/codegen/templates/operation_group.py.jinja2 +14 -13
  64. package/autorest/codegen/templates/operation_groups_container.py.jinja2 +1 -2
  65. package/autorest/codegen/templates/operation_tools.jinja2 +8 -2
  66. package/autorest/codegen/templates/operations_folder_init.py.jinja2 +4 -0
  67. package/autorest/codegen/templates/paging_operation.py.jinja2 +7 -8
  68. package/autorest/codegen/templates/patch.py.jinja2 +18 -29
  69. package/autorest/codegen/templates/request_builder.py.jinja2 +20 -13
  70. package/autorest/codegen/templates/setup.py.jinja2 +9 -3
  71. package/autorest/codegen/templates/vendor.py.jinja2 +12 -2
  72. package/autorest/jsonrpc/__init__.py +7 -12
  73. package/autorest/jsonrpc/localapi.py +4 -3
  74. package/autorest/jsonrpc/server.py +28 -9
  75. package/autorest/jsonrpc/stdstream.py +13 -6
  76. package/autorest/m2r/__init__.py +5 -8
  77. package/autorest/m4reformatter/__init__.py +1108 -0
  78. package/autorest/multiapi/__init__.py +24 -14
  79. package/autorest/multiapi/models/client.py +21 -11
  80. package/autorest/multiapi/models/code_model.py +23 -10
  81. package/autorest/multiapi/models/config.py +4 -1
  82. package/autorest/multiapi/models/constant_global_parameter.py +1 -0
  83. package/autorest/multiapi/models/global_parameter.py +2 -1
  84. package/autorest/multiapi/models/global_parameters.py +14 -8
  85. package/autorest/multiapi/models/imports.py +35 -18
  86. package/autorest/multiapi/models/mixin_operation.py +5 -5
  87. package/autorest/multiapi/models/operation_group.py +2 -1
  88. package/autorest/multiapi/models/operation_mixin_group.py +21 -10
  89. package/autorest/multiapi/serializers/__init__.py +20 -25
  90. package/autorest/multiapi/serializers/import_serializer.py +47 -15
  91. package/autorest/multiapi/serializers/multiapi_serializer.py +17 -17
  92. package/autorest/multiapi/templates/multiapi_config.py.jinja2 +3 -3
  93. package/autorest/multiapi/templates/multiapi_init.py.jinja2 +2 -2
  94. package/autorest/multiapi/templates/multiapi_operations_mixin.py.jinja2 +4 -4
  95. package/autorest/multiapi/templates/multiapi_service_client.py.jinja2 +9 -9
  96. package/autorest/multiapi/utils.py +3 -3
  97. package/autorest/postprocess/__init__.py +202 -0
  98. package/autorest/postprocess/get_all.py +19 -0
  99. package/autorest/postprocess/venvtools.py +73 -0
  100. package/autorest/preprocess/__init__.py +209 -0
  101. package/autorest/preprocess/helpers.py +54 -0
  102. package/autorest/{namer → preprocess}/python_mappings.py +25 -32
  103. package/package.json +3 -3
  104. package/run-python3.js +2 -3
  105. package/venvtools.py +1 -1
  106. package/autorest/codegen/models/constant_schema.py +0 -97
  107. package/autorest/codegen/models/credential_schema.py +0 -90
  108. package/autorest/codegen/models/credential_schema_policy.py +0 -77
  109. package/autorest/codegen/models/dictionary_schema.py +0 -103
  110. package/autorest/codegen/models/enum_schema.py +0 -246
  111. package/autorest/codegen/models/list_schema.py +0 -113
  112. package/autorest/codegen/models/object_schema.py +0 -249
  113. package/autorest/codegen/models/primitive_schemas.py +0 -476
  114. package/autorest/codegen/models/request_builder_parameter_list.py +0 -280
  115. package/autorest/codegen/models/rest.py +0 -42
  116. package/autorest/codegen/models/schema_request.py +0 -45
  117. package/autorest/codegen/models/schema_response.py +0 -123
  118. package/autorest/codegen/serializers/rest_serializer.py +0 -57
  119. package/autorest/namer/__init__.py +0 -25
  120. package/autorest/namer/name_converter.py +0 -412
@@ -0,0 +1,239 @@
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
+ from typing import Any, Dict, List, Optional, TYPE_CHECKING, cast
7
+
8
+ from autorest.codegen.models.utils import add_to_pylint_disable
9
+ from .base_type import BaseType
10
+ from .property import Property
11
+ from .imports import FileImport, ImportType, TypingSection
12
+
13
+ if TYPE_CHECKING:
14
+ from .code_model import CodeModel
15
+
16
+
17
+ def _get_properties(type: "ModelType", properties: List[Property]) -> List[Property]:
18
+ for parent in type.parents:
19
+ # here we're adding the properties from our parents
20
+
21
+ # need to make sure that the properties we choose from our parent also don't contain
22
+ # any of our own properties
23
+ property_names = set(
24
+ [p.client_name for p in properties]
25
+ + [p.client_name for p in type.properties]
26
+ )
27
+ chosen_parent_properties = [
28
+ p for p in parent.properties if p.client_name not in property_names
29
+ ]
30
+ properties = _get_properties(parent, chosen_parent_properties) + properties
31
+ return properties
32
+
33
+
34
+ class ModelType(BaseType): # pylint: disable=too-many-instance-attributes
35
+ """Represents a class ready to be serialized in Python.
36
+
37
+ :param str name: The name of the class.
38
+ :param str description: The description of the class.
39
+ :param properties: the optional properties of the class.
40
+ :type properties: dict(str, str)
41
+ """
42
+
43
+ def __init__(
44
+ self,
45
+ yaml_data: Dict[str, Any],
46
+ code_model: "CodeModel",
47
+ *,
48
+ properties: Optional[List[Property]] = None,
49
+ parents: Optional[List["ModelType"]] = None,
50
+ discriminated_subtypes: Optional[Dict[str, str]] = None,
51
+ ) -> None:
52
+ super().__init__(yaml_data=yaml_data, code_model=code_model)
53
+ self.name: str = self.yaml_data["name"]
54
+ self.max_properties: Optional[int] = self.yaml_data.get("maxProperties")
55
+ self.min_properties: Optional[int] = self.yaml_data.get("minProperties")
56
+ self.properties = properties or []
57
+ self.parents = parents or []
58
+ self.discriminated_subtypes = discriminated_subtypes or {}
59
+ self.discriminator_value: Optional[str] = self.yaml_data.get(
60
+ "discriminatorValue"
61
+ )
62
+ self._created_json_template_representation = False
63
+ self.is_public: bool = self.yaml_data.get("isPublic", True)
64
+
65
+ @property
66
+ def is_xml(self) -> bool:
67
+ return self.yaml_data.get("isXml", False)
68
+
69
+ @property
70
+ def serialization_type(self) -> str:
71
+ if self.code_model.options["models_mode"]:
72
+ return self.name
73
+ return "object"
74
+
75
+ def type_annotation(self, **kwargs: Any) -> str:
76
+ if self.code_model.options["models_mode"]:
77
+ is_operation_file = kwargs.pop("is_operation_file", False)
78
+ if self.is_public:
79
+ retval = f"_models.{self.name}"
80
+ return retval if is_operation_file else f'"{retval}"'
81
+ return self.name if is_operation_file else f'"{self.name}"'
82
+ return "ET.Element" if self.is_xml else "JSON"
83
+
84
+ def docstring_type(self, **kwargs: Any) -> str:
85
+ if self.code_model.options["models_mode"]:
86
+ return f"~{self.code_model.namespace}.models.{self.name}"
87
+ return "ET.Element" if self.is_xml else "JSON"
88
+
89
+ def description(self, *, is_operation_file: bool = False) -> str:
90
+ return "" if is_operation_file else self.yaml_data.get("description", self.name)
91
+
92
+ def docstring_text(self, **kwargs: Any) -> str:
93
+ if self.code_model.options["models_mode"]:
94
+ return self.name
95
+ return "XML Element" if self.is_xml else "JSON object"
96
+
97
+ def get_declaration(self, value: Any) -> str:
98
+ return f"{self.name}()"
99
+
100
+ def __repr__(self) -> str:
101
+ return f"<{self.__class__.__name__} {self.name}>"
102
+
103
+ @property
104
+ def xml_serialization_ctxt(self) -> Optional[str]:
105
+ # object schema contains _xml_map, they don't need serialization context
106
+ return ""
107
+
108
+ @property
109
+ def xml_map_content(self) -> Optional[str]:
110
+ # This is NOT an error on the super call, we use the serialization context for "xml_map",
111
+ # but we don't want to write a serialization context for an object.
112
+ return super().xml_serialization_ctxt
113
+
114
+ def get_json_template_representation(
115
+ self,
116
+ *,
117
+ optional: bool = True,
118
+ client_default_value_declaration: Optional[str] = None,
119
+ description: Optional[str] = None,
120
+ ) -> Any:
121
+ if self._created_json_template_representation:
122
+ return "..." # do this to avoid loop
123
+ self._created_json_template_representation = True
124
+ # don't add additional properties, because there's not really a concept of
125
+ # additional properties in the template
126
+ representation = {
127
+ f'"{prop.rest_api_name}"': prop.get_json_template_representation(
128
+ optional=optional,
129
+ client_default_value_declaration=client_default_value_declaration,
130
+ description=description,
131
+ )
132
+ for prop in [
133
+ p
134
+ for p in self.properties
135
+ if not (p.is_discriminator or p.client_name == "additional_properties")
136
+ ]
137
+ }
138
+ try:
139
+ # add discriminator prop if there is one
140
+ discriminator = next(p for p in self.properties if p.is_discriminator)
141
+ representation[discriminator.rest_api_name] = (
142
+ self.discriminator_value or discriminator.rest_api_name
143
+ )
144
+ except StopIteration:
145
+ pass
146
+
147
+ # once we've finished, we want to reset created_json_template_representation to false
148
+ # so we can call it again
149
+ self._created_json_template_representation = False
150
+ return representation
151
+
152
+ @classmethod
153
+ def from_yaml(
154
+ cls, yaml_data: Dict[str, Any], code_model: "CodeModel"
155
+ ) -> "ModelType":
156
+ raise ValueError(
157
+ "You shouldn't call from_yaml for ModelType to avoid recursion. "
158
+ "Please initial a blank ModelType, then call .fill_instance_from_yaml on the created type."
159
+ )
160
+
161
+ def fill_instance_from_yaml(
162
+ self, yaml_data: Dict[str, Any], code_model: "CodeModel"
163
+ ) -> None:
164
+ from . import build_type
165
+
166
+ self.parents = [
167
+ cast(ModelType, build_type(bm, code_model))
168
+ for bm in yaml_data.get("parents", [])
169
+ ]
170
+ properties = [
171
+ Property.from_yaml(p, code_model) for p in yaml_data["properties"]
172
+ ]
173
+ self.properties = _get_properties(self, properties)
174
+ # checking to see if this is a polymorphic class
175
+ self.discriminated_subtypes = self.yaml_data.get("discriminatedSubtypes", {})
176
+
177
+ @property
178
+ def has_readonly_or_constant_property(self) -> bool:
179
+ return any(x.readonly or x.constant for x in self.properties)
180
+
181
+ @property
182
+ def discriminator(self) -> Optional[Property]:
183
+ try:
184
+ return next(p for p in self.properties if p.is_discriminator)
185
+ except StopIteration:
186
+ return None
187
+
188
+ @property
189
+ def instance_check_template(self) -> str:
190
+ if self.code_model.options["models_mode"]:
191
+ return "isinstance({}, msrest.Model)"
192
+ return "isinstance({}, MutableMapping)"
193
+
194
+ @property
195
+ def pylint_disable(self) -> str:
196
+ retval: str = ""
197
+ if len(self.properties) > 10:
198
+ retval = add_to_pylint_disable(retval, "too-many-instance-attributes")
199
+ return retval
200
+
201
+ @property
202
+ def init_pylint_disable(self) -> str:
203
+ retval: str = ""
204
+ if len(self.properties) > 23:
205
+ retval = add_to_pylint_disable(retval, "too-many-locals")
206
+ return retval
207
+
208
+ def imports(self, **kwargs: Any) -> FileImport:
209
+ file_import = FileImport()
210
+ relative_path = kwargs.pop("relative_path", None)
211
+ if self.code_model.options["models_mode"] and relative_path:
212
+ # add import for models in operations file
213
+ if self.is_public:
214
+ file_import.add_submodule_import(
215
+ relative_path, "models", ImportType.LOCAL, alias="_models"
216
+ )
217
+ else:
218
+ # a little hacky, but we only do this for version tolerant
219
+ # models files, which are all python3 only
220
+ models_filename = self.code_model.get_models_filename(
221
+ is_python3_file=True
222
+ )
223
+ file_import.add_submodule_import(
224
+ f"{relative_path}models.{models_filename}",
225
+ self.name,
226
+ ImportType.LOCAL,
227
+ )
228
+ if self.code_model.options["models_mode"]:
229
+ return file_import
230
+ file_import.add_submodule_import(
231
+ "typing", "Any", ImportType.STDLIB, TypingSection.CONDITIONAL
232
+ )
233
+ file_import.add_import("sys", ImportType.STDLIB)
234
+ file_import.define_mutable_mapping_type()
235
+ if self.is_xml:
236
+ file_import.add_submodule_import(
237
+ "xml.etree", "ElementTree", ImportType.STDLIB, alias="ET"
238
+ )
239
+ return file_import