@autorest/python 5.17.0 → 6.0.0-rc.1
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/ChangeLog.md +63 -1
- package/README.md +9 -0
- package/autorest/codegen/__init__.py +24 -30
- package/autorest/codegen/models/base_builder.py +17 -6
- package/autorest/codegen/models/base_type.py +6 -0
- package/autorest/codegen/models/client.py +9 -6
- package/autorest/codegen/models/code_model.py +20 -14
- package/autorest/codegen/models/dictionary_type.py +16 -1
- package/autorest/codegen/models/imports.py +57 -2
- package/autorest/codegen/models/list_type.py +16 -1
- package/autorest/codegen/models/lro_operation.py +4 -6
- package/autorest/codegen/models/lro_paging_operation.py +3 -9
- package/autorest/codegen/models/model_type.py +34 -16
- package/autorest/codegen/models/operation.py +47 -79
- package/autorest/codegen/models/operation_group.py +10 -9
- package/autorest/codegen/models/paging_operation.py +4 -6
- package/autorest/codegen/models/parameter.py +3 -7
- package/autorest/codegen/models/parameter_list.py +26 -35
- package/autorest/codegen/models/property.py +14 -0
- package/autorest/codegen/models/request_builder.py +32 -43
- package/autorest/codegen/serializers/__init__.py +12 -50
- package/autorest/codegen/serializers/builder_serializer.py +136 -49
- package/autorest/codegen/serializers/client_serializer.py +23 -32
- package/autorest/codegen/serializers/general_serializer.py +12 -12
- package/autorest/codegen/serializers/import_serializer.py +11 -22
- package/autorest/codegen/serializers/metadata_serializer.py +0 -2
- package/autorest/codegen/serializers/{model_base_serializer.py → model_serializer.py} +58 -44
- package/autorest/codegen/serializers/operation_groups_serializer.py +3 -7
- package/autorest/codegen/serializers/operations_init_serializer.py +2 -23
- package/autorest/codegen/serializers/patch_serializer.py +1 -3
- package/autorest/codegen/serializers/request_builders_serializer.py +2 -5
- package/autorest/codegen/serializers/utils.py +3 -4
- package/autorest/codegen/templates/client.py.jinja2 +6 -3
- package/autorest/codegen/templates/config.py.jinja2 +2 -2
- package/autorest/codegen/templates/metadata.json.jinja2 +4 -4
- package/autorest/codegen/templates/model.py.jinja2 +1 -1
- package/autorest/codegen/templates/model_init.py.jinja2 +5 -12
- package/autorest/codegen/templates/operation_group.py.jinja2 +16 -3
- package/autorest/codegen/templates/operation_groups_container.py.jinja2 +2 -8
- package/autorest/codegen/templates/operation_tools.jinja2 +3 -1
- package/autorest/codegen/templates/patch.py.jinja2 +1 -2
- package/autorest/codegen/templates/request_builder.py.jinja2 +0 -7
- package/autorest/codegen/templates/request_builders.py.jinja2 +1 -4
- package/autorest/codegen/templates/rest_init.py.jinja2 +3 -8
- package/autorest/codegen/templates/serialization.py.jinja2 +2006 -0
- package/autorest/codegen/templates/setup.py.jinja2 +4 -0
- package/autorest/codegen/templates/vendor.py.jinja2 +10 -0
- package/autorest/m4reformatter/__init__.py +82 -58
- package/autorest/multiapi/models/client.py +12 -2
- package/autorest/multiapi/serializers/__init__.py +17 -8
- package/autorest/multiapi/serializers/import_serializer.py +4 -8
- package/autorest/multiapi/templates/multiapi_operations_mixin.py.jinja2 +1 -1
- package/autorest/preprocess/__init__.py +1 -0
- package/package.json +2 -2
- package/autorest/codegen/serializers/model_generic_serializer.py +0 -32
- package/autorest/codegen/serializers/model_python3_serializer.py +0 -72
|
@@ -11,9 +11,8 @@ from .parameter_serializer import ParameterSerializer, PopKwargType
|
|
|
11
11
|
|
|
12
12
|
|
|
13
13
|
class ClientSerializer:
|
|
14
|
-
def __init__(self, code_model: CodeModel
|
|
14
|
+
def __init__(self, code_model: CodeModel) -> None:
|
|
15
15
|
self.code_model = code_model
|
|
16
|
-
self.is_python3_file = is_python3_file
|
|
17
16
|
self.parameter_serializer = ParameterSerializer()
|
|
18
17
|
|
|
19
18
|
def _init_signature(self, async_mode: bool) -> str:
|
|
@@ -22,36 +21,32 @@ class ClientSerializer:
|
|
|
22
21
|
method_name="__init__",
|
|
23
22
|
need_self_param=True,
|
|
24
23
|
method_param_signatures=self.code_model.client.parameters.method_signature(
|
|
25
|
-
async_mode
|
|
24
|
+
async_mode
|
|
26
25
|
),
|
|
27
26
|
)
|
|
28
27
|
|
|
29
28
|
def init_signature_and_response_type_annotation(self, async_mode: bool) -> str:
|
|
30
29
|
init_signature = self._init_signature(async_mode)
|
|
31
30
|
return utils.method_signature_and_response_type_annotation_template(
|
|
32
|
-
is_python3_file=async_mode or self.is_python3_file,
|
|
33
31
|
method_signature=init_signature,
|
|
34
32
|
response_type_annotation="None",
|
|
35
33
|
)
|
|
36
34
|
|
|
37
|
-
def pop_kwargs_from_signature(self
|
|
35
|
+
def pop_kwargs_from_signature(self) -> List[str]:
|
|
38
36
|
return self.parameter_serializer.pop_kwargs_from_signature(
|
|
39
|
-
self.code_model.client.parameters.kwargs_to_pop
|
|
40
|
-
async_mode or self.is_python3_file,
|
|
41
|
-
),
|
|
37
|
+
self.code_model.client.parameters.kwargs_to_pop,
|
|
42
38
|
check_kwarg_dict=False,
|
|
43
39
|
pop_headers_kwarg=PopKwargType.NO,
|
|
44
40
|
pop_params_kwarg=PopKwargType.NO,
|
|
45
41
|
)
|
|
46
42
|
|
|
47
|
-
|
|
43
|
+
@property
|
|
44
|
+
def class_definition(self) -> str:
|
|
48
45
|
class_name = self.code_model.client.name
|
|
49
46
|
has_mixin_og = any(og for og in self.code_model.operation_groups if og.is_mixin)
|
|
50
47
|
base_class = ""
|
|
51
48
|
if has_mixin_og:
|
|
52
49
|
base_class = f"{class_name}OperationsMixin"
|
|
53
|
-
elif not (async_mode or self.is_python3_file):
|
|
54
|
-
base_class = "object"
|
|
55
50
|
pylint_disable = self.code_model.client.pylint_disable
|
|
56
51
|
if base_class:
|
|
57
52
|
return f"class {class_name}({base_class}):{pylint_disable}"
|
|
@@ -101,6 +96,14 @@ class ClientSerializer:
|
|
|
101
96
|
except StopIteration:
|
|
102
97
|
return "_endpoint"
|
|
103
98
|
|
|
99
|
+
@property
|
|
100
|
+
def should_init_super(self) -> bool:
|
|
101
|
+
return any(
|
|
102
|
+
og
|
|
103
|
+
for og in self.code_model.operation_groups
|
|
104
|
+
if og.is_mixin and og.has_abstract_operations
|
|
105
|
+
)
|
|
106
|
+
|
|
104
107
|
def initialize_pipeline_client(self, async_mode: bool) -> str:
|
|
105
108
|
pipeline_client_name = self.code_model.client.pipeline_class(async_mode)
|
|
106
109
|
return (
|
|
@@ -138,17 +141,10 @@ class ClientSerializer:
|
|
|
138
141
|
)
|
|
139
142
|
return retval
|
|
140
143
|
|
|
141
|
-
def _send_request_signature(self
|
|
142
|
-
|
|
143
|
-
request_signature = [
|
|
144
|
+
def _send_request_signature(self) -> str:
|
|
145
|
+
send_request_signature = [
|
|
144
146
|
"request: HttpRequest,"
|
|
145
|
-
|
|
146
|
-
else "request, # type: HttpRequest"
|
|
147
|
-
]
|
|
148
|
-
send_request_signature = (
|
|
149
|
-
request_signature
|
|
150
|
-
+ self.code_model.client.parameters.method_signature_kwargs(is_python3_file)
|
|
151
|
-
)
|
|
147
|
+
] + self.code_model.client.parameters.method_signature_kwargs
|
|
152
148
|
return self.parameter_serializer.serialize_method(
|
|
153
149
|
function_def="def",
|
|
154
150
|
method_name=self.code_model.client.send_request_name,
|
|
@@ -159,9 +155,8 @@ class ClientSerializer:
|
|
|
159
155
|
def send_request_signature_and_response_type_annotation(
|
|
160
156
|
self, async_mode: bool
|
|
161
157
|
) -> str:
|
|
162
|
-
send_request_signature = self._send_request_signature(
|
|
158
|
+
send_request_signature = self._send_request_signature()
|
|
163
159
|
return utils.method_signature_and_response_type_annotation_template(
|
|
164
|
-
is_python3_file=async_mode or self.is_python3_file,
|
|
165
160
|
method_signature=send_request_signature,
|
|
166
161
|
response_type_annotation="Awaitable[AsyncHttpResponse]"
|
|
167
162
|
if async_mode
|
|
@@ -224,7 +219,7 @@ class ClientSerializer:
|
|
|
224
219
|
retval.extend(self._rest_request_example(async_mode))
|
|
225
220
|
retval.append("")
|
|
226
221
|
retval.append(
|
|
227
|
-
"For more information on this code flow, see https://aka.ms/azsdk/python/
|
|
222
|
+
"For more information on this code flow, see https://aka.ms/azsdk/dpcodegen/python/send_request"
|
|
228
223
|
)
|
|
229
224
|
retval.append(f"")
|
|
230
225
|
retval.append(":param request: The network request you want to make. Required.")
|
|
@@ -247,10 +242,9 @@ class ClientSerializer:
|
|
|
247
242
|
|
|
248
243
|
|
|
249
244
|
class ConfigSerializer:
|
|
250
|
-
def __init__(self, code_model: CodeModel
|
|
245
|
+
def __init__(self, code_model: CodeModel) -> None:
|
|
251
246
|
self.code_model = code_model
|
|
252
247
|
self.parameter_serializer = ParameterSerializer()
|
|
253
|
-
self.is_python3_file = is_python3_file
|
|
254
248
|
|
|
255
249
|
def _init_signature(self, async_mode: bool) -> str:
|
|
256
250
|
return self.parameter_serializer.serialize_method(
|
|
@@ -258,23 +252,20 @@ class ConfigSerializer:
|
|
|
258
252
|
method_name="__init__",
|
|
259
253
|
need_self_param=True,
|
|
260
254
|
method_param_signatures=self.code_model.config.parameters.method_signature(
|
|
261
|
-
async_mode
|
|
255
|
+
async_mode
|
|
262
256
|
),
|
|
263
257
|
)
|
|
264
258
|
|
|
265
259
|
def init_signature_and_response_type_annotation(self, async_mode: bool) -> str:
|
|
266
260
|
init_signature = self._init_signature(async_mode)
|
|
267
261
|
return utils.method_signature_and_response_type_annotation_template(
|
|
268
|
-
is_python3_file=async_mode or self.is_python3_file,
|
|
269
262
|
method_signature=init_signature,
|
|
270
263
|
response_type_annotation="None",
|
|
271
264
|
)
|
|
272
265
|
|
|
273
|
-
def pop_kwargs_from_signature(self
|
|
266
|
+
def pop_kwargs_from_signature(self) -> List[str]:
|
|
274
267
|
return self.parameter_serializer.pop_kwargs_from_signature(
|
|
275
|
-
self.code_model.config.parameters.kwargs_to_pop
|
|
276
|
-
async_mode or self.is_python3_file
|
|
277
|
-
),
|
|
268
|
+
self.code_model.config.parameters.kwargs_to_pop,
|
|
278
269
|
check_kwarg_dict=False,
|
|
279
270
|
pop_headers_kwarg=PopKwargType.NO,
|
|
280
271
|
pop_params_kwarg=PopKwargType.NO,
|
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
# --------------------------------------------------------------------------
|
|
6
6
|
from jinja2 import Environment
|
|
7
7
|
from .import_serializer import FileImportSerializer, TypingSection
|
|
8
|
+
from ..models.imports import MsrestImportType
|
|
8
9
|
from ..models import (
|
|
9
10
|
FileImport,
|
|
10
11
|
ImportType,
|
|
@@ -33,14 +34,12 @@ class GeneralSerializer:
|
|
|
33
34
|
|
|
34
35
|
template = self.env.get_template("client.py.jinja2")
|
|
35
36
|
|
|
36
|
-
python3_only = self.code_model.options["python3_only"]
|
|
37
37
|
return template.render(
|
|
38
38
|
code_model=self.code_model,
|
|
39
39
|
async_mode=self.async_mode,
|
|
40
|
-
serializer=ClientSerializer(self.code_model
|
|
40
|
+
serializer=ClientSerializer(self.code_model),
|
|
41
41
|
imports=FileImportSerializer(
|
|
42
42
|
self.code_model.client.imports(self.async_mode),
|
|
43
|
-
is_python3_file=self.async_mode or python3_only,
|
|
44
43
|
),
|
|
45
44
|
)
|
|
46
45
|
|
|
@@ -73,18 +72,17 @@ class GeneralSerializer:
|
|
|
73
72
|
f"{self.code_model.client.name}Configuration",
|
|
74
73
|
ImportType.LOCAL,
|
|
75
74
|
)
|
|
76
|
-
file_import.
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
75
|
+
file_import.add_msrest_import(
|
|
76
|
+
self.code_model,
|
|
77
|
+
".." if self.async_mode else ".",
|
|
78
|
+
MsrestImportType.SerializerDeserializer,
|
|
79
|
+
TypingSection.TYPING,
|
|
81
80
|
)
|
|
82
81
|
|
|
83
82
|
return template.render(
|
|
84
83
|
code_model=self.code_model,
|
|
85
84
|
imports=FileImportSerializer(
|
|
86
85
|
file_import,
|
|
87
|
-
is_python3_file=self.async_mode,
|
|
88
86
|
),
|
|
89
87
|
async_mode=self.async_mode,
|
|
90
88
|
)
|
|
@@ -98,15 +96,13 @@ class GeneralSerializer:
|
|
|
98
96
|
package_name if package_name else self.code_model.client.name.lower()
|
|
99
97
|
)
|
|
100
98
|
template = self.env.get_template("config.py.jinja2")
|
|
101
|
-
python3_only = self.code_model.options["python3_only"]
|
|
102
99
|
return template.render(
|
|
103
100
|
code_model=self.code_model,
|
|
104
101
|
async_mode=self.async_mode,
|
|
105
102
|
imports=FileImportSerializer(
|
|
106
103
|
self.code_model.config.imports(self.async_mode),
|
|
107
|
-
is_python3_file=self.async_mode or python3_only,
|
|
108
104
|
),
|
|
109
|
-
serializer=ConfigSerializer(self.code_model
|
|
105
|
+
serializer=ConfigSerializer(self.code_model),
|
|
110
106
|
sdk_moniker=sdk_moniker,
|
|
111
107
|
)
|
|
112
108
|
|
|
@@ -120,3 +116,7 @@ class GeneralSerializer:
|
|
|
120
116
|
params.update(self.code_model.options)
|
|
121
117
|
params.update(self.code_model.package_dependency)
|
|
122
118
|
return template.render(code_model=self.code_model, **params)
|
|
119
|
+
|
|
120
|
+
def serialize_serialization_file(self) -> str:
|
|
121
|
+
template = self.env.get_template("serialization.py.jinja2")
|
|
122
|
+
return template.render(code_model=self.code_model)
|
|
@@ -58,11 +58,8 @@ def _get_import_clauses(imports: List[ImportModel], delimiter: str) -> List[str]
|
|
|
58
58
|
|
|
59
59
|
|
|
60
60
|
class FileImportSerializer:
|
|
61
|
-
def __init__(
|
|
62
|
-
self, file_import: FileImport, is_python3_file: bool, async_mode: bool = False
|
|
63
|
-
) -> None:
|
|
61
|
+
def __init__(self, file_import: FileImport, async_mode: bool = False) -> None:
|
|
64
62
|
self.file_import = file_import
|
|
65
|
-
self.is_python3_file = is_python3_file
|
|
66
63
|
self.async_mode = async_mode
|
|
67
64
|
|
|
68
65
|
def _get_imports_list(
|
|
@@ -82,20 +79,14 @@ class FileImportSerializer:
|
|
|
82
79
|
return file_import_copy.get_imports_from_section(baseline_typing_section)
|
|
83
80
|
|
|
84
81
|
def _add_type_checking_import(self):
|
|
85
|
-
|
|
86
|
-
self.file_import.get_imports_from_section(TypingSection.TYPING)
|
|
87
|
-
)
|
|
88
|
-
conditional_and_not_py3 = not self.is_python3_file and any(
|
|
89
|
-
self.file_import.get_imports_from_section(TypingSection.CONDITIONAL)
|
|
90
|
-
)
|
|
91
|
-
if any_typing or conditional_and_not_py3:
|
|
82
|
+
if any(self.file_import.get_imports_from_section(TypingSection.TYPING)):
|
|
92
83
|
self.file_import.add_submodule_import(
|
|
93
84
|
"typing", "TYPE_CHECKING", ImportType.STDLIB
|
|
94
85
|
)
|
|
95
86
|
|
|
96
87
|
def _get_typing_definitions(self) -> str:
|
|
97
88
|
def declare_defintion(
|
|
98
|
-
|
|
89
|
+
type_name: str, type_definition: TypeDefinition
|
|
99
90
|
) -> List[str]:
|
|
100
91
|
ret: List[str] = []
|
|
101
92
|
definition_value = (
|
|
@@ -112,21 +103,20 @@ class FileImportSerializer:
|
|
|
112
103
|
):
|
|
113
104
|
if version is not None:
|
|
114
105
|
ret.append(
|
|
115
|
-
"{}
|
|
116
|
-
|
|
106
|
+
"{} sys.version_info >= {}:".format(
|
|
107
|
+
"if" if i == 0 else "elif", version
|
|
117
108
|
)
|
|
118
109
|
)
|
|
119
110
|
elif i > 0:
|
|
120
|
-
ret.append("
|
|
111
|
+
ret.append("else:")
|
|
121
112
|
for import_clause in _get_import_clauses(
|
|
122
113
|
[type_definition.version_imports[version]], "\n"
|
|
123
114
|
):
|
|
124
115
|
ret.append(
|
|
125
|
-
"{}{}
|
|
116
|
+
"{}{}".format(
|
|
126
117
|
" "
|
|
127
118
|
if len(versions) > 1 or version is not None
|
|
128
119
|
else "",
|
|
129
|
-
spacing,
|
|
130
120
|
import_clause,
|
|
131
121
|
)
|
|
132
122
|
)
|
|
@@ -134,15 +124,14 @@ class FileImportSerializer:
|
|
|
134
124
|
ret[
|
|
135
125
|
-1
|
|
136
126
|
] += " # type: ignore # pylint: disable=ungrouped-imports"
|
|
137
|
-
ret.append("{}
|
|
127
|
+
ret.append("{} = {}".format(type_name, definition_value))
|
|
138
128
|
return ret
|
|
139
129
|
|
|
140
130
|
if not self.file_import.type_definitions:
|
|
141
131
|
return ""
|
|
142
|
-
spacing = "" if self.is_python3_file else " "
|
|
143
132
|
declarations: List[str] = [""]
|
|
144
133
|
for type_name, value in self.file_import.type_definitions.items():
|
|
145
|
-
declarations.extend(declare_defintion(
|
|
134
|
+
declarations.extend(declare_defintion(type_name, value))
|
|
146
135
|
return "\n".join(declarations)
|
|
147
136
|
|
|
148
137
|
def __str__(self) -> str:
|
|
@@ -150,7 +139,7 @@ class FileImportSerializer:
|
|
|
150
139
|
regular_imports = ""
|
|
151
140
|
regular_imports_list = self._get_imports_list(
|
|
152
141
|
baseline_typing_section=TypingSection.REGULAR,
|
|
153
|
-
add_conditional_typing=
|
|
142
|
+
add_conditional_typing=True,
|
|
154
143
|
)
|
|
155
144
|
|
|
156
145
|
if regular_imports_list:
|
|
@@ -161,7 +150,7 @@ class FileImportSerializer:
|
|
|
161
150
|
typing_imports = ""
|
|
162
151
|
typing_imports_list = self._get_imports_list(
|
|
163
152
|
baseline_typing_section=TypingSection.TYPING,
|
|
164
|
-
add_conditional_typing=
|
|
153
|
+
add_conditional_typing=False,
|
|
165
154
|
)
|
|
166
155
|
if typing_imports_list:
|
|
167
156
|
typing_imports += "\n\nif TYPE_CHECKING:\n # pylint: disable=unused-import,ungrouped-imports\n "
|
|
@@ -143,13 +143,11 @@ class MetadataSerializer:
|
|
|
143
143
|
get_operation_serializer,
|
|
144
144
|
code_model=self.code_model,
|
|
145
145
|
async_mode=True,
|
|
146
|
-
is_python3_file=True,
|
|
147
146
|
),
|
|
148
147
|
get_sync_operation_serializer=functools.partial(
|
|
149
148
|
get_operation_serializer,
|
|
150
149
|
code_model=self.code_model,
|
|
151
150
|
async_mode=False,
|
|
152
|
-
is_python3_file=False,
|
|
153
151
|
),
|
|
154
152
|
has_credential=bool(self.code_model.credential),
|
|
155
153
|
)
|
|
@@ -3,11 +3,10 @@
|
|
|
3
3
|
# Licensed under the MIT License. See License.txt in the project root for
|
|
4
4
|
# license information.
|
|
5
5
|
# --------------------------------------------------------------------------
|
|
6
|
-
from abc import abstractmethod
|
|
7
6
|
from typing import cast, List
|
|
8
7
|
from jinja2 import Environment
|
|
9
8
|
from ..models import ModelType, CodeModel, Property
|
|
10
|
-
from ..models.imports import FileImport,
|
|
9
|
+
from ..models.imports import FileImport, TypingSection, MsrestImportType
|
|
11
10
|
from .import_serializer import FileImportSerializer
|
|
12
11
|
|
|
13
12
|
|
|
@@ -27,31 +26,33 @@ def _documentation_string(
|
|
|
27
26
|
return retval
|
|
28
27
|
|
|
29
28
|
|
|
30
|
-
class
|
|
31
|
-
def __init__(
|
|
32
|
-
self, code_model: CodeModel, env: Environment, is_python3_file: bool
|
|
33
|
-
) -> None:
|
|
29
|
+
class ModelSerializer:
|
|
30
|
+
def __init__(self, code_model: CodeModel, env: Environment) -> None:
|
|
34
31
|
self.code_model = code_model
|
|
35
32
|
self.env = env
|
|
36
|
-
self.is_python3_file = is_python3_file
|
|
37
33
|
|
|
38
34
|
def serialize(self) -> str:
|
|
39
35
|
# Generate the models
|
|
40
36
|
template = self.env.get_template("model_container.py.jinja2")
|
|
41
37
|
return template.render(
|
|
42
38
|
code_model=self.code_model,
|
|
43
|
-
imports=FileImportSerializer(
|
|
44
|
-
self.imports(), is_python3_file=self.is_python3_file
|
|
45
|
-
),
|
|
39
|
+
imports=FileImportSerializer(self.imports()),
|
|
46
40
|
str=str,
|
|
47
41
|
serializer=self,
|
|
48
42
|
)
|
|
49
43
|
|
|
50
44
|
def imports(self) -> FileImport:
|
|
51
45
|
file_import = FileImport()
|
|
52
|
-
file_import.
|
|
46
|
+
file_import.add_msrest_import(
|
|
47
|
+
self.code_model, "..", MsrestImportType.Module, TypingSection.REGULAR
|
|
48
|
+
)
|
|
53
49
|
for model in self.code_model.model_types:
|
|
54
50
|
file_import.merge(model.imports(is_operation_file=False))
|
|
51
|
+
init_line_parameters = [
|
|
52
|
+
p for p in model.properties if not p.readonly and not p.is_discriminator
|
|
53
|
+
]
|
|
54
|
+
for param in init_line_parameters:
|
|
55
|
+
file_import.merge(param.imports())
|
|
55
56
|
return file_import
|
|
56
57
|
|
|
57
58
|
@staticmethod
|
|
@@ -71,9 +72,12 @@ class ModelBaseSerializer:
|
|
|
71
72
|
properties_to_initialize = model.properties
|
|
72
73
|
return properties_to_initialize
|
|
73
74
|
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
75
|
+
def declare_model(self, model: ModelType) -> str:
|
|
76
|
+
basename = (
|
|
77
|
+
"msrest.serialization.Model"
|
|
78
|
+
if self.code_model.options["client_side_validation"]
|
|
79
|
+
else "_serialization.Model"
|
|
80
|
+
)
|
|
77
81
|
if model.parents:
|
|
78
82
|
basename = ", ".join([cast(ModelType, m).name for m in model.parents])
|
|
79
83
|
return f"class {model.name}({basename}):{model.pylint_disable}"
|
|
@@ -87,18 +91,12 @@ class ModelBaseSerializer:
|
|
|
87
91
|
def variable_documentation_string(prop: Property) -> List[str]:
|
|
88
92
|
return _documentation_string(prop, "ivar", "vartype")
|
|
89
93
|
|
|
90
|
-
@abstractmethod
|
|
91
|
-
def super_call_template(self, model: ModelType) -> str:
|
|
92
|
-
...
|
|
93
|
-
|
|
94
94
|
def super_call(self, model: ModelType):
|
|
95
|
-
return self.
|
|
96
|
-
self.properties_to_pass_to_super(model)
|
|
97
|
-
)
|
|
95
|
+
return f"super().__init__({self.properties_to_pass_to_super(model)})"
|
|
98
96
|
|
|
99
97
|
def initialize_properties(self, model: ModelType) -> List[str]:
|
|
100
98
|
init_args = []
|
|
101
|
-
for prop in
|
|
99
|
+
for prop in self.get_properties_to_initialize(model):
|
|
102
100
|
if prop.is_discriminator:
|
|
103
101
|
discriminator_value = (
|
|
104
102
|
f"'{model.discriminator_value}'"
|
|
@@ -115,37 +113,53 @@ class ModelBaseSerializer:
|
|
|
115
113
|
elif prop.readonly:
|
|
116
114
|
init_args.append(f"self.{prop.client_name} = None")
|
|
117
115
|
elif not prop.constant:
|
|
118
|
-
init_args.append(self.
|
|
116
|
+
init_args.append(f"self.{prop.client_name} = {prop.client_name}")
|
|
119
117
|
return init_args
|
|
120
118
|
|
|
121
|
-
|
|
119
|
+
@staticmethod
|
|
120
|
+
def initialize_standard_property(prop: Property):
|
|
122
121
|
if not (prop.optional or prop.client_default_value is not None):
|
|
123
|
-
return
|
|
124
|
-
return
|
|
122
|
+
return f"{prop.client_name}: {prop.type_annotation()},{prop.pylint_disable}"
|
|
123
|
+
return (
|
|
124
|
+
f"{prop.client_name}: {prop.type_annotation()} = "
|
|
125
|
+
f"{prop.client_default_value_declaration},{prop.pylint_disable}"
|
|
126
|
+
)
|
|
125
127
|
|
|
126
128
|
@staticmethod
|
|
127
129
|
def discriminator_docstring(model: ModelType) -> str:
|
|
128
130
|
return (
|
|
129
131
|
"You probably want to use the sub-classes and not this class directly. "
|
|
130
|
-
f"Known sub-classes are: {', '.join(model.discriminated_subtypes.values())}"
|
|
132
|
+
f"Known sub-classes are: {', '.join(v.name for v in model.discriminated_subtypes.values())}"
|
|
131
133
|
)
|
|
132
134
|
|
|
133
|
-
@abstractmethod
|
|
134
135
|
def init_line(self, model: ModelType) -> List[str]:
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
136
|
+
init_properties_declaration = []
|
|
137
|
+
init_line_parameters = [
|
|
138
|
+
p
|
|
139
|
+
for p in model.properties
|
|
140
|
+
if not p.readonly and not p.is_discriminator and not p.constant
|
|
141
|
+
]
|
|
142
|
+
init_line_parameters.sort(key=lambda x: x.optional)
|
|
143
|
+
if init_line_parameters:
|
|
144
|
+
init_properties_declaration.append("*,")
|
|
145
|
+
for param in init_line_parameters:
|
|
146
|
+
init_properties_declaration.append(self.initialize_standard_property(param))
|
|
147
|
+
|
|
148
|
+
return init_properties_declaration
|
|
140
149
|
|
|
141
|
-
@
|
|
142
|
-
def
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
150
|
+
@staticmethod
|
|
151
|
+
def properties_to_pass_to_super(model: ModelType) -> str:
|
|
152
|
+
properties_to_pass_to_super = []
|
|
153
|
+
for parent in model.parents:
|
|
154
|
+
for prop in model.properties:
|
|
155
|
+
if (
|
|
156
|
+
prop in parent.properties
|
|
157
|
+
and not prop.is_discriminator
|
|
158
|
+
and not prop.constant
|
|
159
|
+
and not prop.readonly
|
|
160
|
+
):
|
|
161
|
+
properties_to_pass_to_super.append(
|
|
162
|
+
f"{prop.client_name}={prop.client_name}"
|
|
163
|
+
)
|
|
164
|
+
properties_to_pass_to_super.append("**kwargs")
|
|
165
|
+
return ", ".join(properties_to_pass_to_super)
|
|
@@ -22,13 +22,11 @@ class OperationGroupsSerializer:
|
|
|
22
22
|
code_model: CodeModel,
|
|
23
23
|
env: Environment,
|
|
24
24
|
async_mode: bool,
|
|
25
|
-
is_python3_file: bool,
|
|
26
25
|
operation_group: Optional[OperationGroup] = None,
|
|
27
26
|
) -> None:
|
|
28
27
|
self.code_model = code_model
|
|
29
28
|
self.env = env
|
|
30
29
|
self.async_mode = async_mode
|
|
31
|
-
self.is_python3_file = is_python3_file
|
|
32
30
|
self.operation_group = operation_group
|
|
33
31
|
|
|
34
32
|
def serialize(self) -> str:
|
|
@@ -42,7 +40,6 @@ class OperationGroupsSerializer:
|
|
|
42
40
|
imports.merge(
|
|
43
41
|
operation_group.imports(
|
|
44
42
|
async_mode=self.async_mode,
|
|
45
|
-
is_python3_file=self.is_python3_file,
|
|
46
43
|
)
|
|
47
44
|
)
|
|
48
45
|
|
|
@@ -54,20 +51,19 @@ class OperationGroupsSerializer:
|
|
|
54
51
|
operation_groups=operation_groups,
|
|
55
52
|
imports=FileImportSerializer(
|
|
56
53
|
imports,
|
|
57
|
-
is_python3_file=self.is_python3_file,
|
|
58
54
|
async_mode=self.async_mode,
|
|
59
55
|
),
|
|
60
56
|
async_mode=self.async_mode,
|
|
61
|
-
is_python3_file=self.is_python3_file,
|
|
62
57
|
get_operation_serializer=functools.partial(
|
|
63
58
|
get_operation_serializer,
|
|
64
59
|
code_model=self.code_model,
|
|
65
60
|
async_mode=self.async_mode,
|
|
66
|
-
is_python3_file=self.is_python3_file,
|
|
67
61
|
),
|
|
68
62
|
request_builder_serializer=RequestBuilderSerializer(
|
|
69
63
|
self.code_model,
|
|
70
64
|
async_mode=False,
|
|
71
|
-
is_python3_file=self.is_python3_file,
|
|
72
65
|
),
|
|
66
|
+
request_builders=[
|
|
67
|
+
rb for rb in self.code_model.request_builders if not rb.abstract
|
|
68
|
+
],
|
|
73
69
|
)
|
|
@@ -18,40 +18,19 @@ class OperationsInitSerializer:
|
|
|
18
18
|
self.env = env
|
|
19
19
|
self.async_mode = async_mode
|
|
20
20
|
|
|
21
|
-
def
|
|
21
|
+
def operation_group_imports(self) -> List[str]:
|
|
22
22
|
def _get_filename(operation_group: OperationGroup) -> str:
|
|
23
|
-
|
|
23
|
+
return (
|
|
24
24
|
"_operations"
|
|
25
25
|
if self.code_model.options["combine_operation_files"]
|
|
26
26
|
else operation_group.filename
|
|
27
27
|
)
|
|
28
|
-
return prefix + filename_suffix
|
|
29
28
|
|
|
30
29
|
return [
|
|
31
30
|
f"from .{_get_filename(og)} import {og.class_name}"
|
|
32
31
|
for og in self.code_model.operation_groups
|
|
33
32
|
]
|
|
34
33
|
|
|
35
|
-
def operation_group_imports(self) -> List[str]:
|
|
36
|
-
typed_py3_files = self.code_model.options["add_python3_operation_files"]
|
|
37
|
-
py3_only = self.code_model.options["python3_only"]
|
|
38
|
-
if typed_py3_files and not py3_only and not self.async_mode:
|
|
39
|
-
retval: List[str] = ["try:"]
|
|
40
|
-
retval.extend(
|
|
41
|
-
[
|
|
42
|
-
f" {line}"
|
|
43
|
-
for line in self._operation_group_imports_helper(
|
|
44
|
-
filename_suffix="_py3"
|
|
45
|
-
)
|
|
46
|
-
]
|
|
47
|
-
)
|
|
48
|
-
retval.append("except (SyntaxError, ImportError):")
|
|
49
|
-
retval.extend(
|
|
50
|
-
[f" {line}" for line in self._operation_group_imports_helper()]
|
|
51
|
-
)
|
|
52
|
-
return retval
|
|
53
|
-
return self._operation_group_imports_helper()
|
|
54
|
-
|
|
55
34
|
def serialize(self) -> str:
|
|
56
35
|
operation_group_init_template = self.env.get_template(
|
|
57
36
|
"operations_folder_init.py.jinja2"
|
|
@@ -19,9 +19,7 @@ class PatchSerializer:
|
|
|
19
19
|
imports.add_submodule_import(
|
|
20
20
|
"typing", "List", ImportType.STDLIB, TypingSection.CONDITIONAL
|
|
21
21
|
)
|
|
22
|
-
is_python3_file = self.code_model.options["python3_only"]
|
|
23
22
|
return template.render(
|
|
24
23
|
code_model=self.code_model,
|
|
25
|
-
imports=FileImportSerializer(imports
|
|
26
|
-
is_python3_file=is_python3_file,
|
|
24
|
+
imports=FileImportSerializer(imports),
|
|
27
25
|
)
|
|
@@ -18,13 +18,11 @@ class RequestBuildersSerializer:
|
|
|
18
18
|
code_model: CodeModel,
|
|
19
19
|
env: Environment,
|
|
20
20
|
request_builders: List[RequestBuilderType],
|
|
21
|
-
is_python3_file: bool,
|
|
22
21
|
) -> None:
|
|
23
22
|
self.code_model = code_model
|
|
24
23
|
self.env = env
|
|
25
24
|
self.request_builders = request_builders
|
|
26
25
|
self.group_name = request_builders[0].group_name
|
|
27
|
-
self.is_python3_file = is_python3_file
|
|
28
26
|
|
|
29
27
|
@property
|
|
30
28
|
def imports(self) -> FileImport:
|
|
@@ -46,12 +44,11 @@ class RequestBuildersSerializer:
|
|
|
46
44
|
|
|
47
45
|
return template.render(
|
|
48
46
|
code_model=self.code_model,
|
|
49
|
-
request_builders=self.request_builders,
|
|
47
|
+
request_builders=[rb for rb in self.request_builders if not rb.abstract],
|
|
50
48
|
imports=FileImportSerializer(
|
|
51
49
|
self.imports,
|
|
52
|
-
is_python3_file=True,
|
|
53
50
|
),
|
|
54
51
|
request_builder_serializer=RequestBuilderSerializer(
|
|
55
|
-
self.code_model, async_mode=False
|
|
52
|
+
self.code_model, async_mode=False
|
|
56
53
|
),
|
|
57
54
|
)
|
|
@@ -3,12 +3,11 @@
|
|
|
3
3
|
# Licensed under the MIT License. See License.txt in the project root for
|
|
4
4
|
# license information.
|
|
5
5
|
# --------------------------------------------------------------------------
|
|
6
|
+
|
|
7
|
+
|
|
6
8
|
def method_signature_and_response_type_annotation_template(
|
|
7
9
|
*,
|
|
8
|
-
is_python3_file: bool,
|
|
9
10
|
method_signature: str,
|
|
10
11
|
response_type_annotation: str,
|
|
11
12
|
) -> str:
|
|
12
|
-
|
|
13
|
-
return f"{method_signature} -> {response_type_annotation}:"
|
|
14
|
-
return f"{method_signature}:\n # type: (...) -> {response_type_annotation}"
|
|
13
|
+
return f"{method_signature} -> {response_type_annotation}:"
|
|
@@ -6,13 +6,16 @@
|
|
|
6
6
|
|
|
7
7
|
{{ imports }}
|
|
8
8
|
|
|
9
|
-
{{ serializer.class_definition
|
|
9
|
+
{{ serializer.class_definition }}
|
|
10
10
|
"""{{ op_tools.wrap_string(code_model.client.description, "\n") | indent }}
|
|
11
11
|
|
|
12
12
|
{{ op_tools.serialize_with_wrap(serializer.property_descriptions(async_mode), "\n ") | indent }}
|
|
13
13
|
{{ serializer.init_signature_and_response_type_annotation(async_mode) | indent }}
|
|
14
|
-
{% if
|
|
15
|
-
|
|
14
|
+
{% if serializer.should_init_super %}
|
|
15
|
+
super().__init__()
|
|
16
|
+
{% endif %}
|
|
17
|
+
{% if code_model.client.parameters.kwargs_to_pop %}
|
|
18
|
+
{{ op_tools.serialize(serializer.pop_kwargs_from_signature()) | indent(8) }}
|
|
16
19
|
{% endif %}
|
|
17
20
|
{% if code_model.client.has_parameterized_host %}
|
|
18
21
|
{{ serializer.host_variable_name }} = {{ keywords.escape_str(code_model.client.url) }}
|