@autorest/python 5.15.0 → 5.18.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (118) hide show
  1. package/ChangeLog.md +98 -4
  2. package/README.md +30 -4
  3. package/autorest/__init__.py +2 -3
  4. package/autorest/black/__init__.py +12 -5
  5. package/autorest/codegen/__init__.py +122 -211
  6. package/autorest/codegen/models/__init__.py +122 -78
  7. package/autorest/codegen/models/base_builder.py +70 -72
  8. package/autorest/codegen/models/base_model.py +7 -5
  9. package/autorest/codegen/models/{base_schema.py → base_type.py} +68 -45
  10. package/autorest/codegen/models/client.py +193 -40
  11. package/autorest/codegen/models/code_model.py +145 -245
  12. package/autorest/codegen/models/combined_type.py +107 -0
  13. package/autorest/codegen/models/constant_type.py +122 -0
  14. package/autorest/codegen/models/credential_types.py +224 -0
  15. package/autorest/codegen/models/dictionary_type.py +131 -0
  16. package/autorest/codegen/models/enum_type.py +195 -0
  17. package/autorest/codegen/models/imports.py +93 -41
  18. package/autorest/codegen/models/list_type.py +149 -0
  19. package/autorest/codegen/models/lro_operation.py +90 -133
  20. package/autorest/codegen/models/lro_paging_operation.py +28 -12
  21. package/autorest/codegen/models/model_type.py +262 -0
  22. package/autorest/codegen/models/operation.py +412 -259
  23. package/autorest/codegen/models/operation_group.py +80 -91
  24. package/autorest/codegen/models/paging_operation.py +101 -117
  25. package/autorest/codegen/models/parameter.py +302 -341
  26. package/autorest/codegen/models/parameter_list.py +373 -357
  27. package/autorest/codegen/models/primitive_types.py +544 -0
  28. package/autorest/codegen/models/property.py +136 -134
  29. package/autorest/codegen/models/request_builder.py +138 -86
  30. package/autorest/codegen/models/request_builder_parameter.py +122 -86
  31. package/autorest/codegen/models/response.py +325 -0
  32. package/autorest/codegen/models/utils.py +13 -17
  33. package/autorest/codegen/serializers/__init__.py +212 -112
  34. package/autorest/codegen/serializers/builder_serializer.py +931 -1040
  35. package/autorest/codegen/serializers/client_serializer.py +140 -84
  36. package/autorest/codegen/serializers/general_serializer.py +26 -50
  37. package/autorest/codegen/serializers/import_serializer.py +96 -31
  38. package/autorest/codegen/serializers/metadata_serializer.py +39 -79
  39. package/autorest/codegen/serializers/model_base_serializer.py +62 -34
  40. package/autorest/codegen/serializers/model_generic_serializer.py +9 -10
  41. package/autorest/codegen/serializers/model_init_serializer.py +4 -2
  42. package/autorest/codegen/serializers/model_python3_serializer.py +29 -22
  43. package/autorest/codegen/serializers/operation_groups_serializer.py +21 -19
  44. package/autorest/codegen/serializers/operations_init_serializer.py +23 -11
  45. package/autorest/codegen/serializers/parameter_serializer.py +174 -0
  46. package/autorest/codegen/serializers/patch_serializer.py +4 -1
  47. package/autorest/codegen/serializers/request_builders_serializer.py +57 -0
  48. package/autorest/codegen/serializers/utils.py +0 -126
  49. package/autorest/codegen/templates/MANIFEST.in.jinja2 +1 -0
  50. package/autorest/codegen/templates/{service_client.py.jinja2 → client.py.jinja2} +7 -7
  51. package/autorest/codegen/templates/config.py.jinja2 +13 -13
  52. package/autorest/codegen/templates/enum.py.jinja2 +4 -4
  53. package/autorest/codegen/templates/enum_container.py.jinja2 +1 -1
  54. package/autorest/codegen/templates/init.py.jinja2 +3 -3
  55. package/autorest/codegen/templates/lro_operation.py.jinja2 +6 -5
  56. package/autorest/codegen/templates/lro_paging_operation.py.jinja2 +6 -5
  57. package/autorest/codegen/templates/metadata.json.jinja2 +36 -35
  58. package/autorest/codegen/templates/model.py.jinja2 +23 -24
  59. package/autorest/codegen/templates/model_container.py.jinja2 +2 -1
  60. package/autorest/codegen/templates/model_init.py.jinja2 +3 -5
  61. package/autorest/codegen/templates/operation.py.jinja2 +10 -14
  62. package/autorest/codegen/templates/operation_group.py.jinja2 +9 -15
  63. package/autorest/codegen/templates/operation_groups_container.py.jinja2 +1 -1
  64. package/autorest/codegen/templates/operation_tools.jinja2 +8 -2
  65. package/autorest/codegen/templates/paging_operation.py.jinja2 +7 -8
  66. package/autorest/codegen/templates/request_builder.py.jinja2 +19 -10
  67. package/autorest/codegen/templates/setup.py.jinja2 +9 -3
  68. package/autorest/codegen/templates/vendor.py.jinja2 +1 -1
  69. package/autorest/jsonrpc/__init__.py +7 -12
  70. package/autorest/jsonrpc/localapi.py +4 -3
  71. package/autorest/jsonrpc/server.py +28 -9
  72. package/autorest/jsonrpc/stdstream.py +13 -6
  73. package/autorest/m2r/__init__.py +5 -8
  74. package/autorest/m4reformatter/__init__.py +1126 -0
  75. package/autorest/multiapi/__init__.py +24 -14
  76. package/autorest/multiapi/models/client.py +21 -11
  77. package/autorest/multiapi/models/code_model.py +23 -10
  78. package/autorest/multiapi/models/config.py +4 -1
  79. package/autorest/multiapi/models/constant_global_parameter.py +1 -0
  80. package/autorest/multiapi/models/global_parameter.py +2 -1
  81. package/autorest/multiapi/models/global_parameters.py +14 -8
  82. package/autorest/multiapi/models/imports.py +24 -17
  83. package/autorest/multiapi/models/mixin_operation.py +5 -5
  84. package/autorest/multiapi/models/operation_group.py +2 -1
  85. package/autorest/multiapi/models/operation_mixin_group.py +21 -10
  86. package/autorest/multiapi/serializers/__init__.py +20 -25
  87. package/autorest/multiapi/serializers/import_serializer.py +47 -17
  88. package/autorest/multiapi/serializers/multiapi_serializer.py +17 -17
  89. package/autorest/multiapi/templates/multiapi_config.py.jinja2 +3 -3
  90. package/autorest/multiapi/templates/multiapi_init.py.jinja2 +2 -2
  91. package/autorest/multiapi/templates/multiapi_operations_mixin.py.jinja2 +4 -4
  92. package/autorest/multiapi/templates/multiapi_service_client.py.jinja2 +9 -9
  93. package/autorest/multiapi/utils.py +3 -3
  94. package/autorest/postprocess/__init__.py +202 -0
  95. package/autorest/postprocess/get_all.py +19 -0
  96. package/autorest/postprocess/venvtools.py +73 -0
  97. package/autorest/preprocess/__init__.py +210 -0
  98. package/autorest/preprocess/helpers.py +54 -0
  99. package/autorest/{namer → preprocess}/python_mappings.py +25 -32
  100. package/package.json +3 -3
  101. package/run-python3.js +2 -3
  102. package/venvtools.py +1 -1
  103. package/autorest/codegen/models/constant_schema.py +0 -101
  104. package/autorest/codegen/models/credential_model.py +0 -47
  105. package/autorest/codegen/models/credential_schema.py +0 -91
  106. package/autorest/codegen/models/credential_schema_policy.py +0 -77
  107. package/autorest/codegen/models/dictionary_schema.py +0 -103
  108. package/autorest/codegen/models/enum_schema.py +0 -215
  109. package/autorest/codegen/models/list_schema.py +0 -123
  110. package/autorest/codegen/models/object_schema.py +0 -253
  111. package/autorest/codegen/models/primitive_schemas.py +0 -466
  112. package/autorest/codegen/models/request_builder_parameter_list.py +0 -280
  113. package/autorest/codegen/models/rest.py +0 -42
  114. package/autorest/codegen/models/schema_request.py +0 -45
  115. package/autorest/codegen/models/schema_response.py +0 -136
  116. package/autorest/codegen/serializers/rest_serializer.py +0 -57
  117. package/autorest/namer/__init__.py +0 -25
  118. package/autorest/namer/name_converter.py +0 -412
