@autorest/python 6.2.6 → 6.2.8
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/black/__init__.py +3 -2
- package/autorest/codegen/__init__.py +1 -1
- package/autorest/codegen/models/base.py +9 -1
- package/autorest/codegen/models/client.py +4 -2
- package/autorest/codegen/models/operation.py +6 -6
- package/autorest/codegen/models/operation_group.py +0 -6
- package/autorest/codegen/models/primitive_types.py +50 -0
- package/autorest/codegen/serializers/__init__.py +17 -8
- package/autorest/codegen/serializers/builder_serializer.py +61 -24
- package/autorest/codegen/serializers/client_serializer.py +4 -3
- package/autorest/codegen/serializers/metadata_serializer.py +7 -1
- package/autorest/codegen/serializers/model_serializer.py +8 -12
- package/autorest/codegen/serializers/operation_groups_serializer.py +1 -0
- package/autorest/codegen/serializers/parameter_serializer.py +3 -3
- package/autorest/codegen/serializers/patch_serializer.py +2 -4
- package/autorest/codegen/serializers/sample_serializer.py +23 -14
- package/autorest/codegen/serializers/utils.py +6 -0
- package/autorest/codegen/templates/client.py.jinja2 +3 -12
- package/autorest/codegen/templates/config.py.jinja2 +2 -5
- package/autorest/codegen/templates/keywords.jinja2 +2 -2
- package/autorest/codegen/templates/metadata.json.jinja2 +2 -2
- package/autorest/codegen/templates/model_base.py.jinja2 +2 -4
- package/autorest/codegen/templates/model_dpg.py.jinja2 +1 -1
- package/autorest/codegen/templates/request_builder.py.jinja2 +1 -1
- package/autorest/codegen/templates/serialization.py.jinja2 +286 -325
- package/autorest/jsonrpc/stdstream.py +1 -1
- package/autorest/m2r/__init__.py +2 -2
- package/autorest/multiapi/models/imports.py +13 -5
- package/autorest/multiapi/serializers/import_serializer.py +1 -1
- package/autorest/multiapi/templates/multiapi_config.py.jinja2 +2 -8
- package/autorest/multiapi/templates/multiapi_service_client.py.jinja2 +1 -1
- package/autorest/postprocess/__init__.py +5 -4
- package/package.json +1 -1
|
@@ -8,13 +8,14 @@ from pathlib import Path
|
|
|
8
8
|
import os
|
|
9
9
|
from typing import Any, Dict
|
|
10
10
|
import black
|
|
11
|
+
from black.report import NothingChanged
|
|
11
12
|
|
|
12
13
|
from .. import Plugin, PluginAutorest
|
|
13
14
|
from .._utils import parse_args
|
|
14
15
|
|
|
15
16
|
logging.getLogger("blib2to3").setLevel(logging.ERROR)
|
|
16
17
|
|
|
17
|
-
_BLACK_MODE = black.Mode()
|
|
18
|
+
_BLACK_MODE = black.Mode() # pyright: ignore [reportPrivateImportUsage]
|
|
18
19
|
_BLACK_MODE.line_length = 120
|
|
19
20
|
|
|
20
21
|
|
|
@@ -42,7 +43,7 @@ class BlackScriptPlugin(Plugin): # pylint: disable=abstract-method
|
|
|
42
43
|
file_content = black.format_file_contents(
|
|
43
44
|
file_content, fast=True, mode=_BLACK_MODE
|
|
44
45
|
)
|
|
45
|
-
except
|
|
46
|
+
except NothingChanged:
|
|
46
47
|
pass
|
|
47
48
|
self.write_file(file, file_content)
|
|
48
49
|
|
|
@@ -293,7 +293,7 @@ class CodeGeneratorAutorest(CodeGenerator, PluginAutorest):
|
|
|
293
293
|
# Parse the received YAML
|
|
294
294
|
return yaml.safe_load(file_content)
|
|
295
295
|
|
|
296
|
-
def get_serializer(self, code_model: CodeModel):
|
|
296
|
+
def get_serializer(self, code_model: CodeModel):
|
|
297
297
|
return JinjaSerializerAutorest(
|
|
298
298
|
self._autorestapi,
|
|
299
299
|
code_model,
|
|
@@ -32,7 +32,7 @@ class BaseModel:
|
|
|
32
32
|
return f"<{self.__class__.__name__}>"
|
|
33
33
|
|
|
34
34
|
|
|
35
|
-
class BaseType(BaseModel, ABC):
|
|
35
|
+
class BaseType(BaseModel, ABC): # pylint: disable=too-many-public-methods
|
|
36
36
|
"""This is the base class for all types.
|
|
37
37
|
|
|
38
38
|
:param yaml_data: the yaml data for this schema
|
|
@@ -60,6 +60,14 @@ class BaseType(BaseModel, ABC):
|
|
|
60
60
|
def imports_for_multiapi(self, **kwargs: Any) -> FileImport:
|
|
61
61
|
return self.imports(**kwargs)
|
|
62
62
|
|
|
63
|
+
@staticmethod
|
|
64
|
+
def imports_for_sample() -> FileImport:
|
|
65
|
+
return FileImport()
|
|
66
|
+
|
|
67
|
+
@staticmethod
|
|
68
|
+
def serialize_sample_value(value: Any) -> str:
|
|
69
|
+
return repr(value)
|
|
70
|
+
|
|
63
71
|
@property
|
|
64
72
|
def xml_metadata(self) -> Dict[str, Any]:
|
|
65
73
|
"""XML metadata for the type, if the type has it."""
|
|
@@ -86,7 +86,7 @@ class Client(_ClientConfigBase[ClientGlobalParameterList]):
|
|
|
86
86
|
client=self,
|
|
87
87
|
)
|
|
88
88
|
if request_builder.overloads:
|
|
89
|
-
request_builders.extend(request_builder.overloads)
|
|
89
|
+
request_builders.extend(request_builder.overloads)
|
|
90
90
|
request_builders.append(request_builder)
|
|
91
91
|
if operation_yaml.get("nextOperation"):
|
|
92
92
|
# i am a paging operation and i have a next operation.
|
|
@@ -176,7 +176,9 @@ class Client(_ClientConfigBase[ClientGlobalParameterList]):
|
|
|
176
176
|
for gp in self.parameters:
|
|
177
177
|
if gp.method_location == ParameterMethodLocation.KWARG:
|
|
178
178
|
continue
|
|
179
|
-
file_import.merge(
|
|
179
|
+
file_import.merge(
|
|
180
|
+
gp.imports(async_mode, relative_path=".." if async_mode else ".")
|
|
181
|
+
)
|
|
180
182
|
file_import.add_submodule_import(
|
|
181
183
|
"._configuration",
|
|
182
184
|
f"{self.name}Configuration",
|
|
@@ -160,10 +160,10 @@ class OperationBase( # pylint: disable=too-many-public-methods
|
|
|
160
160
|
isinstance(r.type, ModelType) for r in self.responses
|
|
161
161
|
):
|
|
162
162
|
r = next(r for r in self.responses if isinstance(r.type, ModelType))
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
163
|
+
item_type = getattr(r, "item_type", getattr(r, "type"))
|
|
164
|
+
if item_type:
|
|
165
|
+
type_name = item_type.docstring_text(**kwargs)
|
|
166
|
+
retval += f". The {type_name} is compatible with MutableMapping"
|
|
167
167
|
return retval
|
|
168
168
|
|
|
169
169
|
def response_docstring_type(self, **kwargs) -> str:
|
|
@@ -344,9 +344,9 @@ class OperationBase( # pylint: disable=too-many-public-methods
|
|
|
344
344
|
)
|
|
345
345
|
|
|
346
346
|
if self.has_kwargs_to_pop_with_default(
|
|
347
|
-
self.parameters.kwargs_to_pop, ParameterLocation.HEADER
|
|
347
|
+
self.parameters.kwargs_to_pop, ParameterLocation.HEADER # type: ignore
|
|
348
348
|
) or self.has_kwargs_to_pop_with_default(
|
|
349
|
-
self.parameters.kwargs_to_pop, ParameterLocation.QUERY
|
|
349
|
+
self.parameters.kwargs_to_pop, ParameterLocation.QUERY # type: ignore
|
|
350
350
|
):
|
|
351
351
|
file_import.add_submodule_import(
|
|
352
352
|
"azure.core.utils", "case_insensitive_dict", ImportType.AZURECORE
|
|
@@ -68,12 +68,6 @@ class OperationGroup(BaseModel):
|
|
|
68
68
|
retval = add_to_pylint_disable(retval, "abstract-class-instantiated")
|
|
69
69
|
return retval
|
|
70
70
|
|
|
71
|
-
@property
|
|
72
|
-
def mypy_ignore(self) -> str:
|
|
73
|
-
if self.has_abstract_operations:
|
|
74
|
-
return " # type: ignore"
|
|
75
|
-
return ""
|
|
76
|
-
|
|
77
71
|
@property
|
|
78
72
|
def need_validation(self) -> bool:
|
|
79
73
|
"""Whether any of its operations need validation"""
|
|
@@ -384,6 +384,16 @@ class DatetimeType(PrimitiveType):
|
|
|
384
384
|
def instance_check_template(self) -> str:
|
|
385
385
|
return "isinstance({}, datetime.datetime)"
|
|
386
386
|
|
|
387
|
+
@staticmethod
|
|
388
|
+
def imports_for_sample() -> FileImport:
|
|
389
|
+
file_import = super(DatetimeType, DatetimeType).imports_for_sample()
|
|
390
|
+
file_import.add_import("isodate", ImportType.STDLIB)
|
|
391
|
+
return file_import
|
|
392
|
+
|
|
393
|
+
@staticmethod
|
|
394
|
+
def serialize_sample_value(value: Any) -> str:
|
|
395
|
+
return f"isodate.parse_datetime({repr(value)})"
|
|
396
|
+
|
|
387
397
|
|
|
388
398
|
class TimeType(PrimitiveType):
|
|
389
399
|
@property
|
|
@@ -418,6 +428,16 @@ class TimeType(PrimitiveType):
|
|
|
418
428
|
def instance_check_template(self) -> str:
|
|
419
429
|
return "isinstance({}, datetime.time)"
|
|
420
430
|
|
|
431
|
+
@staticmethod
|
|
432
|
+
def imports_for_sample() -> FileImport:
|
|
433
|
+
file_import = super(TimeType, TimeType).imports_for_sample()
|
|
434
|
+
file_import.add_import("isodate", ImportType.STDLIB)
|
|
435
|
+
return file_import
|
|
436
|
+
|
|
437
|
+
@staticmethod
|
|
438
|
+
def serialize_sample_value(value: Any) -> str:
|
|
439
|
+
return f"isodate.parse_time({repr(value)})"
|
|
440
|
+
|
|
421
441
|
|
|
422
442
|
class UnixTimeType(PrimitiveType):
|
|
423
443
|
@property
|
|
@@ -452,6 +472,16 @@ class UnixTimeType(PrimitiveType):
|
|
|
452
472
|
def instance_check_template(self) -> str:
|
|
453
473
|
return "isinstance({}, datetime.time)"
|
|
454
474
|
|
|
475
|
+
@staticmethod
|
|
476
|
+
def imports_for_sample() -> FileImport:
|
|
477
|
+
file_import = super(UnixTimeType, UnixTimeType).imports_for_sample()
|
|
478
|
+
file_import.add_import("datetime", ImportType.STDLIB)
|
|
479
|
+
return file_import
|
|
480
|
+
|
|
481
|
+
@staticmethod
|
|
482
|
+
def serialize_sample_value(value: Any) -> str:
|
|
483
|
+
return f"datetime.datetime.fromtimestamp({repr(value)}, datetime.timezone.utc)"
|
|
484
|
+
|
|
455
485
|
|
|
456
486
|
class DateType(PrimitiveType):
|
|
457
487
|
@property
|
|
@@ -486,6 +516,16 @@ class DateType(PrimitiveType):
|
|
|
486
516
|
def instance_check_template(self) -> str:
|
|
487
517
|
return "isinstance({}, datetime.date)"
|
|
488
518
|
|
|
519
|
+
@staticmethod
|
|
520
|
+
def imports_for_sample() -> FileImport:
|
|
521
|
+
file_import = super(DateType, DateType).imports_for_sample()
|
|
522
|
+
file_import.add_import("isodate", ImportType.STDLIB)
|
|
523
|
+
return file_import
|
|
524
|
+
|
|
525
|
+
@staticmethod
|
|
526
|
+
def serialize_sample_value(value: Any) -> str:
|
|
527
|
+
return f"isodate.parse_date({repr(value)})"
|
|
528
|
+
|
|
489
529
|
|
|
490
530
|
class DurationType(PrimitiveType):
|
|
491
531
|
@property
|
|
@@ -520,6 +560,16 @@ class DurationType(PrimitiveType):
|
|
|
520
560
|
def instance_check_template(self) -> str:
|
|
521
561
|
return "isinstance({}, datetime.timedelta)"
|
|
522
562
|
|
|
563
|
+
@staticmethod
|
|
564
|
+
def imports_for_sample() -> FileImport:
|
|
565
|
+
file_import = super(DurationType, DurationType).imports_for_sample()
|
|
566
|
+
file_import.add_import("isodate", ImportType.STDLIB)
|
|
567
|
+
return file_import
|
|
568
|
+
|
|
569
|
+
@staticmethod
|
|
570
|
+
def serialize_sample_value(value: Any) -> str:
|
|
571
|
+
return f"isodate.parse_duration({repr(value)})"
|
|
572
|
+
|
|
523
573
|
|
|
524
574
|
class ByteArraySchema(PrimitiveType):
|
|
525
575
|
def __init__(self, yaml_data: Dict[str, Any], code_model: "CodeModel") -> None:
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
# license information.
|
|
5
5
|
# --------------------------------------------------------------------------
|
|
6
6
|
import logging
|
|
7
|
-
from typing import List, Optional, Any, Union
|
|
7
|
+
from typing import List, Optional, Any, Union
|
|
8
8
|
from pathlib import Path
|
|
9
9
|
from jinja2 import PackageLoader, Environment, FileSystemLoader, StrictUndefined
|
|
10
10
|
|
|
@@ -28,6 +28,7 @@ from .request_builders_serializer import RequestBuildersSerializer
|
|
|
28
28
|
from .patch_serializer import PatchSerializer
|
|
29
29
|
from .sample_serializer import SampleSerializer
|
|
30
30
|
from ..._utils import to_snake_case
|
|
31
|
+
from .utils import extract_sample_name
|
|
31
32
|
|
|
32
33
|
_LOGGER = logging.getLogger(__name__)
|
|
33
34
|
|
|
@@ -141,7 +142,7 @@ class JinjaSerializer(ReaderAndWriter): # pylint: disable=abstract-method
|
|
|
141
142
|
namespace_path = (
|
|
142
143
|
Path(".")
|
|
143
144
|
if self.code_model.options["no_namespace_folders"]
|
|
144
|
-
else Path(*
|
|
145
|
+
else Path(*self._name_space().split("."))
|
|
145
146
|
)
|
|
146
147
|
|
|
147
148
|
p = namespace_path.parent
|
|
@@ -190,7 +191,7 @@ class JinjaSerializer(ReaderAndWriter): # pylint: disable=abstract-method
|
|
|
190
191
|
if self.read_file(namespace_path / Path("models.py")):
|
|
191
192
|
self.write_file(
|
|
192
193
|
namespace_path / Path("models.py"),
|
|
193
|
-
|
|
194
|
+
self.read_file(namespace_path / Path("models.py")),
|
|
194
195
|
)
|
|
195
196
|
|
|
196
197
|
def _serialize_and_write_package_files(self, namespace_path: Path) -> None:
|
|
@@ -531,9 +532,17 @@ class JinjaSerializer(ReaderAndWriter): # pylint: disable=abstract-method
|
|
|
531
532
|
namespace_path / Path("_metadata.json"), metadata_serializer.serialize()
|
|
532
533
|
)
|
|
533
534
|
|
|
535
|
+
def _name_space(self) -> str:
|
|
536
|
+
if self.code_model.namespace.count(".") >= (
|
|
537
|
+
self.code_model.options["package_name"] or ""
|
|
538
|
+
).count("-"):
|
|
539
|
+
return self.code_model.namespace
|
|
540
|
+
|
|
541
|
+
return self.code_model.options["package_name"].replace("-", ".")
|
|
542
|
+
|
|
534
543
|
# find root folder where "setup.py" is
|
|
535
544
|
def _package_root_folder(self, namespace_path: Path) -> Path:
|
|
536
|
-
return namespace_path / Path("../" * (self.
|
|
545
|
+
return namespace_path / Path("../" * (self._name_space().count(".") + 1))
|
|
537
546
|
|
|
538
547
|
def _serialize_and_write_sample(self, env: Environment, namespace_path: Path):
|
|
539
548
|
out_path = self._package_root_folder(namespace_path) / Path("generated_samples")
|
|
@@ -543,8 +552,9 @@ class JinjaSerializer(ReaderAndWriter): # pylint: disable=abstract-method
|
|
|
543
552
|
samples = operation.yaml_data["samples"]
|
|
544
553
|
if not samples or operation.name.startswith("_"):
|
|
545
554
|
continue
|
|
546
|
-
for
|
|
547
|
-
|
|
555
|
+
for value in samples.values():
|
|
556
|
+
file = value.get("x-ms-original-file", "sample.json")
|
|
557
|
+
file_name = to_snake_case(extract_sample_name(file)) + ".py"
|
|
548
558
|
try:
|
|
549
559
|
self.write_file(
|
|
550
560
|
out_path / file_name,
|
|
@@ -555,12 +565,11 @@ class JinjaSerializer(ReaderAndWriter): # pylint: disable=abstract-method
|
|
|
555
565
|
operation=operation,
|
|
556
566
|
sample=value,
|
|
557
567
|
file_name=file_name,
|
|
558
|
-
sample_origin_name=key,
|
|
559
568
|
).serialize(),
|
|
560
569
|
)
|
|
561
570
|
except Exception as e: # pylint: disable=broad-except
|
|
562
571
|
# sample generation shall not block code generation, so just log error
|
|
563
|
-
log_error = f"error happens in sample {
|
|
572
|
+
log_error = f"error happens in sample {file}: {e}"
|
|
564
573
|
_LOGGER.error(log_error)
|
|
565
574
|
|
|
566
575
|
|
|
@@ -583,8 +583,10 @@ class _OperationSerializer(
|
|
|
583
583
|
return retval
|
|
584
584
|
|
|
585
585
|
def make_pipeline_call(self, builder: OperationType) -> List[str]:
|
|
586
|
+
type_ignore = self.async_mode and builder.group_name == "" # is in a mixin
|
|
586
587
|
return [
|
|
587
|
-
f"pipeline_response = {self._call_method}self._client._pipeline.run(
|
|
588
|
+
f"pipeline_response: PipelineResponse = {self._call_method}self._client._pipeline.run( "
|
|
589
|
+
+ f"{'# type: ignore' if type_ignore else ''} # pylint: disable=protected-access",
|
|
588
590
|
" request,",
|
|
589
591
|
f" stream={builder.has_stream_response},",
|
|
590
592
|
" **kwargs",
|
|
@@ -627,24 +629,22 @@ class _OperationSerializer(
|
|
|
627
629
|
check_kwarg_dict=True,
|
|
628
630
|
pop_headers_kwarg=PopKwargType.CASE_INSENSITIVE
|
|
629
631
|
if builder.has_kwargs_to_pop_with_default(
|
|
630
|
-
kwargs_to_pop, ParameterLocation.HEADER
|
|
632
|
+
kwargs_to_pop, ParameterLocation.HEADER # type: ignore
|
|
631
633
|
)
|
|
632
634
|
else PopKwargType.SIMPLE,
|
|
633
635
|
pop_params_kwarg=PopKwargType.CASE_INSENSITIVE
|
|
634
636
|
if builder.has_kwargs_to_pop_with_default(
|
|
635
|
-
kwargs_to_pop, ParameterLocation.QUERY
|
|
637
|
+
kwargs_to_pop, ParameterLocation.QUERY # type: ignore
|
|
636
638
|
)
|
|
637
639
|
else PopKwargType.SIMPLE,
|
|
638
640
|
check_client_input=not self.code_model.options["multiapi"],
|
|
639
641
|
)
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
)
|
|
642
|
+
cls_annotation = builder.cls_type_annotation(async_mode=self.async_mode)
|
|
643
|
+
kwargs.append(f"cls: {cls_annotation} = kwargs.pop('cls', None)")
|
|
644
|
+
if any(x.startswith("_") for x in cls_annotation.split(".")):
|
|
645
|
+
kwargs[-1] += " # pylint: disable=protected-access"
|
|
643
646
|
return kwargs
|
|
644
647
|
|
|
645
|
-
def cls_type_annotation(self, builder: OperationType) -> str:
|
|
646
|
-
return f"# type: {builder.cls_type_annotation(async_mode=self.async_mode)}"
|
|
647
|
-
|
|
648
648
|
def response_docstring(self, builder: OperationType) -> List[str]:
|
|
649
649
|
response_str = (
|
|
650
650
|
f":return: {builder.response_docstring_text(async_mode=self.async_mode)}"
|
|
@@ -847,8 +847,18 @@ class _OperationSerializer(
|
|
|
847
847
|
# in paging operations with a single swagger operation defintion,
|
|
848
848
|
# we skip passing query params when building the next request
|
|
849
849
|
continue
|
|
850
|
+
type_ignore = (
|
|
851
|
+
parameter.grouped_by
|
|
852
|
+
and parameter.client_default_value is not None
|
|
853
|
+
and next(
|
|
854
|
+
p
|
|
855
|
+
for p in builder.parameters
|
|
856
|
+
if p.grouper and p.client_name == parameter.grouped_by
|
|
857
|
+
).optional
|
|
858
|
+
)
|
|
850
859
|
retval.append(
|
|
851
860
|
f" {parameter.client_name}={parameter.name_in_high_level_operation},"
|
|
861
|
+
f"{' # type: ignore' if type_ignore else ''}"
|
|
852
862
|
)
|
|
853
863
|
if request_builder.overloads:
|
|
854
864
|
seen_body_params = set()
|
|
@@ -896,7 +906,7 @@ class _OperationSerializer(
|
|
|
896
906
|
if self.code_model.options["version_tolerant"] and template_url:
|
|
897
907
|
url_to_format = template_url
|
|
898
908
|
retval.append(
|
|
899
|
-
"request.url = self._client.format_url({}{})
|
|
909
|
+
"request.url = self._client.format_url({}{})".format(
|
|
900
910
|
url_to_format,
|
|
901
911
|
", **path_format_arguments" if builder.parameters.path else "",
|
|
902
912
|
)
|
|
@@ -1044,20 +1054,41 @@ class _OperationSerializer(
|
|
|
1044
1054
|
self.response_headers_and_deserialization(builder.responses[0])
|
|
1045
1055
|
)
|
|
1046
1056
|
retval.append("")
|
|
1057
|
+
type_ignore = (
|
|
1058
|
+
builder.has_response_body
|
|
1059
|
+
and not builder.has_optional_return_type
|
|
1060
|
+
and not (
|
|
1061
|
+
self.code_model.options["models_mode"] == "msrest"
|
|
1062
|
+
and any(not resp.is_stream_response for resp in builder.responses)
|
|
1063
|
+
)
|
|
1064
|
+
)
|
|
1047
1065
|
if builder.has_optional_return_type or self.code_model.options["models_mode"]:
|
|
1048
1066
|
deserialized = "deserialized"
|
|
1049
1067
|
else:
|
|
1050
1068
|
deserialized = f"cast({builder.response_type_annotation(async_mode=self.async_mode)}, deserialized)"
|
|
1069
|
+
type_ignore = False
|
|
1070
|
+
if (
|
|
1071
|
+
not builder.has_optional_return_type
|
|
1072
|
+
and len(builder.responses) > 1
|
|
1073
|
+
and any(resp.is_stream_response or resp.type for resp in builder.responses)
|
|
1074
|
+
):
|
|
1075
|
+
type_ignore = True
|
|
1051
1076
|
retval.append("if cls:")
|
|
1052
1077
|
retval.append(
|
|
1053
|
-
" return cls(pipeline_response, {}, {})".format(
|
|
1078
|
+
" return cls(pipeline_response, {}, {}){}".format(
|
|
1054
1079
|
deserialized if builder.has_response_body else "None",
|
|
1055
1080
|
"response_headers" if builder.any_response_has_headers else "{}",
|
|
1081
|
+
" # type: ignore" if type_ignore else "",
|
|
1056
1082
|
)
|
|
1057
1083
|
)
|
|
1058
|
-
if builder.has_response_body
|
|
1084
|
+
if builder.has_response_body and any(
|
|
1085
|
+
response.is_stream_response or response.type
|
|
1086
|
+
for response in builder.responses
|
|
1087
|
+
):
|
|
1059
1088
|
retval.append("")
|
|
1060
|
-
retval.append(
|
|
1089
|
+
retval.append(
|
|
1090
|
+
f"return {deserialized}{' # type: ignore' if type_ignore else ''}"
|
|
1091
|
+
)
|
|
1061
1092
|
if (
|
|
1062
1093
|
builder.request_builder.method == "HEAD"
|
|
1063
1094
|
and self.code_model.options["head_as_boolean"]
|
|
@@ -1131,7 +1162,7 @@ class _OperationSerializer(
|
|
|
1131
1162
|
@staticmethod
|
|
1132
1163
|
def get_metadata_url(builder: OperationType) -> str:
|
|
1133
1164
|
url = _escape_str(builder.request_builder.url)
|
|
1134
|
-
return f"{builder.name}.metadata = {{'url': { url }}}
|
|
1165
|
+
return f"{builder.name}.metadata = {{'url': { url }}}"
|
|
1135
1166
|
|
|
1136
1167
|
@property
|
|
1137
1168
|
def _call_method(self) -> str:
|
|
@@ -1280,7 +1311,7 @@ class _PagingOperationSerializer(
|
|
|
1280
1311
|
)
|
|
1281
1312
|
retval.append(f" list_of_elem = deserialized{list_of_elem}")
|
|
1282
1313
|
retval.append(" if cls:")
|
|
1283
|
-
retval.append(" list_of_elem = cls(list_of_elem)")
|
|
1314
|
+
retval.append(" list_of_elem = cls(list_of_elem) # type: ignore")
|
|
1284
1315
|
|
|
1285
1316
|
continuation_token_name = builder.continuation_token_name
|
|
1286
1317
|
if not continuation_token_name:
|
|
@@ -1364,19 +1395,19 @@ class _LROOperationSerializer(_OperationSerializer[LROOperationType]):
|
|
|
1364
1395
|
|
|
1365
1396
|
def initial_call(self, builder: LROOperationType) -> List[str]:
|
|
1366
1397
|
retval = [
|
|
1367
|
-
"polling = kwargs.pop('polling', True)
|
|
1368
|
-
f"{builder.get_base_polling_method(self.async_mode)}]"
|
|
1398
|
+
f"polling: Union[bool, {builder.get_base_polling_method(self.async_mode)}] = kwargs.pop('polling', True)",
|
|
1369
1399
|
]
|
|
1370
1400
|
retval.append("lro_delay = kwargs.pop(")
|
|
1371
1401
|
retval.append(" 'polling_interval',")
|
|
1372
1402
|
retval.append(" self._config.polling_interval")
|
|
1373
1403
|
retval.append(")")
|
|
1374
1404
|
retval.append(
|
|
1375
|
-
"cont_token = kwargs.pop('continuation_token', None)
|
|
1405
|
+
"cont_token: Optional[str] = kwargs.pop('continuation_token', None)"
|
|
1376
1406
|
)
|
|
1377
1407
|
retval.append("if cont_token is None:")
|
|
1378
1408
|
retval.append(
|
|
1379
|
-
f" raw_result = {self._call_method}self.{builder.initial_operation.name}(
|
|
1409
|
+
f" raw_result = {self._call_method}self.{builder.initial_operation.name}("
|
|
1410
|
+
f"{'' if builder.lro_response and builder.lro_response.type else ' # type: ignore'}"
|
|
1380
1411
|
)
|
|
1381
1412
|
retval.extend(
|
|
1382
1413
|
[
|
|
@@ -1409,13 +1440,14 @@ class _LROOperationSerializer(_OperationSerializer[LROOperationType]):
|
|
|
1409
1440
|
retval.extend(
|
|
1410
1441
|
[
|
|
1411
1442
|
"if polling is True:",
|
|
1412
|
-
f" polling_method
|
|
1443
|
+
f" polling_method: {builder.get_base_polling_method(self.async_mode)} "
|
|
1444
|
+
+ f"= cast({builder.get_base_polling_method(self.async_mode)}, "
|
|
1413
1445
|
f"{builder.get_polling_method(self.async_mode)}(",
|
|
1414
1446
|
" lro_delay,",
|
|
1415
1447
|
f" {lro_options_str}",
|
|
1416
1448
|
f" {path_format_arguments_str}",
|
|
1417
1449
|
" **kwargs",
|
|
1418
|
-
f"))
|
|
1450
|
+
f"))",
|
|
1419
1451
|
]
|
|
1420
1452
|
)
|
|
1421
1453
|
retval.append(
|
|
@@ -1434,7 +1466,7 @@ class _LROOperationSerializer(_OperationSerializer[LROOperationType]):
|
|
|
1434
1466
|
retval.append(" )")
|
|
1435
1467
|
retval.append(
|
|
1436
1468
|
f"return {builder.get_poller(self.async_mode)}"
|
|
1437
|
-
"(self._client, raw_result, get_long_running_output, polling_method)"
|
|
1469
|
+
"(self._client, raw_result, get_long_running_output, polling_method) # type: ignore"
|
|
1438
1470
|
)
|
|
1439
1471
|
return retval
|
|
1440
1472
|
|
|
@@ -1462,13 +1494,18 @@ class _LROOperationSerializer(_OperationSerializer[LROOperationType]):
|
|
|
1462
1494
|
)
|
|
1463
1495
|
retval.append(" if cls:")
|
|
1464
1496
|
retval.append(
|
|
1465
|
-
" return cls(pipeline_response, {}, {})".format(
|
|
1497
|
+
" return cls(pipeline_response, {}, {}){}".format(
|
|
1466
1498
|
"deserialized"
|
|
1467
1499
|
if builder.lro_response and builder.lro_response.type
|
|
1468
1500
|
else "None",
|
|
1469
1501
|
"response_headers"
|
|
1470
1502
|
if builder.lro_response and builder.lro_response.headers
|
|
1471
1503
|
else "{}",
|
|
1504
|
+
" # type: ignore"
|
|
1505
|
+
if builder.lro_response
|
|
1506
|
+
and builder.lro_response.type
|
|
1507
|
+
and not self.code_model.options["models_mode"]
|
|
1508
|
+
else "",
|
|
1472
1509
|
)
|
|
1473
1510
|
)
|
|
1474
1511
|
if builder.lro_response and builder.lro_response.type:
|
|
@@ -1507,7 +1544,7 @@ class LROPagingOperationSerializer(
|
|
|
1507
1544
|
retval.append(" )")
|
|
1508
1545
|
return retval
|
|
1509
1546
|
|
|
1510
|
-
def decorators(self, builder: LROPagingOperation) -> List[str]:
|
|
1547
|
+
def decorators(self, builder: LROPagingOperation) -> List[str]:
|
|
1511
1548
|
"""Decorators for the method"""
|
|
1512
1549
|
return _LROOperationSerializer.decorators(self, builder) # type: ignore
|
|
1513
1550
|
|
|
@@ -120,7 +120,7 @@ class ClientSerializer:
|
|
|
120
120
|
def _get_client_models_value(models_dict_name: str) -> str:
|
|
121
121
|
if self.client.code_model.model_types:
|
|
122
122
|
return f"{{k: v for k, v in {models_dict_name}.__dict__.items() if isinstance(v, type)}}"
|
|
123
|
-
return "{}
|
|
123
|
+
return "{}"
|
|
124
124
|
|
|
125
125
|
is_msrest_model = self.client.code_model.options["models_mode"] == "msrest"
|
|
126
126
|
if is_msrest_model:
|
|
@@ -133,7 +133,8 @@ class ClientSerializer:
|
|
|
133
133
|
else "_models"
|
|
134
134
|
)
|
|
135
135
|
retval.append(
|
|
136
|
-
f"client_models
|
|
136
|
+
f"client_models{': Dict[str, Any]' if not self.client.code_model.model_types else ''}"
|
|
137
|
+
f" = {_get_client_models_value(model_dict_name)}"
|
|
137
138
|
)
|
|
138
139
|
if add_private_models and self.client.code_model.model_types:
|
|
139
140
|
update_dict = f"{{k: v for k, v in _models.__dict__.items() if isinstance(v, type)}}"
|
|
@@ -149,7 +150,7 @@ class ClientSerializer:
|
|
|
149
150
|
for og in operation_groups:
|
|
150
151
|
retval.extend(
|
|
151
152
|
[
|
|
152
|
-
f"self.{og.property_name} = {og.class_name}({og.
|
|
153
|
+
f"self.{og.property_name} = {og.class_name}({og.pylint_disable}",
|
|
153
154
|
" self._client, self._config, self._serialize, self._deserialize",
|
|
154
155
|
")",
|
|
155
156
|
]
|
|
@@ -58,7 +58,13 @@ def _json_serialize_imports(
|
|
|
58
58
|
name_import_ordered_list = []
|
|
59
59
|
if name_imports:
|
|
60
60
|
name_import_ordered_list = list(name_imports)
|
|
61
|
-
name_import_ordered_list.sort(
|
|
61
|
+
name_import_ordered_list.sort(
|
|
62
|
+
key=lambda e: "".join(e) # type: ignore
|
|
63
|
+
if isinstance(e, (list, tuple))
|
|
64
|
+
else e
|
|
65
|
+
if isinstance(e, str)
|
|
66
|
+
else ""
|
|
67
|
+
)
|
|
62
68
|
json_package_name_dictionary[package_name] = name_import_ordered_list
|
|
63
69
|
json_import_type_dictionary[import_type_key] = json_package_name_dictionary
|
|
64
70
|
json_serialize_imports[typing_section_key] = json_import_type_dictionary
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
# Licensed under the MIT License. See License.txt in the project root for
|
|
4
4
|
# license information.
|
|
5
5
|
# --------------------------------------------------------------------------
|
|
6
|
-
from typing import
|
|
6
|
+
from typing import List, cast
|
|
7
7
|
from abc import ABC, abstractmethod
|
|
8
8
|
|
|
9
9
|
from jinja2 import Environment
|
|
@@ -77,7 +77,7 @@ class _ModelSerializer(ABC):
|
|
|
77
77
|
typing = "Optional[str]"
|
|
78
78
|
else:
|
|
79
79
|
typing = "str"
|
|
80
|
-
return f"self.{prop.client_name}
|
|
80
|
+
return f"self.{prop.client_name}: {typing} = {discriminator_value}"
|
|
81
81
|
|
|
82
82
|
@staticmethod
|
|
83
83
|
def initialize_standard_property(prop: Property):
|
|
@@ -152,7 +152,7 @@ class MsrestModelSerializer(_ModelSerializer):
|
|
|
152
152
|
else "_serialization.Model"
|
|
153
153
|
)
|
|
154
154
|
if model.parents:
|
|
155
|
-
basename = ", ".join([
|
|
155
|
+
basename = ", ".join([m.name for m in model.parents])
|
|
156
156
|
return f"class {model.name}({basename}):{model.pylint_disable}"
|
|
157
157
|
|
|
158
158
|
@staticmethod
|
|
@@ -163,9 +163,7 @@ class MsrestModelSerializer(_ModelSerializer):
|
|
|
163
163
|
p.client_name: p
|
|
164
164
|
for bm in model.parents
|
|
165
165
|
for p in model.properties
|
|
166
|
-
if p not in
|
|
167
|
-
or p.is_discriminator
|
|
168
|
-
or p.constant
|
|
166
|
+
if p not in bm.properties or p.is_discriminator or p.constant
|
|
169
167
|
}.values()
|
|
170
168
|
)
|
|
171
169
|
else:
|
|
@@ -223,7 +221,7 @@ class DpgModelSerializer(_ModelSerializer):
|
|
|
223
221
|
def declare_model(self, model: ModelType) -> str:
|
|
224
222
|
basename = "_model_base.Model"
|
|
225
223
|
if model.parents:
|
|
226
|
-
basename = ", ".join([
|
|
224
|
+
basename = ", ".join([m.name for m in model.parents])
|
|
227
225
|
if model.discriminator_value:
|
|
228
226
|
basename += f", discriminator='{model.discriminator_value}'"
|
|
229
227
|
return f"class {model.name}({basename}):{model.pylint_disable}"
|
|
@@ -231,9 +229,7 @@ class DpgModelSerializer(_ModelSerializer):
|
|
|
231
229
|
@staticmethod
|
|
232
230
|
def get_properties_to_declare(model: ModelType) -> List[Property]:
|
|
233
231
|
if model.parents:
|
|
234
|
-
parent_properties = [
|
|
235
|
-
p for bm in model.parents for p in cast(ModelType, bm).properties
|
|
236
|
-
]
|
|
232
|
+
parent_properties = [p for bm in model.parents for p in bm.properties]
|
|
237
233
|
properties_to_declare = [
|
|
238
234
|
p
|
|
239
235
|
for p in model.properties
|
|
@@ -274,7 +270,7 @@ class DpgModelSerializer(_ModelSerializer):
|
|
|
274
270
|
for prop in self.get_properties_to_declare(model):
|
|
275
271
|
if prop.constant or prop.is_discriminator:
|
|
276
272
|
init_args.append(
|
|
277
|
-
f"self.{prop.client_name}
|
|
278
|
-
f"
|
|
273
|
+
f"self.{prop.client_name}: {prop.type_annotation()} = "
|
|
274
|
+
f"{cast(ConstantType, prop.type).get_declaration()}"
|
|
279
275
|
)
|
|
280
276
|
return init_args
|
|
@@ -145,13 +145,13 @@ class ParameterSerializer:
|
|
|
145
145
|
f"_{kwarg_dict}.pop('{kwarg.rest_api_name}', {default_value})"
|
|
146
146
|
)
|
|
147
147
|
retval.append(
|
|
148
|
-
f"{kwarg.client_name} = kwargs.pop('{kwarg.client_name}', "
|
|
149
|
-
+ f"{default_value})
|
|
148
|
+
f"{kwarg.client_name}: {kwarg.type_annotation()} = kwargs.pop('{kwarg.client_name}', "
|
|
149
|
+
+ f"{default_value})"
|
|
150
150
|
)
|
|
151
151
|
else:
|
|
152
152
|
type_annot = kwarg.type_annotation()
|
|
153
153
|
retval.append(
|
|
154
|
-
f"{kwarg.client_name} = kwargs.pop('{kwarg.client_name}')
|
|
154
|
+
f"{kwarg.client_name}: {type_annot} = kwargs.pop('{kwarg.client_name}')"
|
|
155
155
|
)
|
|
156
156
|
return retval
|
|
157
157
|
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
# --------------------------------------------------------------------------
|
|
6
6
|
from jinja2 import Environment
|
|
7
7
|
from .import_serializer import FileImportSerializer
|
|
8
|
-
from ..models import CodeModel, FileImport, ImportType
|
|
8
|
+
from ..models import CodeModel, FileImport, ImportType
|
|
9
9
|
|
|
10
10
|
|
|
11
11
|
class PatchSerializer:
|
|
@@ -16,9 +16,7 @@ class PatchSerializer:
|
|
|
16
16
|
def serialize(self) -> str:
|
|
17
17
|
template = self.env.get_template("patch.py.jinja2")
|
|
18
18
|
imports = FileImport()
|
|
19
|
-
imports.add_submodule_import(
|
|
20
|
-
"typing", "List", ImportType.STDLIB, TypingSection.CONDITIONAL
|
|
21
|
-
)
|
|
19
|
+
imports.add_submodule_import("typing", "List", ImportType.STDLIB)
|
|
22
20
|
return template.render(
|
|
23
21
|
code_model=self.code_model,
|
|
24
22
|
imports=FileImportSerializer(imports),
|