@typespec/http-client-python 0.4.4 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/emitter/emitter.d.ts.map +1 -1
- package/dist/emitter/emitter.js +85 -24
- package/dist/emitter/emitter.js.map +1 -1
- package/dist/emitter/lib.d.ts +1 -0
- package/dist/emitter/lib.d.ts.map +1 -1
- package/dist/emitter/lib.js +1 -0
- package/dist/emitter/lib.js.map +1 -1
- package/dist/emitter/run-python3.d.ts +2 -0
- package/dist/emitter/run-python3.d.ts.map +1 -0
- package/dist/emitter/run-python3.js +19 -0
- package/dist/emitter/run-python3.js.map +1 -0
- package/dist/emitter/system-requirements.d.ts +17 -0
- package/dist/emitter/system-requirements.d.ts.map +1 -0
- package/dist/emitter/system-requirements.js +167 -0
- package/dist/emitter/system-requirements.js.map +1 -0
- package/emitter/src/emitter.ts +88 -23
- package/emitter/src/lib.ts +2 -0
- package/emitter/src/run-python3.ts +20 -0
- package/emitter/src/system-requirements.ts +261 -0
- package/emitter/temp/tsconfig.tsbuildinfo +1 -1
- package/eng/scripts/ci/regenerate.ts +16 -4
- package/eng/scripts/setup/__pycache__/venvtools.cpython-38.pyc +0 -0
- package/eng/scripts/setup/build.ts +16 -0
- package/eng/scripts/setup/build_pygen_wheel.py +40 -0
- package/eng/scripts/setup/install.py +9 -3
- package/eng/scripts/setup/install.ts +32 -0
- package/eng/scripts/setup/prepare.py +3 -1
- package/eng/scripts/setup/prepare.ts +11 -0
- package/eng/scripts/setup/run-python3.ts +1 -6
- package/generator/build/lib/pygen/__init__.py +107 -0
- package/generator/build/lib/pygen/_version.py +7 -0
- package/generator/build/lib/pygen/black.py +71 -0
- package/generator/build/lib/pygen/codegen/__init__.py +357 -0
- package/generator/build/lib/pygen/codegen/_utils.py +17 -0
- package/generator/build/lib/pygen/codegen/models/__init__.py +204 -0
- package/generator/build/lib/pygen/codegen/models/base.py +186 -0
- package/generator/build/lib/pygen/codegen/models/base_builder.py +118 -0
- package/generator/build/lib/pygen/codegen/models/client.py +435 -0
- package/generator/build/lib/pygen/codegen/models/code_model.py +237 -0
- package/generator/build/lib/pygen/codegen/models/combined_type.py +149 -0
- package/generator/build/lib/pygen/codegen/models/constant_type.py +129 -0
- package/generator/build/lib/pygen/codegen/models/credential_types.py +214 -0
- package/generator/build/lib/pygen/codegen/models/dictionary_type.py +127 -0
- package/generator/build/lib/pygen/codegen/models/enum_type.py +238 -0
- package/generator/build/lib/pygen/codegen/models/imports.py +291 -0
- package/generator/build/lib/pygen/codegen/models/list_type.py +143 -0
- package/generator/build/lib/pygen/codegen/models/lro_operation.py +142 -0
- package/generator/build/lib/pygen/codegen/models/lro_paging_operation.py +32 -0
- package/generator/build/lib/pygen/codegen/models/model_type.py +357 -0
- package/generator/build/lib/pygen/codegen/models/operation.py +509 -0
- package/generator/build/lib/pygen/codegen/models/operation_group.py +184 -0
- package/generator/build/lib/pygen/codegen/models/paging_operation.py +155 -0
- package/generator/build/lib/pygen/codegen/models/parameter.py +412 -0
- package/generator/build/lib/pygen/codegen/models/parameter_list.py +387 -0
- package/generator/build/lib/pygen/codegen/models/primitive_types.py +659 -0
- package/generator/build/lib/pygen/codegen/models/property.py +170 -0
- package/generator/build/lib/pygen/codegen/models/request_builder.py +189 -0
- package/generator/build/lib/pygen/codegen/models/request_builder_parameter.py +115 -0
- package/generator/build/lib/pygen/codegen/models/response.py +348 -0
- package/generator/build/lib/pygen/codegen/models/utils.py +21 -0
- package/generator/build/lib/pygen/codegen/serializers/__init__.py +574 -0
- package/generator/build/lib/pygen/codegen/serializers/base_serializer.py +21 -0
- package/generator/build/lib/pygen/codegen/serializers/builder_serializer.py +1533 -0
- package/generator/build/lib/pygen/codegen/serializers/client_serializer.py +294 -0
- package/generator/build/lib/pygen/codegen/serializers/enum_serializer.py +15 -0
- package/generator/build/lib/pygen/codegen/serializers/general_serializer.py +213 -0
- package/generator/build/lib/pygen/codegen/serializers/import_serializer.py +126 -0
- package/generator/build/lib/pygen/codegen/serializers/metadata_serializer.py +198 -0
- package/generator/build/lib/pygen/codegen/serializers/model_init_serializer.py +33 -0
- package/generator/build/lib/pygen/codegen/serializers/model_serializer.py +335 -0
- package/generator/build/lib/pygen/codegen/serializers/operation_groups_serializer.py +89 -0
- package/generator/build/lib/pygen/codegen/serializers/operations_init_serializer.py +44 -0
- package/generator/build/lib/pygen/codegen/serializers/parameter_serializer.py +221 -0
- package/generator/build/lib/pygen/codegen/serializers/patch_serializer.py +19 -0
- package/generator/build/lib/pygen/codegen/serializers/request_builders_serializer.py +52 -0
- package/generator/build/lib/pygen/codegen/serializers/sample_serializer.py +168 -0
- package/generator/build/lib/pygen/codegen/serializers/test_serializer.py +292 -0
- package/generator/build/lib/pygen/codegen/serializers/types_serializer.py +31 -0
- package/generator/build/lib/pygen/codegen/serializers/utils.py +68 -0
- package/generator/build/lib/pygen/codegen/templates/client.py.jinja2 +37 -0
- package/generator/build/lib/pygen/codegen/templates/client_container.py.jinja2 +12 -0
- package/generator/build/lib/pygen/codegen/templates/config.py.jinja2 +73 -0
- package/generator/build/lib/pygen/codegen/templates/config_container.py.jinja2 +16 -0
- package/generator/build/lib/pygen/codegen/templates/conftest.py.jinja2 +28 -0
- package/generator/build/lib/pygen/codegen/templates/enum.py.jinja2 +13 -0
- package/generator/build/lib/pygen/codegen/templates/enum_container.py.jinja2 +10 -0
- package/generator/build/lib/pygen/codegen/templates/init.py.jinja2 +24 -0
- package/generator/build/lib/pygen/codegen/templates/keywords.jinja2 +27 -0
- package/generator/build/lib/pygen/codegen/templates/lro_operation.py.jinja2 +16 -0
- package/generator/build/lib/pygen/codegen/templates/lro_paging_operation.py.jinja2 +18 -0
- package/generator/build/lib/pygen/codegen/templates/macros.jinja2 +12 -0
- package/generator/build/lib/pygen/codegen/templates/metadata.json.jinja2 +167 -0
- package/generator/build/lib/pygen/codegen/templates/model_base.py.jinja2 +1174 -0
- package/generator/build/lib/pygen/codegen/templates/model_container.py.jinja2 +15 -0
- package/generator/build/lib/pygen/codegen/templates/model_dpg.py.jinja2 +97 -0
- package/generator/build/lib/pygen/codegen/templates/model_init.py.jinja2 +33 -0
- package/generator/build/lib/pygen/codegen/templates/model_msrest.py.jinja2 +92 -0
- package/generator/build/lib/pygen/codegen/templates/operation.py.jinja2 +21 -0
- package/generator/build/lib/pygen/codegen/templates/operation_group.py.jinja2 +75 -0
- package/generator/build/lib/pygen/codegen/templates/operation_groups_container.py.jinja2 +19 -0
- package/generator/build/lib/pygen/codegen/templates/operation_tools.jinja2 +81 -0
- package/generator/build/lib/pygen/codegen/templates/operations_folder_init.py.jinja2 +17 -0
- package/generator/build/lib/pygen/codegen/templates/packaging_templates/CHANGELOG.md.jinja2 +6 -0
- package/generator/build/lib/pygen/codegen/templates/packaging_templates/LICENSE.jinja2 +21 -0
- package/generator/build/lib/pygen/codegen/templates/packaging_templates/MANIFEST.in.jinja2 +8 -0
- package/generator/build/lib/pygen/codegen/templates/packaging_templates/README.md.jinja2 +107 -0
- package/generator/build/lib/pygen/codegen/templates/packaging_templates/dev_requirements.txt.jinja2 +9 -0
- package/generator/build/lib/pygen/codegen/templates/packaging_templates/setup.py.jinja2 +108 -0
- package/generator/build/lib/pygen/codegen/templates/paging_operation.py.jinja2 +21 -0
- package/generator/build/lib/pygen/codegen/templates/patch.py.jinja2 +19 -0
- package/generator/build/lib/pygen/codegen/templates/pkgutil_init.py.jinja2 +1 -0
- package/generator/build/lib/pygen/codegen/templates/request_builder.py.jinja2 +28 -0
- package/generator/build/lib/pygen/codegen/templates/request_builders.py.jinja2 +10 -0
- package/generator/build/lib/pygen/codegen/templates/rest_init.py.jinja2 +12 -0
- package/generator/build/lib/pygen/codegen/templates/sample.py.jinja2 +44 -0
- package/generator/build/lib/pygen/codegen/templates/serialization.py.jinja2 +2117 -0
- package/generator/build/lib/pygen/codegen/templates/test.py.jinja2 +50 -0
- package/generator/build/lib/pygen/codegen/templates/testpreparer.py.jinja2 +26 -0
- package/generator/build/lib/pygen/codegen/templates/types.py.jinja2 +7 -0
- package/generator/build/lib/pygen/codegen/templates/validation.py.jinja2 +38 -0
- package/generator/build/lib/pygen/codegen/templates/vendor.py.jinja2 +96 -0
- package/generator/build/lib/pygen/codegen/templates/version.py.jinja2 +4 -0
- package/generator/build/lib/pygen/m2r.py +65 -0
- package/generator/build/lib/pygen/preprocess/__init__.py +515 -0
- package/generator/build/lib/pygen/preprocess/helpers.py +27 -0
- package/generator/build/lib/pygen/preprocess/python_mappings.py +226 -0
- package/generator/build/lib/pygen/utils.py +163 -0
- package/generator/component-detection-pip-report.json +134 -0
- package/generator/dev_requirements.txt +0 -1
- package/generator/dist/pygen-0.1.0-py3-none-any.whl +0 -0
- package/generator/pygen.egg-info/PKG-INFO +7 -4
- package/generator/pygen.egg-info/requires.txt +7 -4
- package/generator/requirements.txt +5 -10
- package/generator/setup.py +7 -4
- package/generator/test/azure/requirements.txt +1 -1
- package/generator/test/unbranded/requirements.txt +1 -1
- package/package.json +6 -5
|
@@ -0,0 +1,659 @@
|
|
|
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 datetime
|
|
7
|
+
import decimal
|
|
8
|
+
from typing import Any, Dict, List, Optional, Union, TYPE_CHECKING
|
|
9
|
+
|
|
10
|
+
from .base import BaseType
|
|
11
|
+
from .imports import FileImport, ImportType, TypingSection
|
|
12
|
+
|
|
13
|
+
if TYPE_CHECKING:
|
|
14
|
+
from .code_model import CodeModel
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class RawString:
|
|
18
|
+
def __init__(self, string: str) -> None:
|
|
19
|
+
self.string = string
|
|
20
|
+
|
|
21
|
+
def __repr__(self) -> str:
|
|
22
|
+
return "r'{}'".format(self.string.replace("'", "\\'"))
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class PrimitiveType(BaseType):
|
|
26
|
+
def description(self, *, is_operation_file: bool) -> str:
|
|
27
|
+
return ""
|
|
28
|
+
|
|
29
|
+
def type_annotation(self, **kwargs: Any) -> str:
|
|
30
|
+
return self.docstring_type(**kwargs)
|
|
31
|
+
|
|
32
|
+
def docstring_text(self, **kwargs: Any) -> str:
|
|
33
|
+
return self.docstring_type(**kwargs)
|
|
34
|
+
|
|
35
|
+
def get_json_template_representation(
|
|
36
|
+
self,
|
|
37
|
+
*,
|
|
38
|
+
client_default_value_declaration: Optional[str] = None,
|
|
39
|
+
) -> Any:
|
|
40
|
+
if self.client_default_value is not None:
|
|
41
|
+
client_default_value_declaration = client_default_value_declaration or self.get_declaration(
|
|
42
|
+
self.client_default_value
|
|
43
|
+
)
|
|
44
|
+
return client_default_value_declaration or self.default_template_representation_declaration
|
|
45
|
+
|
|
46
|
+
@property
|
|
47
|
+
def default_template_representation_declaration(self) -> str:
|
|
48
|
+
return self.get_declaration(self.docstring_type())
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
class BooleanType(PrimitiveType):
|
|
52
|
+
@property
|
|
53
|
+
def serialization_type(self) -> str:
|
|
54
|
+
return "bool"
|
|
55
|
+
|
|
56
|
+
def docstring_type(self, **kwargs: Any) -> str:
|
|
57
|
+
return "bool"
|
|
58
|
+
|
|
59
|
+
@property
|
|
60
|
+
def instance_check_template(self) -> str:
|
|
61
|
+
return "isinstance({}, bool)"
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
class BinaryType(PrimitiveType):
|
|
65
|
+
def __init__(self, yaml_data: Dict[str, Any], code_model: "CodeModel") -> None:
|
|
66
|
+
super().__init__(yaml_data=yaml_data, code_model=code_model)
|
|
67
|
+
self.type = "IO"
|
|
68
|
+
|
|
69
|
+
@property
|
|
70
|
+
def serialization_type(self) -> str:
|
|
71
|
+
return self.type
|
|
72
|
+
|
|
73
|
+
def docstring_type(self, **kwargs: Any) -> str:
|
|
74
|
+
return f"{self.type}[bytes]"
|
|
75
|
+
|
|
76
|
+
def type_annotation(self, **kwargs: Any) -> str:
|
|
77
|
+
return f"{self.type}[bytes]"
|
|
78
|
+
|
|
79
|
+
def docstring_text(self, **kwargs: Any) -> str:
|
|
80
|
+
return f"{self.type}[bytes]"
|
|
81
|
+
|
|
82
|
+
@property
|
|
83
|
+
def default_template_representation_declaration(self) -> str:
|
|
84
|
+
return self.get_declaration(b"bytes")
|
|
85
|
+
|
|
86
|
+
def imports(self, **kwargs: Any) -> FileImport:
|
|
87
|
+
from .combined_type import CombinedType
|
|
88
|
+
from .operation import OperationBase
|
|
89
|
+
|
|
90
|
+
file_import = FileImport(self.code_model)
|
|
91
|
+
file_import.add_submodule_import("typing", "IO", ImportType.STDLIB)
|
|
92
|
+
operation = kwargs.get("operation")
|
|
93
|
+
if (
|
|
94
|
+
isinstance(operation, OperationBase)
|
|
95
|
+
and operation.parameters.has_body
|
|
96
|
+
and isinstance(operation.parameters.body_parameter.type, CombinedType)
|
|
97
|
+
):
|
|
98
|
+
file_import.add_submodule_import("io", "IOBase", ImportType.STDLIB)
|
|
99
|
+
return file_import
|
|
100
|
+
|
|
101
|
+
@property
|
|
102
|
+
def instance_check_template(self) -> str:
|
|
103
|
+
return "isinstance({}, (IOBase, bytes))"
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
class BinaryIteratorType(PrimitiveType):
|
|
107
|
+
def _iterator_name(self, **kwargs: Any) -> str:
|
|
108
|
+
return "AsyncIterator" if kwargs.pop("async_mode") else "Iterator"
|
|
109
|
+
|
|
110
|
+
@property
|
|
111
|
+
def serialization_type(self) -> str:
|
|
112
|
+
return "IO"
|
|
113
|
+
|
|
114
|
+
def docstring_type(self, **kwargs: Any) -> str:
|
|
115
|
+
return f"{self._iterator_name(**kwargs)}[bytes]"
|
|
116
|
+
|
|
117
|
+
def type_annotation(self, **kwargs: Any) -> str:
|
|
118
|
+
return f"{self._iterator_name(**kwargs)}[bytes]"
|
|
119
|
+
|
|
120
|
+
def docstring_text(self, **kwargs: Any) -> str:
|
|
121
|
+
return f"{self._iterator_name(**kwargs)}[bytes]"
|
|
122
|
+
|
|
123
|
+
@property
|
|
124
|
+
def default_template_representation_declaration(self) -> str:
|
|
125
|
+
return self.get_declaration(b"bytes")
|
|
126
|
+
|
|
127
|
+
def imports(self, **kwargs: Any) -> FileImport:
|
|
128
|
+
file_import = FileImport(self.code_model)
|
|
129
|
+
file_import.add_submodule_import("typing", self._iterator_name(**kwargs), ImportType.STDLIB)
|
|
130
|
+
return file_import
|
|
131
|
+
|
|
132
|
+
@property
|
|
133
|
+
def instance_check_template(self) -> str:
|
|
134
|
+
return "getattr({}, '__aiter__', None) is not None or getattr({}, '__iter__', None) is not None"
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
class AnyType(PrimitiveType):
|
|
138
|
+
@property
|
|
139
|
+
def serialization_type(self) -> str:
|
|
140
|
+
return "object"
|
|
141
|
+
|
|
142
|
+
def docstring_type(self, **kwargs: Any) -> str:
|
|
143
|
+
return "any"
|
|
144
|
+
|
|
145
|
+
def type_annotation(self, **kwargs: Any) -> str:
|
|
146
|
+
return "Any"
|
|
147
|
+
|
|
148
|
+
@property
|
|
149
|
+
def default_template_representation_declaration(self) -> str:
|
|
150
|
+
return self.get_declaration({})
|
|
151
|
+
|
|
152
|
+
def imports(self, **kwargs: Any) -> FileImport:
|
|
153
|
+
file_import = FileImport(self.code_model)
|
|
154
|
+
file_import.add_submodule_import("typing", "Any", ImportType.STDLIB, TypingSection.CONDITIONAL)
|
|
155
|
+
return file_import
|
|
156
|
+
|
|
157
|
+
@property
|
|
158
|
+
def instance_check_template(self) -> str:
|
|
159
|
+
raise ValueError("Shouldn't do instance check on an anytype, it can be anything")
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
class AnyObjectType(PrimitiveType):
|
|
163
|
+
@property
|
|
164
|
+
def serialization_type(self) -> str:
|
|
165
|
+
return "object"
|
|
166
|
+
|
|
167
|
+
def docstring_type(self, **kwargs: Any) -> str:
|
|
168
|
+
return "JSON"
|
|
169
|
+
|
|
170
|
+
def type_annotation(self, **kwargs: Any) -> str:
|
|
171
|
+
return "JSON"
|
|
172
|
+
|
|
173
|
+
@property
|
|
174
|
+
def default_template_representation_declaration(self) -> str:
|
|
175
|
+
return self.get_declaration({})
|
|
176
|
+
|
|
177
|
+
@property
|
|
178
|
+
def instance_check_template(self) -> str:
|
|
179
|
+
return "isinstance({}, MutableMapping)"
|
|
180
|
+
|
|
181
|
+
def imports(self, **kwargs: Any) -> FileImport:
|
|
182
|
+
file_import = FileImport(self.code_model)
|
|
183
|
+
file_import.define_mutable_mapping_type()
|
|
184
|
+
return file_import
|
|
185
|
+
|
|
186
|
+
@property
|
|
187
|
+
def type_description(self) -> str:
|
|
188
|
+
return "JSON"
|
|
189
|
+
|
|
190
|
+
|
|
191
|
+
class NumberType(PrimitiveType):
|
|
192
|
+
def __init__(self, yaml_data: Dict[str, Any], code_model: "CodeModel") -> None:
|
|
193
|
+
super().__init__(yaml_data=yaml_data, code_model=code_model)
|
|
194
|
+
self.precision: Optional[int] = yaml_data.get("precision")
|
|
195
|
+
self.multiple: Optional[int] = yaml_data.get("multipleOf")
|
|
196
|
+
self.maximum: Optional[int] = yaml_data.get("maximum")
|
|
197
|
+
self.minimum: Optional[int] = yaml_data.get("minimum")
|
|
198
|
+
self.exclusive_maximum: Optional[int] = yaml_data.get("exclusiveMaximum")
|
|
199
|
+
self.exclusive_minimum: Optional[int] = yaml_data.get("exclusiveMinimum")
|
|
200
|
+
|
|
201
|
+
@property
|
|
202
|
+
def serialization_constraints(self) -> List[str]:
|
|
203
|
+
validation_constraints = [
|
|
204
|
+
(f"maximum_ex={self.maximum}" if self.maximum is not None and self.exclusive_maximum else None),
|
|
205
|
+
(f"maximum={self.maximum}" if self.maximum is not None and not self.exclusive_maximum else None),
|
|
206
|
+
(f"minimum_ex={self.minimum}" if self.minimum is not None and self.exclusive_minimum else None),
|
|
207
|
+
(f"minimum={self.minimum}" if self.minimum is not None and not self.exclusive_minimum else None),
|
|
208
|
+
f"multiple={self.multiple}" if self.multiple else None,
|
|
209
|
+
]
|
|
210
|
+
return [x for x in validation_constraints if x is not None]
|
|
211
|
+
|
|
212
|
+
@property
|
|
213
|
+
def validation(self) -> Optional[Dict[str, Union[bool, int, str]]]:
|
|
214
|
+
validation: Dict[str, Union[bool, int, str]] = {}
|
|
215
|
+
if self.maximum is not None:
|
|
216
|
+
if self.exclusive_maximum:
|
|
217
|
+
validation["maximum_ex"] = self.maximum
|
|
218
|
+
else:
|
|
219
|
+
validation["maximum"] = self.maximum
|
|
220
|
+
if self.minimum is not None:
|
|
221
|
+
if self.exclusive_minimum:
|
|
222
|
+
validation["minimum_ex"] = self.minimum
|
|
223
|
+
else:
|
|
224
|
+
validation["minimum"] = self.minimum
|
|
225
|
+
if self.multiple:
|
|
226
|
+
validation["multiple"] = self.multiple
|
|
227
|
+
return validation or None
|
|
228
|
+
|
|
229
|
+
@property
|
|
230
|
+
def default_template_representation_declaration(self) -> str:
|
|
231
|
+
default_value = 0 if self.docstring_type() == "int" else 0.0
|
|
232
|
+
return self.get_declaration(default_value)
|
|
233
|
+
|
|
234
|
+
|
|
235
|
+
class IntegerType(NumberType):
|
|
236
|
+
|
|
237
|
+
def __init__(self, yaml_data: Dict[str, Any], code_model: "CodeModel") -> None:
|
|
238
|
+
super().__init__(yaml_data=yaml_data, code_model=code_model)
|
|
239
|
+
if yaml_data.get("encode") == "string":
|
|
240
|
+
self.encode = "str"
|
|
241
|
+
|
|
242
|
+
@property
|
|
243
|
+
def serialization_type(self) -> str:
|
|
244
|
+
return "int"
|
|
245
|
+
|
|
246
|
+
def docstring_type(self, **kwargs: Any) -> str:
|
|
247
|
+
return "int"
|
|
248
|
+
|
|
249
|
+
def type_annotation(self, **kwargs: Any) -> str:
|
|
250
|
+
return "int"
|
|
251
|
+
|
|
252
|
+
@property
|
|
253
|
+
def default_template_representation_declaration(self) -> str:
|
|
254
|
+
return self.get_declaration(0)
|
|
255
|
+
|
|
256
|
+
@property
|
|
257
|
+
def instance_check_template(self) -> str:
|
|
258
|
+
return "isinstance({}, int)"
|
|
259
|
+
|
|
260
|
+
|
|
261
|
+
class FloatType(NumberType):
|
|
262
|
+
@property
|
|
263
|
+
def serialization_type(self) -> str:
|
|
264
|
+
return "float"
|
|
265
|
+
|
|
266
|
+
def docstring_type(self, **kwargs: Any) -> str:
|
|
267
|
+
return "float"
|
|
268
|
+
|
|
269
|
+
def type_annotation(self, **kwargs: Any) -> str:
|
|
270
|
+
return "float"
|
|
271
|
+
|
|
272
|
+
@property
|
|
273
|
+
def default_template_representation_declaration(self) -> str:
|
|
274
|
+
return self.get_declaration(0.0)
|
|
275
|
+
|
|
276
|
+
@property
|
|
277
|
+
def instance_check_template(self) -> str:
|
|
278
|
+
return "isinstance({}, float)"
|
|
279
|
+
|
|
280
|
+
|
|
281
|
+
class DecimalType(NumberType):
|
|
282
|
+
@property
|
|
283
|
+
def serialization_type(self) -> str:
|
|
284
|
+
return "decimal"
|
|
285
|
+
|
|
286
|
+
def docstring_type(self, **kwargs: Any) -> str:
|
|
287
|
+
return "~" + self.type_annotation()
|
|
288
|
+
|
|
289
|
+
def type_annotation(self, **kwargs: Any) -> str:
|
|
290
|
+
return "decimal.Decimal"
|
|
291
|
+
|
|
292
|
+
def docstring_text(self, **kwargs: Any) -> str:
|
|
293
|
+
return self.type_annotation()
|
|
294
|
+
|
|
295
|
+
def get_declaration(self, value: decimal.Decimal) -> str:
|
|
296
|
+
return str(value)
|
|
297
|
+
|
|
298
|
+
def imports(self, **kwargs: Any) -> FileImport:
|
|
299
|
+
file_import = FileImport(self.code_model)
|
|
300
|
+
file_import.add_import("decimal", ImportType.STDLIB)
|
|
301
|
+
return file_import
|
|
302
|
+
|
|
303
|
+
@property
|
|
304
|
+
def default_template_representation_declaration(self) -> str:
|
|
305
|
+
return self.get_declaration(decimal.Decimal("0.0"))
|
|
306
|
+
|
|
307
|
+
@property
|
|
308
|
+
def instance_check_template(self) -> str:
|
|
309
|
+
return "isinstance({}, decimal.Decimal)"
|
|
310
|
+
|
|
311
|
+
|
|
312
|
+
class StringType(PrimitiveType):
|
|
313
|
+
def __init__(self, yaml_data: Dict[str, Any], code_model: "CodeModel") -> None:
|
|
314
|
+
super().__init__(yaml_data=yaml_data, code_model=code_model)
|
|
315
|
+
self.max_length: Optional[int] = yaml_data.get("maxLength")
|
|
316
|
+
self.min_length: Optional[int] = (
|
|
317
|
+
yaml_data.get("minLength", 0) if yaml_data.get("maxLength") else yaml_data.get("minLength")
|
|
318
|
+
)
|
|
319
|
+
self.pattern: Optional[str] = yaml_data.get("pattern")
|
|
320
|
+
|
|
321
|
+
@property
|
|
322
|
+
def serialization_constraints(self) -> List[str]:
|
|
323
|
+
validation_constraints = [
|
|
324
|
+
f"max_length={self.max_length}" if self.max_length is not None else None,
|
|
325
|
+
f"min_length={self.min_length}" if self.min_length is not None else None,
|
|
326
|
+
f"pattern={RawString(self.pattern)}" if self.pattern else None,
|
|
327
|
+
]
|
|
328
|
+
return [x for x in validation_constraints if x is not None]
|
|
329
|
+
|
|
330
|
+
@property
|
|
331
|
+
def validation(self) -> Optional[Dict[str, Union[bool, int, str]]]:
|
|
332
|
+
validation: Dict[str, Union[bool, int, str]] = {}
|
|
333
|
+
if self.max_length is not None:
|
|
334
|
+
validation["max_length"] = self.max_length
|
|
335
|
+
if self.min_length is not None:
|
|
336
|
+
validation["min_length"] = self.min_length
|
|
337
|
+
if self.pattern:
|
|
338
|
+
# https://github.com/Azure/autorest.python/issues/407
|
|
339
|
+
validation["pattern"] = RawString(self.pattern) # type: ignore
|
|
340
|
+
return validation or None
|
|
341
|
+
|
|
342
|
+
def get_declaration(self, value) -> str:
|
|
343
|
+
return f"'{value}'" if value == '"' else f'"{value}"'
|
|
344
|
+
|
|
345
|
+
@property
|
|
346
|
+
def serialization_type(self) -> str:
|
|
347
|
+
return "str"
|
|
348
|
+
|
|
349
|
+
def docstring_type(self, **kwargs: Any) -> str:
|
|
350
|
+
return "str"
|
|
351
|
+
|
|
352
|
+
@property
|
|
353
|
+
def instance_check_template(self) -> str:
|
|
354
|
+
return "isinstance({}, str)"
|
|
355
|
+
|
|
356
|
+
|
|
357
|
+
class DatetimeType(PrimitiveType):
|
|
358
|
+
def __init__(self, yaml_data: Dict[str, Any], code_model: "CodeModel") -> None:
|
|
359
|
+
super().__init__(yaml_data=yaml_data, code_model=code_model)
|
|
360
|
+
self.encode = (
|
|
361
|
+
"rfc3339"
|
|
362
|
+
if yaml_data.get("encode", "date-time") == "date-time" or yaml_data.get("encode", "date-time") == "rfc3339"
|
|
363
|
+
else "rfc7231"
|
|
364
|
+
)
|
|
365
|
+
|
|
366
|
+
@property
|
|
367
|
+
def serialization_type(self) -> str:
|
|
368
|
+
formats_to_attribute_type = {
|
|
369
|
+
"rfc3339": "iso-8601",
|
|
370
|
+
"rfc7231": "rfc-1123",
|
|
371
|
+
}
|
|
372
|
+
return formats_to_attribute_type[self.encode]
|
|
373
|
+
|
|
374
|
+
def docstring_type(self, **kwargs: Any) -> str:
|
|
375
|
+
return "~" + self.type_annotation()
|
|
376
|
+
|
|
377
|
+
def type_annotation(self, **kwargs: Any) -> str:
|
|
378
|
+
return "datetime.datetime"
|
|
379
|
+
|
|
380
|
+
def docstring_text(self, **kwargs: Any) -> str:
|
|
381
|
+
return "datetime"
|
|
382
|
+
|
|
383
|
+
def get_declaration(self, value: datetime.datetime) -> str:
|
|
384
|
+
"""Could be discussed, since technically I should return a datetime object,
|
|
385
|
+
but msrest will do fine.
|
|
386
|
+
"""
|
|
387
|
+
return f'"{value}"'
|
|
388
|
+
|
|
389
|
+
def imports(self, **kwargs: Any) -> FileImport:
|
|
390
|
+
file_import = FileImport(self.code_model)
|
|
391
|
+
file_import.add_import("datetime", ImportType.STDLIB)
|
|
392
|
+
return file_import
|
|
393
|
+
|
|
394
|
+
@property
|
|
395
|
+
def default_template_representation_declaration(self):
|
|
396
|
+
return self.get_declaration(datetime.datetime(2020, 2, 20))
|
|
397
|
+
|
|
398
|
+
@property
|
|
399
|
+
def instance_check_template(self) -> str:
|
|
400
|
+
return "isinstance({}, datetime.datetime)"
|
|
401
|
+
|
|
402
|
+
def imports_for_sample(self) -> FileImport:
|
|
403
|
+
file_import = super().imports_for_sample()
|
|
404
|
+
file_import.add_import("isodate", ImportType.STDLIB)
|
|
405
|
+
return file_import
|
|
406
|
+
|
|
407
|
+
@staticmethod
|
|
408
|
+
def serialize_sample_value(value: Any) -> str:
|
|
409
|
+
return f"isodate.parse_datetime({repr(value)})"
|
|
410
|
+
|
|
411
|
+
|
|
412
|
+
class TimeType(PrimitiveType):
|
|
413
|
+
@property
|
|
414
|
+
def serialization_type(self) -> str:
|
|
415
|
+
return "time"
|
|
416
|
+
|
|
417
|
+
def docstring_type(self, **kwargs: Any) -> str:
|
|
418
|
+
return "~" + self.type_annotation()
|
|
419
|
+
|
|
420
|
+
def type_annotation(self, **kwargs: Any) -> str:
|
|
421
|
+
return "datetime.time"
|
|
422
|
+
|
|
423
|
+
def docstring_text(self, **kwargs: Any) -> str:
|
|
424
|
+
return "time"
|
|
425
|
+
|
|
426
|
+
def get_declaration(self, value: datetime.time) -> str:
|
|
427
|
+
"""Could be discussed, since technically I should return a time object,
|
|
428
|
+
but msrest will do fine.
|
|
429
|
+
"""
|
|
430
|
+
return f'"{value}"'
|
|
431
|
+
|
|
432
|
+
def imports(self, **kwargs: Any) -> FileImport:
|
|
433
|
+
file_import = FileImport(self.code_model)
|
|
434
|
+
file_import.add_import("datetime", ImportType.STDLIB)
|
|
435
|
+
return file_import
|
|
436
|
+
|
|
437
|
+
@property
|
|
438
|
+
def default_template_representation_declaration(self) -> str:
|
|
439
|
+
return self.get_declaration(datetime.time(12, 30, 0))
|
|
440
|
+
|
|
441
|
+
@property
|
|
442
|
+
def instance_check_template(self) -> str:
|
|
443
|
+
return "isinstance({}, datetime.time)"
|
|
444
|
+
|
|
445
|
+
def imports_for_sample(self) -> FileImport:
|
|
446
|
+
file_import = super().imports_for_sample()
|
|
447
|
+
file_import.add_import("isodate", ImportType.STDLIB)
|
|
448
|
+
return file_import
|
|
449
|
+
|
|
450
|
+
@staticmethod
|
|
451
|
+
def serialize_sample_value(value: Any) -> str:
|
|
452
|
+
return f"isodate.parse_time({repr(value)})"
|
|
453
|
+
|
|
454
|
+
|
|
455
|
+
class UnixTimeType(PrimitiveType):
|
|
456
|
+
@property
|
|
457
|
+
def encode(self) -> str:
|
|
458
|
+
return "unix-timestamp"
|
|
459
|
+
|
|
460
|
+
@property
|
|
461
|
+
def serialization_type(self) -> str:
|
|
462
|
+
return "unix-time"
|
|
463
|
+
|
|
464
|
+
def docstring_type(self, **kwargs: Any) -> str:
|
|
465
|
+
return "~" + self.type_annotation()
|
|
466
|
+
|
|
467
|
+
def type_annotation(self, **kwargs: Any) -> str:
|
|
468
|
+
return "datetime.datetime"
|
|
469
|
+
|
|
470
|
+
def docstring_text(self, **kwargs: Any) -> str:
|
|
471
|
+
return "datetime"
|
|
472
|
+
|
|
473
|
+
def get_declaration(self, value: datetime.datetime) -> str:
|
|
474
|
+
"""Could be discussed, since technically I should return a datetime object,
|
|
475
|
+
but msrest will do fine.
|
|
476
|
+
"""
|
|
477
|
+
return f'"{value}"'
|
|
478
|
+
|
|
479
|
+
def imports(self, **kwargs: Any) -> FileImport:
|
|
480
|
+
file_import = FileImport(self.code_model)
|
|
481
|
+
file_import.add_import("datetime", ImportType.STDLIB)
|
|
482
|
+
return file_import
|
|
483
|
+
|
|
484
|
+
@property
|
|
485
|
+
def default_template_representation_declaration(self) -> str:
|
|
486
|
+
return self.get_declaration(datetime.datetime(2020, 2, 20))
|
|
487
|
+
|
|
488
|
+
@property
|
|
489
|
+
def instance_check_template(self) -> str:
|
|
490
|
+
return "isinstance({}, datetime.time)"
|
|
491
|
+
|
|
492
|
+
def imports_for_sample(self) -> FileImport:
|
|
493
|
+
file_import = super().imports_for_sample()
|
|
494
|
+
file_import.add_import("datetime", ImportType.STDLIB)
|
|
495
|
+
return file_import
|
|
496
|
+
|
|
497
|
+
@staticmethod
|
|
498
|
+
def serialize_sample_value(value: Any) -> str:
|
|
499
|
+
return f"datetime.datetime.fromtimestamp({repr(value)}, datetime.timezone.utc)"
|
|
500
|
+
|
|
501
|
+
|
|
502
|
+
class DateType(PrimitiveType):
|
|
503
|
+
@property
|
|
504
|
+
def serialization_type(self) -> str:
|
|
505
|
+
return "date"
|
|
506
|
+
|
|
507
|
+
def docstring_type(self, **kwargs: Any) -> str:
|
|
508
|
+
return "~" + self.type_annotation()
|
|
509
|
+
|
|
510
|
+
def type_annotation(self, **kwargs: Any) -> str:
|
|
511
|
+
return "datetime.date"
|
|
512
|
+
|
|
513
|
+
def docstring_text(self, **kwargs: Any) -> str:
|
|
514
|
+
return "date"
|
|
515
|
+
|
|
516
|
+
def get_declaration(self, value: datetime.date) -> str:
|
|
517
|
+
"""Could be discussed, since technically I should return a datetime object,
|
|
518
|
+
but msrest will do fine.
|
|
519
|
+
"""
|
|
520
|
+
return f'"{value}"'
|
|
521
|
+
|
|
522
|
+
def imports(self, **kwargs: Any) -> FileImport:
|
|
523
|
+
file_import = FileImport(self.code_model)
|
|
524
|
+
file_import.add_import("datetime", ImportType.STDLIB)
|
|
525
|
+
return file_import
|
|
526
|
+
|
|
527
|
+
@property
|
|
528
|
+
def default_template_representation_declaration(self) -> str:
|
|
529
|
+
return self.get_declaration(datetime.date(2020, 2, 20))
|
|
530
|
+
|
|
531
|
+
@property
|
|
532
|
+
def instance_check_template(self) -> str:
|
|
533
|
+
return "isinstance({}, datetime.date)"
|
|
534
|
+
|
|
535
|
+
def imports_for_sample(self) -> FileImport:
|
|
536
|
+
file_import = super().imports_for_sample()
|
|
537
|
+
file_import.add_import("isodate", ImportType.STDLIB)
|
|
538
|
+
return file_import
|
|
539
|
+
|
|
540
|
+
@staticmethod
|
|
541
|
+
def serialize_sample_value(value: Any) -> str:
|
|
542
|
+
return f"isodate.parse_date({repr(value)})"
|
|
543
|
+
|
|
544
|
+
|
|
545
|
+
class DurationType(PrimitiveType):
|
|
546
|
+
@property
|
|
547
|
+
def serialization_type(self) -> str:
|
|
548
|
+
return "duration"
|
|
549
|
+
|
|
550
|
+
def docstring_type(self, **kwargs: Any) -> str:
|
|
551
|
+
return "~" + self.type_annotation()
|
|
552
|
+
|
|
553
|
+
def type_annotation(self, **kwargs: Any) -> str:
|
|
554
|
+
return "datetime.timedelta"
|
|
555
|
+
|
|
556
|
+
def docstring_text(self, **kwargs: Any) -> str:
|
|
557
|
+
return "timedelta"
|
|
558
|
+
|
|
559
|
+
def get_declaration(self, value: datetime.timedelta) -> str:
|
|
560
|
+
"""Could be discussed, since technically I should return a datetime object,
|
|
561
|
+
but msrest will do fine.
|
|
562
|
+
"""
|
|
563
|
+
return f'"{value}"'
|
|
564
|
+
|
|
565
|
+
def imports(self, **kwargs: Any) -> FileImport:
|
|
566
|
+
file_import = FileImport(self.code_model)
|
|
567
|
+
file_import.add_import("datetime", ImportType.STDLIB)
|
|
568
|
+
return file_import
|
|
569
|
+
|
|
570
|
+
@property
|
|
571
|
+
def default_template_representation_declaration(self) -> str:
|
|
572
|
+
return self.get_declaration(datetime.timedelta(1))
|
|
573
|
+
|
|
574
|
+
@property
|
|
575
|
+
def instance_check_template(self) -> str:
|
|
576
|
+
return "isinstance({}, datetime.timedelta)"
|
|
577
|
+
|
|
578
|
+
def imports_for_sample(self) -> FileImport:
|
|
579
|
+
file_import = super().imports_for_sample()
|
|
580
|
+
file_import.add_import("isodate", ImportType.STDLIB)
|
|
581
|
+
return file_import
|
|
582
|
+
|
|
583
|
+
@staticmethod
|
|
584
|
+
def serialize_sample_value(value: Any) -> str:
|
|
585
|
+
return f"isodate.parse_duration({repr(value)})"
|
|
586
|
+
|
|
587
|
+
|
|
588
|
+
class ByteArraySchema(PrimitiveType):
|
|
589
|
+
def __init__(self, yaml_data: Dict[str, Any], code_model: "CodeModel") -> None:
|
|
590
|
+
super().__init__(yaml_data=yaml_data, code_model=code_model)
|
|
591
|
+
self.encode = yaml_data.get("encode", "base64")
|
|
592
|
+
|
|
593
|
+
@property
|
|
594
|
+
def serialization_type(self) -> str:
|
|
595
|
+
if self.encode == "base64url":
|
|
596
|
+
return "base64"
|
|
597
|
+
return "bytearray"
|
|
598
|
+
|
|
599
|
+
def docstring_type(self, **kwargs: Any) -> str:
|
|
600
|
+
return "bytes"
|
|
601
|
+
|
|
602
|
+
def get_declaration(self, value: str) -> str:
|
|
603
|
+
return f'bytes("{value}", encoding="utf-8")'
|
|
604
|
+
|
|
605
|
+
@property
|
|
606
|
+
def instance_check_template(self) -> str:
|
|
607
|
+
return "isinstance({}, bytes)"
|
|
608
|
+
|
|
609
|
+
|
|
610
|
+
class SdkCoreType(PrimitiveType):
|
|
611
|
+
def __init__(self, yaml_data: Dict[str, Any], code_model: "CodeModel") -> None:
|
|
612
|
+
super().__init__(yaml_data=yaml_data, code_model=code_model)
|
|
613
|
+
self.name = yaml_data.get("name", "")
|
|
614
|
+
self.submodule = yaml_data.get("submodule", "")
|
|
615
|
+
|
|
616
|
+
def docstring_type(self, **kwargs: Any) -> str:
|
|
617
|
+
return f"~{self.code_model.core_library}.{self.type_annotation(**kwargs)}"
|
|
618
|
+
|
|
619
|
+
def type_annotation(self, **kwargs: Any) -> str:
|
|
620
|
+
return self.name
|
|
621
|
+
|
|
622
|
+
def imports(self, **kwargs: Any) -> FileImport:
|
|
623
|
+
file_import = super().imports(**kwargs)
|
|
624
|
+
file_import.add_submodule_import(self.submodule, self.name, ImportType.SDKCORE)
|
|
625
|
+
return file_import
|
|
626
|
+
|
|
627
|
+
@property
|
|
628
|
+
def instance_check_template(self) -> str:
|
|
629
|
+
return f"isinstance({{}}, {self.name})"
|
|
630
|
+
|
|
631
|
+
@property
|
|
632
|
+
def serialization_type(self) -> str:
|
|
633
|
+
return self.name
|
|
634
|
+
|
|
635
|
+
|
|
636
|
+
class MultiPartFileType(PrimitiveType):
|
|
637
|
+
def __init__(self, yaml_data: Dict[str, Any], code_model: "CodeModel") -> None:
|
|
638
|
+
super().__init__(yaml_data=yaml_data, code_model=code_model)
|
|
639
|
+
self.name = "FileType"
|
|
640
|
+
|
|
641
|
+
def type_annotation(self, **kwargs: Any) -> str:
|
|
642
|
+
return self.name
|
|
643
|
+
|
|
644
|
+
def docstring_type(self, **kwargs: Any) -> str:
|
|
645
|
+
return f"~{self.code_model.namespace}._vendor.{self.name}"
|
|
646
|
+
|
|
647
|
+
def imports(self, **kwargs: Any) -> FileImport:
|
|
648
|
+
file_import = super().imports(**kwargs)
|
|
649
|
+
relative_path = "..." if kwargs.get("async_mode") else ".."
|
|
650
|
+
file_import.add_submodule_import(f"{relative_path}_vendor", self.name, ImportType.LOCAL)
|
|
651
|
+
return file_import
|
|
652
|
+
|
|
653
|
+
@property
|
|
654
|
+
def default_template_representation_declaration(self) -> str:
|
|
655
|
+
return '"filetype"' if self.code_model.for_test else "filetype"
|
|
656
|
+
|
|
657
|
+
@property
|
|
658
|
+
def instance_check_template(self) -> str:
|
|
659
|
+
return f"isinstance({{}}, {self.name})"
|