@@ -3,120 +3,164 @@
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 Any, Dict
6
+ from typing import Any, Dict, Union
7
7
  from .base_model import BaseModel
8
8
  from .code_model import CodeModel
9
- from .credential_schema import AzureKeyCredentialSchema, TokenCredentialSchema
10
- from .object_schema import ObjectSchema, get_object_schema, HiddenModelObjectSchema
11
- from .dictionary_schema import DictionarySchema
12
- from .list_schema import ListSchema
13
- from .primitive_schemas import get_primitive_schema, AnySchema, PrimitiveSchema, IOSchema
14
- from .enum_schema import EnumSchema, HiddenModelEnumSchema, get_enum_schema
15
- from .base_schema import BaseSchema
16
- from .constant_schema import ConstantSchema
9
+ from .model_type import ModelType
10
+ from .dictionary_type import DictionaryType
11
+ from .list_type import ListType
12
+ from .combined_type import CombinedType
13
+ from .primitive_types import (
14
+ ByteArraySchema,
15
+ DateType,
16
+ DatetimeType,
17
+ DurationType,
18
+ IntegerType,
19
+ FloatType,
20
+ StringType,
21
+ TimeType,
22
+ AnyType,
23
+ PrimitiveType,
24
+ BinaryType,
25
+ BooleanType,
26
+ AnyObjectType,
27
+ UnixTimeType,
28
+ )
29
+ from .enum_type import EnumType
30
+ from .base_type import BaseType
31
+ from .constant_type import ConstantType
17
32
  from .imports import FileImport, ImportType, TypingSection
