@autorest/python 6.2.0 → 6.2.2
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/autorest/codegen/__init__.py +9 -74
- package/autorest/codegen/models/__init__.py +11 -12
- package/autorest/codegen/models/{base_type.py → base.py} +29 -10
- package/autorest/codegen/models/base_builder.py +8 -10
- package/autorest/codegen/models/client.py +111 -40
- package/autorest/codegen/models/code_model.py +131 -90
- package/autorest/codegen/models/combined_type.py +7 -7
- package/autorest/codegen/models/constant_type.py +44 -10
- package/autorest/codegen/models/credential_types.py +17 -19
- package/autorest/codegen/models/dictionary_type.py +7 -9
- package/autorest/codegen/models/enum_type.py +19 -24
- package/autorest/codegen/models/imports.py +82 -33
- package/autorest/codegen/models/list_type.py +11 -15
- package/autorest/codegen/models/lro_operation.py +6 -10
- package/autorest/codegen/models/model_type.py +20 -21
- package/autorest/codegen/models/operation.py +49 -42
- package/autorest/codegen/models/operation_group.py +11 -11
- package/autorest/codegen/models/paging_operation.py +9 -11
- package/autorest/codegen/models/parameter.py +48 -32
- package/autorest/codegen/models/parameter_list.py +16 -22
- package/autorest/codegen/models/primitive_types.py +12 -23
- package/autorest/codegen/models/property.py +11 -11
- package/autorest/codegen/models/request_builder.py +19 -19
- package/autorest/codegen/models/request_builder_parameter.py +14 -16
- package/autorest/codegen/models/response.py +24 -28
- package/autorest/codegen/serializers/__init__.py +202 -187
- package/autorest/codegen/serializers/builder_serializer.py +49 -50
- package/autorest/codegen/serializers/client_serializer.py +9 -7
- package/autorest/codegen/serializers/enum_serializer.py +4 -4
- package/autorest/codegen/serializers/general_serializer.py +26 -36
- package/autorest/codegen/serializers/import_serializer.py +30 -36
- package/autorest/codegen/serializers/metadata_serializer.py +35 -17
- package/autorest/codegen/serializers/model_init_serializer.py +6 -10
- package/autorest/codegen/serializers/model_serializer.py +42 -27
- package/autorest/codegen/serializers/operation_groups_serializer.py +12 -8
- package/autorest/codegen/serializers/operations_init_serializer.py +13 -6
- package/autorest/codegen/serializers/patch_serializer.py +4 -4
- package/autorest/codegen/serializers/request_builders_serializer.py +6 -6
- package/autorest/codegen/serializers/sample_serializer.py +10 -12
- package/autorest/codegen/templates/client_container.py.jinja2 +2 -2
- package/autorest/codegen/templates/config.py.jinja2 +1 -1
- package/autorest/codegen/templates/config_container.py.jinja2 +3 -3
- package/autorest/codegen/templates/enum_container.py.jinja2 +2 -2
- package/autorest/codegen/templates/init.py.jinja2 +2 -2
- package/autorest/codegen/templates/lro_operation.py.jinja2 +1 -1
- package/autorest/codegen/templates/lro_paging_operation.py.jinja2 +1 -1
- package/autorest/codegen/templates/metadata.json.jinja2 +2 -2
- package/autorest/codegen/templates/model_container.py.jinja2 +3 -3
- package/autorest/codegen/templates/model_dpg.py.jinja2 +4 -4
- package/autorest/codegen/templates/model_init.py.jinja2 +3 -3
- package/autorest/codegen/templates/operation.py.jinja2 +1 -1
- package/autorest/codegen/templates/operation_group.py.jinja2 +2 -2
- package/autorest/codegen/templates/operation_groups_container.py.jinja2 +3 -3
- package/autorest/codegen/templates/operation_tools.jinja2 +3 -3
- package/autorest/codegen/templates/operations_folder_init.py.jinja2 +4 -4
- package/autorest/codegen/templates/packaging_templates/setup.py.jinja2 +4 -1
- package/autorest/codegen/templates/paging_operation.py.jinja2 +1 -1
- package/autorest/codegen/templates/request_builder.py.jinja2 +1 -1
- package/autorest/codegen/templates/request_builders.py.jinja2 +2 -2
- package/autorest/codegen/templates/rest_init.py.jinja2 +1 -1
- package/autorest/codegen/templates/sample.py.jinja2 +5 -5
- package/autorest/codegen/templates/validation.py.jinja2 +1 -1
- package/autorest/codegen/templates/vendor.py.jinja2 +6 -6
- package/autorest/codegen/templates/version.py.jinja2 +2 -2
- package/autorest/m4reformatter/__init__.py +57 -47
- package/autorest/multiapi/models/imports.py +89 -18
- package/autorest/multiapi/serializers/import_serializer.py +88 -7
- package/autorest/multiapi/utils.py +6 -0
- package/autorest/preprocess/__init__.py +23 -26
- package/package.json +1 -1
- package/run_cadl.py +0 -1
- package/autorest/cadlflags/__init__.py +0 -130
- package/autorest/codegen/models/base_model.py +0 -30
|
@@ -5,12 +5,12 @@
|
|
|
5
5
|
# --------------------------------------------------------------------------
|
|
6
6
|
from typing import Any, Dict, List, TYPE_CHECKING, Optional
|
|
7
7
|
|
|
8
|
-
from .
|
|
8
|
+
from .base import BaseType
|
|
9
9
|
from .imports import FileImport, ImportType, TypingSection
|
|
10
|
-
from .
|
|
10
|
+
from .base import BaseModel
|
|
11
11
|
|
|
12
12
|
if TYPE_CHECKING:
|
|
13
|
-
from .code_model import
|
|
13
|
+
from .code_model import CodeModel
|
|
14
14
|
|
|
15
15
|
|
|
16
16
|
class EnumValue(BaseModel):
|
|
@@ -21,17 +21,15 @@ class EnumValue(BaseModel):
|
|
|
21
21
|
:param str description: Optional. The description for this enum value
|
|
22
22
|
"""
|
|
23
23
|
|
|
24
|
-
def __init__(
|
|
25
|
-
|
|
26
|
-
) -> None:
|
|
27
|
-
super().__init__(yaml_data=yaml_data, namespace_model=namespace_model)
|
|
24
|
+
def __init__(self, yaml_data: Dict[str, Any], code_model: "CodeModel") -> None:
|
|
25
|
+
super().__init__(yaml_data=yaml_data, code_model=code_model)
|
|
28
26
|
self.name: str = self.yaml_data["name"]
|
|
29
27
|
self.value: str = self.yaml_data["value"]
|
|
30
28
|
self.description: Optional[str] = self.yaml_data.get("description")
|
|
31
29
|
|
|
32
30
|
@classmethod
|
|
33
31
|
def from_yaml(
|
|
34
|
-
cls, yaml_data: Dict[str, Any],
|
|
32
|
+
cls, yaml_data: Dict[str, Any], code_model: "CodeModel"
|
|
35
33
|
) -> "EnumValue":
|
|
36
34
|
"""Constructs an EnumValue from yaml data.
|
|
37
35
|
|
|
@@ -43,7 +41,7 @@ class EnumValue(BaseModel):
|
|
|
43
41
|
"""
|
|
44
42
|
return cls(
|
|
45
43
|
yaml_data=yaml_data,
|
|
46
|
-
|
|
44
|
+
code_model=code_model,
|
|
47
45
|
)
|
|
48
46
|
|
|
49
47
|
|
|
@@ -62,11 +60,11 @@ class EnumType(BaseType):
|
|
|
62
60
|
def __init__(
|
|
63
61
|
self,
|
|
64
62
|
yaml_data: Dict[str, Any],
|
|
65
|
-
|
|
63
|
+
code_model: "CodeModel",
|
|
66
64
|
values: List["EnumValue"],
|
|
67
65
|
value_type: BaseType,
|
|
68
66
|
) -> None:
|
|
69
|
-
super().__init__(yaml_data=yaml_data,
|
|
67
|
+
super().__init__(yaml_data=yaml_data, code_model=code_model)
|
|
70
68
|
self.name: str = yaml_data["name"]
|
|
71
69
|
self.values = values
|
|
72
70
|
self.value_type = value_type
|
|
@@ -108,7 +106,7 @@ class EnumType(BaseType):
|
|
|
108
106
|
:return: The type annotation for this schema
|
|
109
107
|
:rtype: str
|
|
110
108
|
"""
|
|
111
|
-
if self.
|
|
109
|
+
if self.code_model.options["models_mode"]:
|
|
112
110
|
model_name = f"_models.{self.name}"
|
|
113
111
|
# we don't need quoted annotation in operation files, and need it in model folder files.
|
|
114
112
|
if not kwargs.get("is_operation_file", False):
|
|
@@ -121,17 +119,15 @@ class EnumType(BaseType):
|
|
|
121
119
|
return self.value_type.get_declaration(value)
|
|
122
120
|
|
|
123
121
|
def docstring_text(self, **kwargs: Any) -> str:
|
|
124
|
-
if self.
|
|
122
|
+
if self.code_model.options["models_mode"]:
|
|
125
123
|
return self.name
|
|
126
124
|
return self.value_type.type_annotation(**kwargs)
|
|
127
125
|
|
|
128
126
|
def docstring_type(self, **kwargs: Any) -> str:
|
|
129
127
|
"""The python type used for RST syntax input and type annotation."""
|
|
130
|
-
if self.
|
|
128
|
+
if self.code_model.options["models_mode"]:
|
|
131
129
|
type_annotation = self.value_type.type_annotation(**kwargs)
|
|
132
|
-
enum_type_annotation =
|
|
133
|
-
f"{self.namespace_model.namespace}.models.{self.name}"
|
|
134
|
-
)
|
|
130
|
+
enum_type_annotation = f"{self.code_model.namespace}.models.{self.name}"
|
|
135
131
|
return f"{type_annotation} or ~{enum_type_annotation}"
|
|
136
132
|
return self.value_type.type_annotation(**kwargs)
|
|
137
133
|
|
|
@@ -155,7 +151,7 @@ class EnumType(BaseType):
|
|
|
155
151
|
|
|
156
152
|
@classmethod
|
|
157
153
|
def from_yaml(
|
|
158
|
-
cls, yaml_data: Dict[str, Any],
|
|
154
|
+
cls, yaml_data: Dict[str, Any], code_model: "CodeModel"
|
|
159
155
|
) -> "EnumType":
|
|
160
156
|
"""Constructs an EnumType from yaml data.
|
|
161
157
|
|
|
@@ -169,18 +165,17 @@ class EnumType(BaseType):
|
|
|
169
165
|
|
|
170
166
|
return cls(
|
|
171
167
|
yaml_data=yaml_data,
|
|
172
|
-
|
|
173
|
-
value_type=build_type(yaml_data["valueType"],
|
|
168
|
+
code_model=code_model,
|
|
169
|
+
value_type=build_type(yaml_data["valueType"], code_model),
|
|
174
170
|
values=[
|
|
175
|
-
EnumValue.from_yaml(value,
|
|
176
|
-
for value in yaml_data["values"]
|
|
171
|
+
EnumValue.from_yaml(value, code_model) for value in yaml_data["values"]
|
|
177
172
|
],
|
|
178
173
|
)
|
|
179
174
|
|
|
180
175
|
def imports(self, **kwargs: Any) -> FileImport:
|
|
181
176
|
is_operation_file = kwargs.pop("is_operation_file", False)
|
|
182
177
|
file_import = FileImport()
|
|
183
|
-
if self.
|
|
178
|
+
if self.code_model.options["models_mode"]:
|
|
184
179
|
file_import.add_submodule_import(
|
|
185
180
|
"typing", "Union", ImportType.STDLIB, TypingSection.CONDITIONAL
|
|
186
181
|
)
|
|
@@ -196,7 +191,7 @@ class EnumType(BaseType):
|
|
|
196
191
|
self.value_type.imports(is_operation_file=is_operation_file, **kwargs)
|
|
197
192
|
)
|
|
198
193
|
relative_path = kwargs.pop("relative_path", None)
|
|
199
|
-
if self.
|
|
194
|
+
if self.code_model.options["models_mode"] and relative_path:
|
|
200
195
|
# add import for enums in operations file
|
|
201
196
|
file_import.add_submodule_import(
|
|
202
197
|
relative_path, "models", ImportType.LOCAL, alias="_models"
|
|
@@ -4,10 +4,10 @@
|
|
|
4
4
|
# license information.
|
|
5
5
|
# --------------------------------------------------------------------------
|
|
6
6
|
from enum import Enum, auto
|
|
7
|
-
from typing import Dict, List, Optional, Tuple, Union, Set,
|
|
7
|
+
from typing import Dict, List, Optional, Tuple, Union, Set, TYPE_CHECKING
|
|
8
8
|
|
|
9
9
|
if TYPE_CHECKING:
|
|
10
|
-
from .code_model import
|
|
10
|
+
from .code_model import CodeModel
|
|
11
11
|
|
|
12
12
|
|
|
13
13
|
class ImportType(str, Enum):
|
|
@@ -15,6 +15,7 @@ class ImportType(str, Enum):
|
|
|
15
15
|
THIRDPARTY = "thirdparty"
|
|
16
16
|
AZURECORE = "azurecore"
|
|
17
17
|
LOCAL = "local"
|
|
18
|
+
BYVERSION = "by_version"
|
|
18
19
|
|
|
19
20
|
|
|
20
21
|
class TypingSection(str, Enum):
|
|
@@ -42,12 +43,19 @@ class ImportModel:
|
|
|
42
43
|
*,
|
|
43
44
|
submodule_name: Optional[str] = None,
|
|
44
45
|
alias: Optional[str] = None,
|
|
46
|
+
version_modules: Optional[
|
|
47
|
+
Tuple[Tuple[Tuple[int, int], str, Optional[str]]]
|
|
48
|
+
] = None,
|
|
45
49
|
):
|
|
46
50
|
self.typing_section = typing_section
|
|
47
51
|
self.import_type = import_type
|
|
48
52
|
self.module_name = module_name
|
|
49
53
|
self.submodule_name = submodule_name
|
|
50
54
|
self.alias = alias
|
|
55
|
+
# version_modules: this field is for imports submodule from specified module by python version.
|
|
56
|
+
# It's a list of "python version, module_name, comments".
|
|
57
|
+
# The python version is in form of (major, minor), for instance (3, 9) stands for py3.9.
|
|
58
|
+
self.version_modules = version_modules
|
|
51
59
|
|
|
52
60
|
def __eq__(self, other):
|
|
53
61
|
try:
|
|
@@ -74,14 +82,9 @@ class TypeDefinition:
|
|
|
74
82
|
self,
|
|
75
83
|
sync_definition: str,
|
|
76
84
|
async_definition: str,
|
|
77
|
-
version_imports: Mapping[Optional[Tuple[int, int]], ImportModel] = None,
|
|
78
85
|
):
|
|
79
|
-
# version_imports: a map of "python version -> ImportModel".
|
|
80
|
-
# The python version is in form of (major, minor), for instance (3, 9) stands for py3.9.
|
|
81
|
-
# If the python version is None, it's a default ImportModel.
|
|
82
86
|
self.sync_definition = sync_definition
|
|
83
87
|
self.async_definition = async_definition
|
|
84
|
-
self.version_imports = version_imports
|
|
85
88
|
|
|
86
89
|
|
|
87
90
|
class FileImport:
|
|
@@ -114,6 +117,9 @@ class FileImport:
|
|
|
114
117
|
import_type: ImportType,
|
|
115
118
|
typing_section: TypingSection = TypingSection.REGULAR,
|
|
116
119
|
alias: Optional[str] = None,
|
|
120
|
+
version_modules: Optional[
|
|
121
|
+
Tuple[Tuple[Tuple[int, int], str, Optional[str]]]
|
|
122
|
+
] = None,
|
|
117
123
|
) -> None:
|
|
118
124
|
"""Add an import to this import block."""
|
|
119
125
|
self._append_import(
|
|
@@ -123,6 +129,7 @@ class FileImport:
|
|
|
123
129
|
module_name=module_name,
|
|
124
130
|
submodule_name=submodule_name,
|
|
125
131
|
alias=alias,
|
|
132
|
+
version_modules=version_modules,
|
|
126
133
|
)
|
|
127
134
|
)
|
|
128
135
|
|
|
@@ -148,10 +155,9 @@ class FileImport:
|
|
|
148
155
|
type_name: str,
|
|
149
156
|
type_value: str,
|
|
150
157
|
async_type_value: Optional[str] = None,
|
|
151
|
-
version_imports: Mapping[Optional[Tuple[int, int]], ImportModel] = None,
|
|
152
158
|
):
|
|
153
159
|
self.type_definitions[type_name] = TypeDefinition(
|
|
154
|
-
type_value, async_type_value or type_value
|
|
160
|
+
type_value, async_type_value or type_value
|
|
155
161
|
)
|
|
156
162
|
|
|
157
163
|
def merge(self, file_import: "FileImport") -> None:
|
|
@@ -162,24 +168,18 @@ class FileImport:
|
|
|
162
168
|
|
|
163
169
|
def define_mutable_mapping_type(self) -> None:
|
|
164
170
|
"""Helper function for defining the mutable mapping type"""
|
|
171
|
+
self.add_import("sys", ImportType.STDLIB)
|
|
172
|
+
self.add_submodule_import(
|
|
173
|
+
"typing",
|
|
174
|
+
"MutableMapping",
|
|
175
|
+
ImportType.BYVERSION,
|
|
176
|
+
TypingSection.REGULAR,
|
|
177
|
+
None,
|
|
178
|
+
(((3, 9), "collections.abc", None),),
|
|
179
|
+
)
|
|
165
180
|
self.define_mypy_type(
|
|
166
181
|
"JSON",
|
|
167
182
|
"MutableMapping[str, Any] # pylint: disable=unsubscriptable-object",
|
|
168
|
-
None,
|
|
169
|
-
{
|
|
170
|
-
(3, 9): ImportModel(
|
|
171
|
-
TypingSection.CONDITIONAL,
|
|
172
|
-
ImportType.STDLIB,
|
|
173
|
-
"collections.abc",
|
|
174
|
-
submodule_name="MutableMapping",
|
|
175
|
-
),
|
|
176
|
-
None: ImportModel(
|
|
177
|
-
TypingSection.CONDITIONAL,
|
|
178
|
-
ImportType.STDLIB,
|
|
179
|
-
"typing",
|
|
180
|
-
submodule_name="MutableMapping",
|
|
181
|
-
),
|
|
182
|
-
},
|
|
183
183
|
)
|
|
184
184
|
self.add_submodule_import("typing", "Any", ImportType.STDLIB)
|
|
185
185
|
|
|
@@ -187,18 +187,67 @@ class FileImport:
|
|
|
187
187
|
self,
|
|
188
188
|
) -> Dict[
|
|
189
189
|
TypingSection,
|
|
190
|
-
Dict[
|
|
190
|
+
Dict[
|
|
191
|
+
ImportType,
|
|
192
|
+
Dict[
|
|
193
|
+
str,
|
|
194
|
+
Set[
|
|
195
|
+
Optional[
|
|
196
|
+
Union[
|
|
197
|
+
str,
|
|
198
|
+
Tuple[str, str],
|
|
199
|
+
Tuple[
|
|
200
|
+
str,
|
|
201
|
+
Optional[str],
|
|
202
|
+
Tuple[Tuple[Tuple[int, int], str, Optional[str]]],
|
|
203
|
+
],
|
|
204
|
+
]
|
|
205
|
+
]
|
|
206
|
+
],
|
|
207
|
+
],
|
|
208
|
+
],
|
|
191
209
|
]:
|
|
192
210
|
retval: Dict[
|
|
193
211
|
TypingSection,
|
|
194
|
-
Dict[
|
|
212
|
+
Dict[
|
|
213
|
+
ImportType,
|
|
214
|
+
Dict[
|
|
215
|
+
str,
|
|
216
|
+
Set[
|
|
217
|
+
Optional[
|
|
218
|
+
Union[
|
|
219
|
+
str,
|
|
220
|
+
Tuple[str, str],
|
|
221
|
+
Tuple[
|
|
222
|
+
str,
|
|
223
|
+
Optional[str],
|
|
224
|
+
Tuple[Tuple[Tuple[int, int], str, Optional[str]]],
|
|
225
|
+
],
|
|
226
|
+
]
|
|
227
|
+
]
|
|
228
|
+
],
|
|
229
|
+
],
|
|
230
|
+
],
|
|
195
231
|
] = dict()
|
|
196
232
|
for i in self.imports:
|
|
197
|
-
name_import: Optional[
|
|
233
|
+
name_import: Optional[
|
|
234
|
+
Union[
|
|
235
|
+
str,
|
|
236
|
+
Tuple[str, str],
|
|
237
|
+
Tuple[
|
|
238
|
+
str,
|
|
239
|
+
Optional[str],
|
|
240
|
+
Tuple[Tuple[Tuple[int, int], str, Optional[str]]],
|
|
241
|
+
],
|
|
242
|
+
]
|
|
243
|
+
] = None
|
|
198
244
|
if i.submodule_name:
|
|
199
|
-
|
|
200
|
-
(i.submodule_name, i.alias
|
|
201
|
-
|
|
245
|
+
if i.version_modules:
|
|
246
|
+
name_import = (i.submodule_name, i.alias, i.version_modules)
|
|
247
|
+
elif i.alias:
|
|
248
|
+
name_import = (i.submodule_name, i.alias)
|
|
249
|
+
else:
|
|
250
|
+
name_import = i.submodule_name
|
|
202
251
|
retval.setdefault(i.typing_section, dict()).setdefault(
|
|
203
252
|
i.import_type, dict()
|
|
204
253
|
).setdefault(i.module_name, set()).add(name_import)
|
|
@@ -206,12 +255,12 @@ class FileImport:
|
|
|
206
255
|
|
|
207
256
|
def add_msrest_import(
|
|
208
257
|
self,
|
|
209
|
-
|
|
258
|
+
code_model: "CodeModel",
|
|
210
259
|
relative_path: str,
|
|
211
260
|
msrest_import_type: MsrestImportType,
|
|
212
261
|
typing_section: TypingSection,
|
|
213
262
|
):
|
|
214
|
-
if
|
|
263
|
+
if code_model.options["client_side_validation"]:
|
|
215
264
|
if msrest_import_type == MsrestImportType.Module:
|
|
216
265
|
self.add_import(
|
|
217
266
|
"msrest.serialization", ImportType.AZURECORE, typing_section
|
|
@@ -225,7 +274,7 @@ class FileImport:
|
|
|
225
274
|
"msrest", "Deserializer", ImportType.THIRDPARTY, typing_section
|
|
226
275
|
)
|
|
227
276
|
else:
|
|
228
|
-
if
|
|
277
|
+
if code_model.options["multiapi"]:
|
|
229
278
|
relative_path += "."
|
|
230
279
|
if msrest_import_type == MsrestImportType.Module:
|
|
231
280
|
self.add_submodule_import(
|
|
@@ -4,11 +4,11 @@
|
|
|
4
4
|
# license information.
|
|
5
5
|
# --------------------------------------------------------------------------
|
|
6
6
|
from typing import Any, Dict, Optional, Union, TYPE_CHECKING, List
|
|
7
|
-
from .
|
|
7
|
+
from .base import BaseType
|
|
8
8
|
from .imports import FileImport, ImportType, TypingSection
|
|
9
9
|
|
|
10
10
|
if TYPE_CHECKING:
|
|
11
|
-
from .code_model import
|
|
11
|
+
from .code_model import CodeModel
|
|
12
12
|
from .model_type import ModelType
|
|
13
13
|
|
|
14
14
|
|
|
@@ -16,10 +16,10 @@ class ListType(BaseType):
|
|
|
16
16
|
def __init__(
|
|
17
17
|
self,
|
|
18
18
|
yaml_data: Dict[str, Any],
|
|
19
|
-
|
|
19
|
+
code_model: "CodeModel",
|
|
20
20
|
element_type: BaseType,
|
|
21
21
|
) -> None:
|
|
22
|
-
super().__init__(yaml_data=yaml_data,
|
|
22
|
+
super().__init__(yaml_data=yaml_data, code_model=code_model)
|
|
23
23
|
self.element_type = element_type
|
|
24
24
|
self.max_items: Optional[int] = yaml_data.get("maxItems")
|
|
25
25
|
self.min_items: Optional[int] = yaml_data.get("minItems")
|
|
@@ -30,10 +30,7 @@ class ListType(BaseType):
|
|
|
30
30
|
return f"[{self.element_type.serialization_type}]"
|
|
31
31
|
|
|
32
32
|
def type_annotation(self, **kwargs: Any) -> str:
|
|
33
|
-
if
|
|
34
|
-
self.namespace_model.options["version_tolerant"]
|
|
35
|
-
and self.element_type.is_xml
|
|
36
|
-
):
|
|
33
|
+
if self.code_model.options["version_tolerant"] and self.element_type.is_xml:
|
|
37
34
|
# this means we're version tolerant XML, we just return the XML element
|
|
38
35
|
return self.element_type.type_annotation(**kwargs)
|
|
39
36
|
return f"List[{self.element_type.type_annotation(**kwargs)}]"
|
|
@@ -65,7 +62,7 @@ class ListType(BaseType):
|
|
|
65
62
|
|
|
66
63
|
def docstring_type(self, **kwargs: Any) -> str:
|
|
67
64
|
if (
|
|
68
|
-
self.
|
|
65
|
+
self.code_model.options["version_tolerant"]
|
|
69
66
|
and self.element_type.xml_metadata
|
|
70
67
|
):
|
|
71
68
|
# this means we're version tolerant XML, we just return the XML element
|
|
@@ -74,7 +71,7 @@ class ListType(BaseType):
|
|
|
74
71
|
|
|
75
72
|
def docstring_text(self, **kwargs: Any) -> str:
|
|
76
73
|
if (
|
|
77
|
-
self.
|
|
74
|
+
self.code_model.options["version_tolerant"]
|
|
78
75
|
and self.element_type.xml_metadata
|
|
79
76
|
):
|
|
80
77
|
# this means we're version tolerant XML, we just return the XML element
|
|
@@ -128,23 +125,22 @@ class ListType(BaseType):
|
|
|
128
125
|
|
|
129
126
|
@classmethod
|
|
130
127
|
def from_yaml(
|
|
131
|
-
cls, yaml_data: Dict[str, Any],
|
|
128
|
+
cls, yaml_data: Dict[str, Any], code_model: "CodeModel"
|
|
132
129
|
) -> "ListType":
|
|
133
130
|
from . import build_type
|
|
134
131
|
|
|
135
132
|
return cls(
|
|
136
133
|
yaml_data=yaml_data,
|
|
137
|
-
|
|
134
|
+
code_model=code_model,
|
|
138
135
|
element_type=build_type(
|
|
139
|
-
yaml_data=yaml_data["elementType"],
|
|
136
|
+
yaml_data=yaml_data["elementType"], code_model=code_model
|
|
140
137
|
),
|
|
141
138
|
)
|
|
142
139
|
|
|
143
140
|
def imports(self, **kwargs: Any) -> FileImport:
|
|
144
141
|
file_import = FileImport()
|
|
145
142
|
if not (
|
|
146
|
-
self.
|
|
147
|
-
and self.element_type.is_xml
|
|
143
|
+
self.code_model.options["version_tolerant"] and self.element_type.is_xml
|
|
148
144
|
):
|
|
149
145
|
file_import.add_submodule_import(
|
|
150
146
|
"typing", "List", ImportType.STDLIB, TypingSection.CONDITIONAL
|
|
@@ -12,7 +12,7 @@ from .request_builder import RequestBuilder
|
|
|
12
12
|
from .parameter_list import ParameterList
|
|
13
13
|
|
|
14
14
|
if TYPE_CHECKING:
|
|
15
|
-
from .code_model import
|
|
15
|
+
from .code_model import CodeModel
|
|
16
16
|
from .client import Client
|
|
17
17
|
|
|
18
18
|
LROResponseType = TypeVar(
|
|
@@ -24,7 +24,7 @@ class LROOperationBase(OperationBase[LROResponseType]):
|
|
|
24
24
|
def __init__(
|
|
25
25
|
self,
|
|
26
26
|
yaml_data: Dict[str, Any],
|
|
27
|
-
|
|
27
|
+
code_model: "CodeModel",
|
|
28
28
|
client: "Client",
|
|
29
29
|
name: str,
|
|
30
30
|
request_builder: RequestBuilder,
|
|
@@ -37,7 +37,7 @@ class LROOperationBase(OperationBase[LROResponseType]):
|
|
|
37
37
|
want_tracing: bool = True,
|
|
38
38
|
) -> None:
|
|
39
39
|
super().__init__(
|
|
40
|
-
|
|
40
|
+
code_model=code_model,
|
|
41
41
|
client=client,
|
|
42
42
|
yaml_data=yaml_data,
|
|
43
43
|
name=name,
|
|
@@ -92,18 +92,14 @@ class LROOperationBase(OperationBase[LROResponseType]):
|
|
|
92
92
|
"""Initial operation that creates the first call for LRO polling"""
|
|
93
93
|
return Operation(
|
|
94
94
|
yaml_data=self.yaml_data,
|
|
95
|
-
|
|
95
|
+
code_model=self.code_model,
|
|
96
96
|
client=self.client,
|
|
97
|
-
request_builder=self.
|
|
98
|
-
id(self.yaml_data)
|
|
99
|
-
),
|
|
97
|
+
request_builder=self.client.lookup_request_builder(id(self.yaml_data)),
|
|
100
98
|
name=self.name[5:] + "_initial",
|
|
101
99
|
overloads=self.overloads,
|
|
102
100
|
parameters=self.parameters,
|
|
103
101
|
responses=[
|
|
104
|
-
Response(
|
|
105
|
-
r.yaml_data, self.namespace_model, headers=r.headers, type=r.type
|
|
106
|
-
)
|
|
102
|
+
Response(r.yaml_data, self.code_model, headers=r.headers, type=r.type)
|
|
107
103
|
for r in self.responses
|
|
108
104
|
],
|
|
109
105
|
exceptions=self.exceptions,
|
|
@@ -7,13 +7,13 @@ from collections import OrderedDict
|
|
|
7
7
|
from typing import Any, Dict, List, Optional, TYPE_CHECKING, cast
|
|
8
8
|
|
|
9
9
|
from autorest.codegen.models.utils import add_to_pylint_disable
|
|
10
|
-
from .
|
|
10
|
+
from .base import BaseType
|
|
11
11
|
from .constant_type import ConstantType
|
|
12
12
|
from .property import Property
|
|
13
13
|
from .imports import FileImport, ImportType, TypingSection
|
|
14
14
|
|
|
15
15
|
if TYPE_CHECKING:
|
|
16
|
-
from .code_model import
|
|
16
|
+
from .code_model import CodeModel
|
|
17
17
|
|
|
18
18
|
|
|
19
19
|
def _get_properties(type: "ModelType", properties: List[Property]) -> List[Property]:
|
|
@@ -47,13 +47,13 @@ class ModelType(
|
|
|
47
47
|
def __init__(
|
|
48
48
|
self,
|
|
49
49
|
yaml_data: Dict[str, Any],
|
|
50
|
-
|
|
50
|
+
code_model: "CodeModel",
|
|
51
51
|
*,
|
|
52
52
|
properties: Optional[List[Property]] = None,
|
|
53
53
|
parents: Optional[List["ModelType"]] = None,
|
|
54
54
|
discriminated_subtypes: Optional[Dict[str, "ModelType"]] = None,
|
|
55
55
|
) -> None:
|
|
56
|
-
super().__init__(yaml_data=yaml_data,
|
|
56
|
+
super().__init__(yaml_data=yaml_data, code_model=code_model)
|
|
57
57
|
self.name: str = self.yaml_data["name"]
|
|
58
58
|
self.max_properties: Optional[int] = self.yaml_data.get("maxProperties")
|
|
59
59
|
self.min_properties: Optional[int] = self.yaml_data.get("minProperties")
|
|
@@ -74,10 +74,10 @@ class ModelType(
|
|
|
74
74
|
|
|
75
75
|
@property
|
|
76
76
|
def serialization_type(self) -> str:
|
|
77
|
-
if self.
|
|
78
|
-
private_model_path = f"_models.{self.
|
|
77
|
+
if self.code_model.options["models_mode"] == "msrest":
|
|
78
|
+
private_model_path = f"_models.{self.code_model.models_filename}."
|
|
79
79
|
return f"{'' if self.is_public else private_model_path}{self.name}"
|
|
80
|
-
if self.
|
|
80
|
+
if self.code_model.options["models_mode"] == "dpg":
|
|
81
81
|
return f"{'' if self.is_public else '_models.'}_models.{self.name}"
|
|
82
82
|
return "object"
|
|
83
83
|
|
|
@@ -86,24 +86,24 @@ class ModelType(
|
|
|
86
86
|
return any(p.is_polymorphic for p in self.properties)
|
|
87
87
|
|
|
88
88
|
def type_annotation(self, **kwargs: Any) -> str:
|
|
89
|
-
if self.
|
|
89
|
+
if self.code_model.options["models_mode"]:
|
|
90
90
|
is_operation_file = kwargs.pop("is_operation_file", False)
|
|
91
91
|
retval = f"_models.{self.name}"
|
|
92
92
|
if not self.is_public:
|
|
93
|
-
retval = f"{self.
|
|
93
|
+
retval = f"{self.code_model.models_filename}.{retval}"
|
|
94
94
|
return retval if is_operation_file else f'"{retval}"'
|
|
95
95
|
return "ET.Element" if self.is_xml else "JSON"
|
|
96
96
|
|
|
97
97
|
def docstring_type(self, **kwargs: Any) -> str:
|
|
98
|
-
if self.
|
|
99
|
-
return f"~{self.
|
|
98
|
+
if self.code_model.options["models_mode"]:
|
|
99
|
+
return f"~{self.code_model.namespace}.models.{self.name}"
|
|
100
100
|
return "ET.Element" if self.is_xml else "JSON"
|
|
101
101
|
|
|
102
102
|
def description(self, *, is_operation_file: bool = False) -> str:
|
|
103
103
|
return "" if is_operation_file else self.yaml_data.get("description", self.name)
|
|
104
104
|
|
|
105
105
|
def docstring_text(self, **kwargs: Any) -> str:
|
|
106
|
-
if self.
|
|
106
|
+
if self.code_model.options["models_mode"]:
|
|
107
107
|
return self.name
|
|
108
108
|
return "XML Element" if self.is_xml else "JSON object"
|
|
109
109
|
|
|
@@ -198,7 +198,7 @@ class ModelType(
|
|
|
198
198
|
|
|
199
199
|
@classmethod
|
|
200
200
|
def from_yaml(
|
|
201
|
-
cls, yaml_data: Dict[str, Any],
|
|
201
|
+
cls, yaml_data: Dict[str, Any], code_model: "CodeModel"
|
|
202
202
|
) -> "ModelType":
|
|
203
203
|
raise ValueError(
|
|
204
204
|
"You shouldn't call from_yaml for ModelType to avoid recursion. "
|
|
@@ -206,21 +206,21 @@ class ModelType(
|
|
|
206
206
|
)
|
|
207
207
|
|
|
208
208
|
def fill_instance_from_yaml(
|
|
209
|
-
self, yaml_data: Dict[str, Any],
|
|
209
|
+
self, yaml_data: Dict[str, Any], code_model: "CodeModel"
|
|
210
210
|
) -> None:
|
|
211
211
|
from . import build_type
|
|
212
212
|
|
|
213
213
|
self.parents = [
|
|
214
|
-
cast(ModelType, build_type(bm,
|
|
214
|
+
cast(ModelType, build_type(bm, code_model))
|
|
215
215
|
for bm in yaml_data.get("parents", [])
|
|
216
216
|
]
|
|
217
217
|
properties = [
|
|
218
|
-
Property.from_yaml(p,
|
|
218
|
+
Property.from_yaml(p, code_model) for p in yaml_data["properties"]
|
|
219
219
|
]
|
|
220
220
|
self.properties = _get_properties(self, properties)
|
|
221
221
|
# checking to see if this is a polymorphic class
|
|
222
222
|
self.discriminated_subtypes = {
|
|
223
|
-
k: cast(ModelType, build_type(v,
|
|
223
|
+
k: cast(ModelType, build_type(v, code_model))
|
|
224
224
|
for k, v in self.yaml_data.get("discriminatedSubtypes", {}).items()
|
|
225
225
|
}
|
|
226
226
|
|
|
@@ -250,7 +250,7 @@ class ModelType(
|
|
|
250
250
|
|
|
251
251
|
@property
|
|
252
252
|
def instance_check_template(self) -> str:
|
|
253
|
-
models_mode = self.
|
|
253
|
+
models_mode = self.code_model.options["models_mode"]
|
|
254
254
|
if models_mode == "msrest":
|
|
255
255
|
return "isinstance({}, msrest.Model)"
|
|
256
256
|
if models_mode == "dpg":
|
|
@@ -274,17 +274,16 @@ class ModelType(
|
|
|
274
274
|
def imports(self, **kwargs: Any) -> FileImport:
|
|
275
275
|
file_import = FileImport()
|
|
276
276
|
relative_path = kwargs.pop("relative_path", None)
|
|
277
|
-
if self.
|
|
277
|
+
if self.code_model.options["models_mode"] and relative_path:
|
|
278
278
|
# add import for models in operations file
|
|
279
279
|
file_import.add_submodule_import(
|
|
280
280
|
relative_path, "models", ImportType.LOCAL, alias="_models"
|
|
281
281
|
)
|
|
282
|
-
if self.
|
|
282
|
+
if self.code_model.options["models_mode"] == "msrest":
|
|
283
283
|
return file_import
|
|
284
284
|
file_import.add_submodule_import(
|
|
285
285
|
"typing", "Any", ImportType.STDLIB, TypingSection.CONDITIONAL
|
|
286
286
|
)
|
|
287
|
-
file_import.add_import("sys", ImportType.STDLIB)
|
|
288
287
|
file_import.define_mutable_mapping_type()
|
|
289
288
|
if self.is_xml:
|
|
290
289
|
file_import.add_submodule_import(
|