18
33
  from .lro_operation import LROOperation
19
34
  from .paging_operation import PagingOperation
20
- from .parameter import Parameter, ParameterStyle, ParameterLocation
35
+ from .parameter import (
36
+ Parameter,
37
+ ParameterMethodLocation,
38
+ ParameterLocation,
39
+ BodyParameter,
40
+ ParameterDelimeter,
41
+ MultipartBodyParameter,
42
+ ClientParameter,
43
+ ConfigParameter,
44
+ )
21
45
  from .operation import Operation
22
46
  from .property import Property
23
47
  from .operation_group import OperationGroup
24
- from .schema_response import SchemaResponse
25
- from .parameter_list import ParameterList, GlobalParameterList
26
- from .request_builder import RequestBuilder
48
+ from .response import Response
49
+ from .parameter_list import (
50
+ ParameterList,
51
+ ClientGlobalParameterList,
52
+ ConfigGlobalParameterList,
53
+ )
54
+ from .request_builder import (
55
+ RequestBuilder,
56
+ OverloadedRequestBuilder,
57
+ RequestBuilderBase,
58
+ )
27
59
  from .base_builder import BaseBuilder
28
60
  from .lro_paging_operation import LROPagingOperation
29
- from .request_builder_parameter import RequestBuilderParameter
30
- from .schema_request import SchemaRequest
61
+ from .request_builder_parameter import (
62
+ RequestBuilderParameter,
63
+ RequestBuilderBodyParameter,
64
+ )
65
+ from .credential_types import (
66
+ TokenCredentialType,
67
+ AzureKeyCredentialType,
68
+ ARMChallengeAuthenticationPolicyType,
69
+ BearerTokenCredentialPolicyType,
70
+ AzureKeyCredentialPolicyType,
71
+ CredentialType,
72
+ )
31
73
 
32
74
  __all__ = [
33
- "AzureKeyCredentialSchema",
34
- "AnySchema",
75
+ "AzureKeyCredentialPolicyType",
76
+ "AnyType",
35
77
  "BaseModel",
36
- "BaseSchema",
78
+ "BaseType",
37
79
  "CodeModel",
38
- "ConstantSchema",
39
- "ObjectSchema",
40
- "DictionarySchema",
41
- "ListSchema",
42
- "EnumSchema",
43
- "HiddenModelEnumSchema",
80
+ "ConstantType",
81
+ "ModelType",
82
+ "DictionaryType",
83
+ "ListType",
84
+ "EnumType",
44
85
  "FileImport",
45
86
  "ImportType",
46
87
  "TypingSection",
47
- "PrimitiveSchema",
88
+ "PrimitiveType",
48
89
  "LROOperation",
49
90
  "Operation",
50
91
  "PagingOperation",
51
92
  "Parameter",
52
93
  "ParameterList",
53
- "ParameterLocation",
54
94
  "OperationGroup",
55
95
  "Property",
56
96
  "RequestBuilder",
57
- "SchemaResponse",
58
- "TokenCredentialSchema",
97
+ "Response",
98
+ "TokenCredentialType",
59
99
  "LROPagingOperation",
60
100
  "BaseBuilder",
61
- "SchemaRequest",
62
101
  "RequestBuilderParameter",
63
- "HiddenModelObjectSchema",
64
- "ParameterStyle",
65
- "IOSchema",
66
- "GlobalParameterList",
102
+ "BinaryType",
103
+ "ClientGlobalParameterList",
104
+ "ConfigGlobalParameterList",
105
+ "ParameterMethodLocation",
106
+ "ParameterLocation",
107
+ "OverloadedRequestBuilder",
108
+ "RequestBuilderBase",
109
+ "BodyParameter",
110
+ "RequestBuilderBodyParameter",
111
+ "ParameterDelimeter",
112
+ "MultipartBodyParameter",
113
+ "CredentialType",
114
+ "ClientParameter",
115
+ "ConfigParameter",
67
116
  ]
68
117
 
69
- def _generate_as_object_schema(yaml_data: Dict[str, Any]) -> bool:
70
- if (
71
- yaml_data.get('properties') or
72
- yaml_data.get('discriminator') or
73
- yaml_data.get('parents') and yaml_data['parents'].get('immediate')
74
- ):
75
- return True
76
- return False
118
+ TYPE_TO_OBJECT = {
119
+ "integer": IntegerType,
120
+ "float": FloatType,
121
+ "string": StringType,
122
+ "list": ListType,
123
+ "dict": DictionaryType,
124
+ "constant": ConstantType,
125
+ "enum": EnumType,
126
+ "binary": BinaryType,
127
+ "any": AnyType,
128
+ "datetime": DatetimeType,
129
+ "time": TimeType,
130
+ "duration": DurationType,
131
+ "date": DateType,
132
+ "byte-array": ByteArraySchema,
133
+ "boolean": BooleanType,
134
+ "combined": CombinedType,
135
+ "OAuth2": TokenCredentialType,
136
+ "Key": AzureKeyCredentialType,
137
+ "ARMChallengeAuthenticationPolicy": ARMChallengeAuthenticationPolicyType,
138
+ "BearerTokenCredentialPolicy": BearerTokenCredentialPolicyType,
139
+ "AzureKeyCredentialPolicy": AzureKeyCredentialPolicyType,
140
+ "any-object": AnyObjectType,
141
+ "unixtime": UnixTimeType,
142
+ }
77
143
 
78
144
 
79
- def build_schema(yaml_data: Dict[str, Any], **kwargs) -> BaseSchema:
80
- code_model = kwargs.get("code_model", None)
81
- if not code_model:
82
- raise ValueError("CodeModel not passed through kwargs")
145
+ def build_type(yaml_data: Dict[str, Any], code_model: CodeModel) -> BaseType:
83
146
  yaml_id = id(yaml_data)
84
- namespace = code_model.namespace
85
147
  try:
86
- return code_model.lookup_schema(yaml_id)
148
+ return code_model.lookup_type(yaml_id)
87
149
  except KeyError:
88
150
  # Not created yet, let's create it and add it to the index
89
151
  pass
90
- schema: BaseSchema
91
- schema_type = yaml_data["type"]
92
- if schema_type == "constant":
93
- schema = ConstantSchema.from_yaml(namespace=namespace, yaml_data=yaml_data)
94
- code_model.primitives[yaml_id] = schema
95
-
96
- elif schema_type in ["choice", "sealed-choice"]:
97
- schema = get_enum_schema(code_model).from_yaml(namespace=namespace, yaml_data=yaml_data, **kwargs)
98
- code_model.enums[yaml_id] = schema
99
-
100
- elif schema_type == "array":
101
- schema = ListSchema.from_yaml(namespace=namespace, yaml_data=yaml_data, **kwargs)
102
- code_model.primitives[yaml_id] = schema
103
-
104
- elif schema_type == "dictionary":
105
- schema = DictionarySchema.from_yaml(namespace=namespace, yaml_data=yaml_data, **kwargs)
106
- code_model.primitives[yaml_id] = schema
107
-
108
- elif schema_type in ["object", "and", "group", "any-object"]:
109
- if _generate_as_object_schema(yaml_data):
110
- # To avoid infinite loop, create the right instance in memory,
111
- # put it in the index, and then parse the object.
112
- schema = get_object_schema(code_model)(namespace, yaml_data, "_", "")
113
- code_model.schemas[yaml_id] = schema
114
- schema.fill_instance_from_yaml(namespace=namespace, yaml_data=yaml_data, **kwargs)
115
- else:
116
- schema = AnySchema.from_yaml(namespace=namespace, yaml_data=yaml_data)
117
- code_model.primitives[yaml_id] = schema
152
+ if yaml_data["type"] == "model":
153
+ # need to special case model to avoid recursion
154
+ response = ModelType(yaml_data, code_model)
155
+ code_model.types_map[yaml_id] = response
156
+ response.fill_instance_from_yaml(yaml_data, code_model)
118
157
  else:
119
- schema = get_primitive_schema(namespace=namespace, yaml_data=yaml_data)
120
- code_model.primitives[yaml_id] = schema
158
+ response = TYPE_TO_OBJECT[yaml_data["type"]].from_yaml(yaml_data, code_model) # type: ignore
159
+ code_model.types_map[yaml_id] = response
160
+ return response
121
161
 
122
- return schema
162
+
163
+ RequestBuilderType = Union[RequestBuilder, OverloadedRequestBuilder]
164
+ ParameterType = Union[
165
+ Parameter, RequestBuilderParameter, ClientParameter, ConfigParameter
166
+ ]
@@ -3,95 +3,93 @@
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 Any, Callable, Dict, List, Optional, Union
6
+ from abc import abstractmethod
7
+ from typing import Any, Dict, List, Optional, TypeVar, Union, TYPE_CHECKING, Generic
7
8
  from .base_model import BaseModel
8
- from .schema_response import SchemaResponse
9
- from .schema_request import SchemaRequest
9
+ from .parameter_list import (
10
+ ParameterList,
11
+ RequestBuilderParameterList,
12
+ OverloadedRequestBuilderParameterList,
13
+ )
10
14
 
15
+ ParameterListType = TypeVar(
16
+ "ParameterListType",
17
+ bound=Union[
18
+ ParameterList,
19
+ RequestBuilderParameterList,
20
+ OverloadedRequestBuilderParameterList,
21
+ ],
22
+ )
11
23
 
12
- _M4_HEADER_PARAMETERS = ["content_type", "accept"]
13
24
 
14
- def create_parameters(
15
- yaml_data: Dict[str, Any], code_model, parameter_creator: Callable
16
- ):
17
- multiple_requests = len(yaml_data["requests"]) > 1
25
+ if TYPE_CHECKING:
26
+ from .code_model import CodeModel
27
+ from .operation import Operation
28
+ from .request_builder import RequestBuilder
18
29
 
19
- multiple_content_type_parameters = []
20
- parameters = [
21
- parameter_creator(yaml, code_model=code_model)
22
- for yaml in yaml_data.get("parameters", [])
23
- ]
24
30
 
25
- for request in yaml_data["requests"]:
26
- for yaml in request.get("parameters", []):
27
- parameter = parameter_creator(yaml, code_model=code_model)
28
- name = yaml["language"]["python"]["name"]
29
- if name in _M4_HEADER_PARAMETERS:
30
- parameters.append(parameter)
31
- elif multiple_requests:
32
- parameter.has_multiple_content_types = True
33
- multiple_content_type_parameters.append(parameter)
34
- else:
35
- parameters.append(parameter)
36
-
37
- if multiple_content_type_parameters:
38
- body_parameters_name_set = set(
39
- p.serialized_name for p in multiple_content_type_parameters
40
- )
41
- if len(body_parameters_name_set) > 1:
42
- raise ValueError(
43
- f"The body parameter with multiple media types has different names: {body_parameters_name_set}"
44
- )
45
-
46
-
47
- parameters_index = {id(parameter.yaml_data): parameter for parameter in parameters}
48
-
49
- # Need to connect the groupBy and originalParameter
50
- for parameter in parameters:
51
- parameter_grouped_by_id = id(parameter.grouped_by)
52
- if parameter_grouped_by_id in parameters_index:
53
- parameter.grouped_by = parameters_index[parameter_grouped_by_id]
54
-
55
- parameter_original_id = id(parameter.original_parameter)
56
- if parameter_original_id in parameters_index:
57
- parameter.original_parameter = parameters_index[parameter_original_id]
58
-
59
- return parameters, multiple_content_type_parameters
60
-
61
- class BaseBuilder(BaseModel):
31
+ class BaseBuilder(Generic[ParameterListType], BaseModel):
62
32
  """Base class for Operations and Request Builders"""
63
33
 
64
34
  def __init__(
65
35
  self,
66
- code_model,
67
36
  yaml_data: Dict[str, Any],
37
+ code_model: "CodeModel",
68
38
  name: str,
69
- description: str,
70
- parameters,
71
- schema_requests: List[SchemaRequest],
72
- responses: Optional[List[SchemaResponse]] = None,
73
- summary: Optional[str] = None,
39
+ parameters: ParameterListType,
40
+ *,
41
+ overloads=None,
42
+ abstract: bool = False,
43
+ want_tracing: bool = True,
74
44
  ) -> None:
75
- super().__init__(yaml_data=yaml_data)
76
- self.code_model = code_model
45
+ super().__init__(yaml_data=yaml_data, code_model=code_model)
77
46
  self.name = name
78
- self.description = description
47
+ self._description: str = yaml_data.get("description", "")
79
48
  self.parameters = parameters
80
- self.responses = responses or []
81
- self.summary = summary
82
- self.schema_requests = schema_requests
49
+ self.overloads: Union[List["Operation"], List["RequestBuilder"]] = (
50
+ overloads or []
51
+ )
52
+ self._summary: str = yaml_data.get("summary", "")
53
+ # for operations where we don't know what to do, we mark them as abstract so users implement
54
+ # in patch.py
55
+ self.abstract = abstract
56
+ self.want_tracing = want_tracing
57
+ self.group_name: str = yaml_data["groupName"]
58
+ self.is_overload: bool = yaml_data["isOverload"]
59
+ self.api_versions: List[str] = yaml_data["apiVersions"]
83
60
 
84
61
  @property
85
- def default_content_type_declaration(self) -> str:
86
- return f'"{self.parameters.default_content_type}"'
62
+ def summary(self) -> Optional[str]:
63
+ if self.abstract:
64
+ return None
65
+ return self._summary
66
+
67
+ @property
68
+ def pylint_disable(self) -> str:
69
+ return ""
70
+
71
+ @abstractmethod
72
+ def response_type_annotation(self, **kwargs) -> str:
73
+ ...
74
+
75
+ @abstractmethod
76
+ def response_docstring_text(self, **kwargs) -> str:
77
+ ...
87
78
 
88
- def get_response_from_status(self, status_code: Optional[Union[str, int]]) -> SchemaResponse:
89
- for response in self.responses:
90
- if status_code in response.status_codes:
91
- return response
92
- raise ValueError(f"Incorrect status code {status_code}, operation {self.name}")
79
+ @abstractmethod
80
+ def response_docstring_type(self, **kwargs) -> str:
81
+ ...
93
82
 
94
83
  @property
95
- def success_status_code(self) -> List[Union[str, int]]:
96
- """The list of all successfull status code."""
97
- return [code for response in self.responses for code in response.status_codes if code != "default"]
84
+ def description(self) -> str:
85
+ if self.abstract:
86
+ return (
87
+ f'You need to write a custom operation for "{self.name}". Please refer to '
88
+ "https://aka.ms/azsdk/python/dpcodegen/python/customize to learn how to customize."
89
+ )
90
+ return self._description or self.name
91
+
92
+ def method_signature(self, is_python3_file: bool, async_mode: bool) -> List[str]:
93
+ if self.abstract:
94
+ return ["*args,", "**kwargs"]
95
+ return self.parameters.method_signature(is_python3_file, async_mode)
@@ -3,20 +3,22 @@
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 Any, Dict
6
+ from typing import Any, Dict, TYPE_CHECKING
7
+
8
+ if TYPE_CHECKING:
9
+ from .code_model import CodeModel
7
10
 
8
11
 
9
12
  class BaseModel:
10
- """This is the base class for model that are based on some YAML data.
13
+ """This is the base class for model representations that are based on some YAML data.
11
14
 
12
15
  :param yaml_data: the yaml data for this schema
13
16
  :type yaml_data: dict[str, Any]
14
17
  """
15
18
 
16
- def __init__(
17
- self, yaml_data: Dict[str, Any],
18
- ) -> None:
19
+ def __init__(self, yaml_data: Dict[str, Any], code_model: "CodeModel") -> None:
19
20
  self.yaml_data = yaml_data
21
+ self.code_model = code_model
20
22
 
21
23
  @property
22
24
  def id(self) -> int:
@@ -4,40 +4,52 @@
4
4
  # license information.
5
5
  # --------------------------------------------------------------------------
6
6
  from abc import ABC, abstractmethod
7
- from typing import Any, Dict, List, Optional, Union
7
+ from typing import Any, Dict, List, Optional, TYPE_CHECKING
8
8
 
9
9
  from .base_model import BaseModel
10
10
  from .imports import FileImport
11
11
 
12
+ if TYPE_CHECKING:
13
+ from .code_model import CodeModel
14
+ from .model_type import ModelType
12
15
 
13
- class BaseSchema(BaseModel, ABC):
14
- """This is the base class for all schema models.
16
+
17
+ class BaseType(BaseModel, ABC):
18
+ """This is the base class for all types.
15
19
 
16
20
  :param yaml_data: the yaml data for this schema
17
21
  :type yaml_data: dict[str, Any]
18
22
  """
19
23
 
20
- def __init__(self, namespace: str, yaml_data: Dict[str, Any]) -> None:
21
- super().__init__(yaml_data)
22
- self.namespace = namespace
23
- self.default_value = yaml_data.get("defaultValue", None)
24
- self.xml_metadata = yaml_data.get("serialization", {}).get("xml", {})
25
- self.api_versions = set(value_dict["version"] for value_dict in yaml_data.get("apiVersions", []))
24
+ def __init__(self, yaml_data: Dict[str, Any], code_model: "CodeModel") -> None:
25
+ super().__init__(yaml_data, code_model)
26
+ self.type = yaml_data["type"] # the type discriminator
27
+ self.api_versions: List[str] = yaml_data.get(
28
+ "apiVersions", []
29
+ ) # api versions this type is in.
26
30
 
27
31
  @classmethod
28
32
  def from_yaml(
29
- cls, namespace: str, yaml_data: Dict[str, Any], **kwargs # pylint: disable=unused-argument
30
- ) -> "BaseSchema":
31
- return cls(namespace=namespace, yaml_data=yaml_data)
33
+ cls, yaml_data: Dict[str, Any], code_model: "CodeModel"
34
+ ) -> "BaseType":
35
+ return cls(yaml_data=yaml_data, code_model=code_model)
36
+
37
+ def imports( # pylint: disable=no-self-use
38
+ self, **kwargs # pylint: disable=unused-argument
39
+ ) -> FileImport:
40
+ return FileImport()
32
41
 
33
42
  @property
34
- def extra_description_information(self) -> str:
35
- return ""
43
+ def xml_metadata(self) -> Dict[str, Any]:
44
+ """XML metadata for the type, if the type has it."""
45
+ return self.yaml_data.get("xmlMetadata", {})
36
46
 
37
47
  @property
38
- def has_xml_serialization_ctxt(self) -> bool:
48
+ def is_xml(self) -> bool:
49
+ """Whether the type is an XML type or not. Most likely not."""
39
50
  return bool(self.xml_metadata)
40
51
 
52
+ @property
41
53
  def xml_serialization_ctxt(self) -> Optional[str]:
42
54
  """Return the serialization context in case this schema is used in an operation."""
43
55
  attrs_list = []
@@ -53,12 +65,6 @@ class BaseSchema(BaseModel, ABC):
53
65
  attrs_list.append(f"'text': True")
54
66
  return ", ".join(attrs_list)
55
67
 
56
- def imports(self) -> FileImport: # pylint: disable=no-self-use
57
- return FileImport()
58
-
59
- def model_file_imports(self) -> FileImport:
60
- return self.imports()
61
-
62
68
  @property
63
69
  @abstractmethod
64
70
  def serialization_type(self) -> str:
@@ -75,15 +81,22 @@ class BaseSchema(BaseModel, ABC):
75
81
  ...
76
82
 
77
83
  @property
84
+ def client_default_value(self) -> Any:
85
+ """Whether there's a client default value for this type"""
86
+ return self.yaml_data.get("clientDefaultValue")
87
+
78
88
  @abstractmethod
79
- def docstring_text(self) -> str:
80
- """The names used in rtype documentation
81
- """
89
+ def description(self, *, is_operation_file: bool) -> str:
90
+ """The description"""
82
91
  ...
83
92
 
84
- @property
85
93
  @abstractmethod
86
- def docstring_type(self) -> str:
94
+ def docstring_text(self, **kwargs: Any) -> str:
95
+ """The names used in rtype documentation"""
96
+ ...
97
+
98
+ @abstractmethod
99
+ def docstring_type(self, **kwargs: Any) -> str:
87
100
  """The python type used for RST syntax input.
88
101
 
89
102
  Special case for enum, for instance: 'str or ~namespace.EnumName'
@@ -91,13 +104,22 @@ class BaseSchema(BaseModel, ABC):
91
104
  ...
92
105
 
93
106
  @abstractmethod
94
- def type_annotation(self, *, is_operation_file: bool = False) -> str:
107
+ def type_annotation(self, **kwargs: Any) -> str:
95
108
  """The python type used for type annotation
96
109
 
97
110
  Special case for enum, for instance: Union[str, "EnumName"]
98
111
  """
99
112
  ...
100
113
 
114
+ @property
115
+ def validation(self) -> Optional[Dict[str, Any]]:
116
+ """Whether there's any validation constraints on this type.
117
+
118
+ Even though we generate validation maps if there are validation constraints,
119
+ only SDKs with client-side-validate=true (0.001% libraries, if any) actually raise in this case.
120
+ """
121
+ return None
122
+
101
123
  def get_declaration(self, value: Any) -> str: # pylint: disable=no-self-use
102
124
  """Return the current value from YAML as a Python string that represents the constant.
103
125
 
@@ -111,28 +133,29 @@ class BaseSchema(BaseModel, ABC):
111
133
  """
112
134
  return str(value)
113
135
 
114
- @property
115
- def default_value_declaration(self) -> str:
116
- """Return the default value as string using get_declaration.
117
- """
118
- if self.default_value is None:
119
- return "None"
120
- return self.get_declaration(self.default_value)
136
+ @abstractmethod
137
+ def get_json_template_representation(
138
+ self,
139
+ *,
140
+ optional: bool = True,
141
+ client_default_value_declaration: Optional[str] = None,
142
+ description: Optional[str] = None,
143
+ ) -> Any:
144
+ """Template of what this schema would look like as JSON input"""
145
+ ...
121
146
 
122
- @property
123
- def validation_map(self) -> Optional[Dict[str, Union[bool, int, str]]]: # pylint: disable=no-self-use
147
+ def get_polymorphic_subtypes( # pylint: disable=no-self-use
148
+ self, polymorphic_subtypes: List["ModelType"] # pylint: disable=unused-argument
149
+ ) -> None:
124
150
  return None
125
151
 
126
152
  @property
127
- def serialization_constraints(self) -> Optional[List[str]]: # pylint: disable=no-self-use
128
- return None
129
-
130
153
  @abstractmethod
131
- def get_json_template_representation(self, **kwargs: Any) -> Any:
132
- """Template of what this schema would look like as JSON input"""
154
+ def instance_check_template(self) -> str:
155
+ """Template of what an instance check of a variable for this type would look like"""
133
156
  ...
134
157
 
135
- @abstractmethod
136
- def get_files_and_data_template_representation(self, **kwargs: Any) -> Any:
137
- """Template of what this schema would look like as files input"""
138
- ...
158
+ @property
159
+ def serialization_constraints(self) -> List[str]:
160
+ """Whether there are any serialization constraints when serializing this type."""
161
+ return []