@autorest/python 5.14.0 → 5.17.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 (120) hide show
  1. package/ChangeLog.md +91 -2
  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 +130 -179
  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} +62 -49
  10. package/autorest/codegen/models/client.py +195 -36
  11. package/autorest/codegen/models/code_model.py +165 -299
  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 +116 -0
  16. package/autorest/codegen/models/enum_type.py +195 -0
  17. package/autorest/codegen/models/imports.py +95 -41
  18. package/autorest/codegen/models/list_type.py +134 -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 +239 -0
  22. package/autorest/codegen/models/operation.py +415 -241
  23. package/autorest/codegen/models/operation_group.py +82 -88
  24. package/autorest/codegen/models/paging_operation.py +101 -117
  25. package/autorest/codegen/models/parameter.py +307 -322
  26. package/autorest/codegen/models/parameter_list.py +366 -357
  27. package/autorest/codegen/models/primitive_types.py +544 -0
  28. package/autorest/codegen/models/property.py +122 -134
  29. package/autorest/codegen/models/request_builder.py +138 -86
  30. package/autorest/codegen/models/request_builder_parameter.py +122 -79
  31. package/autorest/codegen/models/response.py +325 -0
  32. package/autorest/codegen/models/utils.py +17 -1
  33. package/autorest/codegen/serializers/__init__.py +242 -118
  34. package/autorest/codegen/serializers/builder_serializer.py +863 -1027
  35. package/autorest/codegen/serializers/client_serializer.py +148 -82
  36. package/autorest/codegen/serializers/general_serializer.py +44 -47
  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 +65 -29
  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 -18
  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 +14 -2
  47. package/autorest/codegen/serializers/request_builders_serializer.py +57 -0
  48. package/autorest/codegen/serializers/utils.py +0 -103
  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 -2
  54. package/autorest/codegen/templates/init.py.jinja2 +9 -6
  55. package/autorest/codegen/templates/keywords.jinja2 +14 -1
  56. package/autorest/codegen/templates/lro_operation.py.jinja2 +6 -5
  57. package/autorest/codegen/templates/lro_paging_operation.py.jinja2 +6 -5
  58. package/autorest/codegen/templates/metadata.json.jinja2 +36 -35
  59. package/autorest/codegen/templates/model.py.jinja2 +23 -29
  60. package/autorest/codegen/templates/model_container.py.jinja2 +2 -1
  61. package/autorest/codegen/templates/model_init.py.jinja2 +9 -8
  62. package/autorest/codegen/templates/operation.py.jinja2 +10 -15
  63. package/autorest/codegen/templates/operation_group.py.jinja2 +14 -13
  64. package/autorest/codegen/templates/operation_groups_container.py.jinja2 +1 -2
  65. package/autorest/codegen/templates/operation_tools.jinja2 +8 -2
  66. package/autorest/codegen/templates/operations_folder_init.py.jinja2 +4 -0
  67. package/autorest/codegen/templates/paging_operation.py.jinja2 +7 -8
  68. package/autorest/codegen/templates/patch.py.jinja2 +18 -29
  69. package/autorest/codegen/templates/request_builder.py.jinja2 +20 -13
  70. package/autorest/codegen/templates/setup.py.jinja2 +9 -3
  71. package/autorest/codegen/templates/vendor.py.jinja2 +12 -2
  72. package/autorest/jsonrpc/__init__.py +7 -12
  73. package/autorest/jsonrpc/localapi.py +4 -3
  74. package/autorest/jsonrpc/server.py +28 -9
  75. package/autorest/jsonrpc/stdstream.py +13 -6
  76. package/autorest/m2r/__init__.py +5 -8
  77. package/autorest/m4reformatter/__init__.py +1108 -0
  78. package/autorest/multiapi/__init__.py +24 -14
  79. package/autorest/multiapi/models/client.py +21 -11
  80. package/autorest/multiapi/models/code_model.py +23 -10
  81. package/autorest/multiapi/models/config.py +4 -1
  82. package/autorest/multiapi/models/constant_global_parameter.py +1 -0
  83. package/autorest/multiapi/models/global_parameter.py +2 -1
  84. package/autorest/multiapi/models/global_parameters.py +14 -8
  85. package/autorest/multiapi/models/imports.py +35 -18
  86. package/autorest/multiapi/models/mixin_operation.py +5 -5
  87. package/autorest/multiapi/models/operation_group.py +2 -1
  88. package/autorest/multiapi/models/operation_mixin_group.py +21 -10
  89. package/autorest/multiapi/serializers/__init__.py +20 -25
  90. package/autorest/multiapi/serializers/import_serializer.py +47 -15
  91. package/autorest/multiapi/serializers/multiapi_serializer.py +17 -17
  92. package/autorest/multiapi/templates/multiapi_config.py.jinja2 +3 -3
  93. package/autorest/multiapi/templates/multiapi_init.py.jinja2 +2 -2
  94. package/autorest/multiapi/templates/multiapi_operations_mixin.py.jinja2 +4 -4
  95. package/autorest/multiapi/templates/multiapi_service_client.py.jinja2 +9 -9
  96. package/autorest/multiapi/utils.py +3 -3
  97. package/autorest/postprocess/__init__.py +202 -0
  98. package/autorest/postprocess/get_all.py +19 -0
  99. package/autorest/postprocess/venvtools.py +73 -0
  100. package/autorest/preprocess/__init__.py +209 -0
  101. package/autorest/preprocess/helpers.py +54 -0
  102. package/autorest/{namer → preprocess}/python_mappings.py +25 -32
  103. package/package.json +3 -3
  104. package/run-python3.js +2 -3
  105. package/venvtools.py +1 -1
  106. package/autorest/codegen/models/constant_schema.py +0 -97
  107. package/autorest/codegen/models/credential_schema.py +0 -90
  108. package/autorest/codegen/models/credential_schema_policy.py +0 -77
  109. package/autorest/codegen/models/dictionary_schema.py +0 -103
  110. package/autorest/codegen/models/enum_schema.py +0 -246
  111. package/autorest/codegen/models/list_schema.py +0 -113
  112. package/autorest/codegen/models/object_schema.py +0 -249
  113. package/autorest/codegen/models/primitive_schemas.py +0 -476
  114. package/autorest/codegen/models/request_builder_parameter_list.py +0 -280
  115. package/autorest/codegen/models/rest.py +0 -42
  116. package/autorest/codegen/models/schema_request.py +0 -45
  117. package/autorest/codegen/models/schema_response.py +0 -123
  118. package/autorest/codegen/serializers/rest_serializer.py +0 -57
  119. package/autorest/namer/__init__.py +0 -25
  120. package/autorest/namer/name_converter.py +0 -412
@@ -1,77 +0,0 @@
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
- from abc import abstractmethod
7
- from typing import List
8
- from .credential_schema import CredentialSchema
9
-
10
- class CredentialSchemaPolicy:
11
-
12
- def __init__(self, credential: CredentialSchema, *args, **kwargs) -> None: # pylint: disable=unused-argument
13
- self.credential = credential
14
-
15
- @abstractmethod
16
- def call(self, async_mode: bool) -> str:
17
- ...
18
-
19
- @classmethod
20
- def name(cls):
21
- return cls.__name__
22
-
23
-
24
- class BearerTokenCredentialPolicy(CredentialSchemaPolicy):
25
-
26
- def __init__(
27
- self,
28
- credential: CredentialSchema,
29
- credential_scopes: List[str]
30
- ) -> None:
31
- super().__init__(credential)
32
- self._credential_scopes = credential_scopes
33
-
34
- @property
35
- def credential_scopes(self):
36
- return self._credential_scopes
37
-
38
- def call(self, async_mode: bool) -> str:
39
- policy_name = f"Async{self.name()}" if async_mode else self.name()
40
- return f"policies.{policy_name}(self.credential, *self.credential_scopes, **kwargs)"
41
-
42
-
43
- class ARMChallengeAuthenticationPolicy(BearerTokenCredentialPolicy):
44
-
45
- def call(self, async_mode: bool) -> str:
46
- policy_name = f"Async{self.name()}" if async_mode else self.name()
47
- return f"{policy_name}(self.credential, *self.credential_scopes, **kwargs)"
48
-
49
-
50
- class AzureKeyCredentialPolicy(CredentialSchemaPolicy):
51
-
52
- def __init__(
53
- self,
54
- credential: CredentialSchema,
55
- credential_key_header_name: str
56
- ) -> None:
57
- super().__init__(credential)
58
- self._credential_key_header_name = credential_key_header_name
59
-
60
- @property
61
- def credential_key_header_name(self):
62
- return self._credential_key_header_name
63
-
64
- def call(self, async_mode: bool) -> str:
65
- return f'policies.AzureKeyCredentialPolicy(self.credential, "{self.credential_key_header_name}", **kwargs)'
66
-
67
- def get_credential_schema_policy_type(name):
68
- policies = [ARMChallengeAuthenticationPolicy, BearerTokenCredentialPolicy, AzureKeyCredentialPolicy]
69
- try:
70
- return next(p for p in policies if p.name().lower() == name.lower())
71
- except StopIteration:
72
- raise ValueError(
73
- "The credential policy you pass in with --credential-default-policy-type must be either "
74
- "{}".format(
75
- " or ".join([p.name() for p in policies])
76
- )
77
- )
@@ -1,103 +0,0 @@
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
- from typing import Any, Dict, Optional
7
- from .base_schema import BaseSchema
8
- from .imports import FileImport, ImportType, TypingSection
9
-
10
- class DictionarySchema(BaseSchema):
11
- """Schema for dictionaries that will be serialized.
12
-
13
- :param yaml_data: the yaml data for this schema
14
- :type yaml_data: dict[str, Any]
15
- :param element_type: The type of the value for the dictionary
16
- :type element_type: ~autorest.models.BaseSchema
17
- """
18
-
19
- def __init__(
20
- self,
21
- namespace: str,
22
- yaml_data: Dict[str, Any],
23
- element_type: "BaseSchema"
24
- ) -> None:
25
- super(DictionarySchema, self).__init__(namespace=namespace, yaml_data=yaml_data)
26
- self.element_type = element_type
27
-
28
- @property
29
- def serialization_type(self) -> str:
30
- """Returns the serialization value for msrest.
31
-
32
- :return: The serialization value for msrest
33
- :rtype: str
34
- """
35
- return f"{{{self.element_type.serialization_type}}}"
36
-
37
- @property
38
- def type_annotation(self) -> str:
39
- """The python type used for type annotation
40
-
41
- :return: The type annotation for this schema
42
- :rtype: str
43
- """
44
- return f"Dict[str, {self.element_type.type_annotation}]"
45
-
46
- @property
47
- def operation_type_annotation(self) -> str:
48
- return f"Dict[str, {self.element_type.operation_type_annotation}]"
49
-
50
- @property
51
- def docstring_text(self) -> str:
52
- return f"dict mapping str to {self.element_type.docstring_text}"
53
-
54
- @property
55
- def docstring_type(self) -> str:
56
- """The python type used for RST syntax input and type annotation.
57
-
58
- :param str namespace: Optional. The namespace for the models.
59
- """
60
- return f"dict[str, {self.element_type.docstring_type}]"
61
-
62
- def xml_serialization_ctxt(self) -> Optional[str]:
63
- raise NotImplementedError("Dictionary schema does not support XML serialization.")
64
-
65
- def get_json_template_representation(self, **kwargs: Any) -> Any:
66
- return {
67
- f'"{"str"}"' : self.element_type.get_json_template_representation(**kwargs)
68
- }
69
-
70
- def get_files_and_data_template_representation(self, **kwargs: Any) -> Any:
71
- return {
72
- f'"{"str"}"' : self.element_type.get_files_and_data_template_representation(**kwargs)
73
- }
74
-
75
- @classmethod
76
- def from_yaml(cls, namespace: str, yaml_data: Dict[str, Any], **kwargs: Any) -> "DictionarySchema":
77
- """Constructs a DictionarySchema from yaml data.
78
-
79
- :param yaml_data: the yaml data from which we will construct this schema
80
- :type yaml_data: dict[str, Any]
81
-
82
- :return: A created DictionarySchema
83
- :rtype: ~autorest.models.DictionarySchema
84
- """
85
- element_schema = yaml_data["elementType"]
86
-
87
- from . import build_schema # pylint: disable=import-outside-toplevel
88
-
89
- element_type = build_schema(
90
- yaml_data=element_schema, **kwargs
91
- )
92
-
93
- return cls(
94
- namespace=namespace,
95
- yaml_data=yaml_data,
96
- element_type=element_type,
97
- )
98
-
99
- def imports(self) -> FileImport:
100
- file_import = FileImport()
101
- file_import.add_submodule_import("typing", "Dict", ImportType.STDLIB, TypingSection.CONDITIONAL)
102
- file_import.merge(self.element_type.imports())
103
- return file_import
@@ -1,246 +0,0 @@
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
- from typing import Any, Dict, List, Optional, Set, Type
7
- from .base_schema import BaseSchema
8
- from .primitive_schemas import PrimitiveSchema, get_primitive_schema, StringSchema
9
- from .imports import FileImport, ImportType, TypingSection
10
-
11
-
12
- class EnumValue:
13
- """Model containing necessary information for a single value of an enum.
14
-
15
- :param str name: The name of this enum value
16
- :param str value: The value of this enum value
17
- :param str description: Optional. The description for this enum value
18
- """
19
-
20
- def __init__(self, name: str, value: str, description: Optional[str] = None) -> None:
21
- self.name = name
22
- self.value = value
23
- self.description = description
24
-
25
- @classmethod
26
- def from_yaml(cls, yaml_data: Dict[str, Any]) -> "EnumValue":
27
- """Constructs an EnumValue from yaml data.
28
-
29
- :param yaml_data: the yaml data from which we will construct this object
30
- :type yaml_data: dict[str, Any]
31
-
32
- :return: A created EnumValue
33
- :rtype: ~autorest.models.EnumValue
34
- """
35
- return cls(
36
- name=yaml_data["language"]["python"]["name"],
37
- value=yaml_data["value"],
38
- description=yaml_data["language"]["python"].get("description"),
39
- )
40
-
41
-
42
- class EnumSchema(BaseSchema):
43
- """Schema for enums that will be serialized.
44
-
45
- :param yaml_data: the yaml data for this schema
46
- :type yaml_data: dict[str, Any]
47
- :param str description: The description of this enum
48
- :param str name: The name of the enum.
49
- :type element_type: ~autorest.models.PrimitiveSchema
50
- :param values: List of the values for this enum
51
- :type values: list[~autorest.models.EnumValue]
52
- """
53
-
54
- def __init__(
55
- self,
56
- namespace: str,
57
- yaml_data: Dict[str, Any],
58
- description: str,
59
- name: str,
60
- values: List["EnumValue"],
61
- enum_type: PrimitiveSchema,
62
- enum_file_name: str
63
- ) -> None:
64
- super(EnumSchema, self).__init__(namespace=namespace, yaml_data=yaml_data)
65
- self.description = description
66
- self.name = name
67
- self.values = values
68
- self.enum_file_name = enum_file_name
69
- self.enum_type = enum_type
70
-
71
- def __lt__(self, other):
72
- return self.name.lower() < other.name.lower()
73
-
74
- @property
75
- def serialization_type(self) -> str:
76
- """Returns the serialization value for msrest.
77
-
78
- :return: The serialization value for msrest
79
- :rtype: str
80
- """
81
- return self.enum_type.serialization_type
82
-
83
- @property
84
- def type_annotation(self) -> str:
85
- """The python type used for type annotation
86
-
87
- :return: The type annotation for this schema
88
- :rtype: str
89
- """
90
- return f'Union[{self.enum_type.type_annotation}, "{self.name}"]'
91
-
92
- @property
93
- def operation_type_annotation(self) -> str:
94
- """The python type used for type annotation
95
-
96
- :return: The type annotation for this schema
97
- :rtype: str
98
- """
99
- return f'Union[{self.enum_type.type_annotation}, "_models.{self.name}"]'
100
-
101
- def get_declaration(self, value: Any) -> str:
102
- return self.enum_type.get_declaration(value)
103
-
104
- @property
105
- def docstring_text(self) -> str:
106
- return self.name
107
-
108
- @property
109
- def docstring_type(self) -> str:
110
- """The python type used for RST syntax input and type annotation.
111
- """
112
- return f"{self.enum_type.type_annotation} or ~{self.namespace}.models.{self.name}"
113
-
114
- @staticmethod
115
- def _get_enum_values(yaml_data: List[Dict[str, Any]]) -> List["EnumValue"]:
116
- """Creates the list of values for this enum.
117
-
118
- :param yaml_data: yaml data about the enum's values
119
- :type yaml_data: dict[str, Any]
120
- :return: The list of values for this enum
121
- :rtype: list[~autorest.models.EnumValue]
122
- """
123
- values = []
124
- seen_enums: Set[str] = set()
125
-
126
- for enum in yaml_data:
127
- enum_name = enum["language"]["python"]["name"]
128
- if enum_name in seen_enums:
129
- continue
130
- values.append(EnumValue.from_yaml(enum))
131
- seen_enums.add(enum_name)
132
- return values
133
-
134
- def _template_kwargs(self, **kwargs: Any) -> Any:
135
- if len(self.values) == 1 and not kwargs.get("default_value_declaration"):
136
- kwargs['default_value_declaration'] = self.enum_type.get_declaration(self.values[0].value)
137
- description = kwargs.pop("description", "")
138
- kwargs["description"] = description
139
- return kwargs
140
-
141
-
142
- def get_json_template_representation(self, **kwargs: Any) -> Any:
143
- # for better display effect, use the only value instead of var type
144
- return self.enum_type.get_json_template_representation(**self._template_kwargs(**kwargs))
145
-
146
- def get_files_and_data_template_representation(self, **kwargs: Any) -> Any:
147
- return self.enum_type.get_files_and_data_template_representation(**self._template_kwargs(**kwargs))
148
-
149
- @classmethod
150
- def from_yaml(cls, namespace: str, yaml_data: Dict[str, Any], **kwargs: Any) -> "EnumSchema":
151
- """Constructs an EnumSchema from yaml data.
152
-
153
- :param yaml_data: the yaml data from which we will construct this schema
154
- :type yaml_data: dict[str, Any]
155
-
156
- :return: A created EnumSchema
157
- :rtype: ~autorest.models.EnumSchema
158
- """
159
- name = yaml_data["language"]["python"]["name"]
160
-
161
- # choice type doesn't always exist. if there is no choiceType, we default to string
162
- if yaml_data.get("choiceType"):
163
- enum_type = get_primitive_schema(namespace, yaml_data["choiceType"])
164
- else:
165
- enum_type = StringSchema(namespace, {"type": "str"})
166
- values = EnumSchema._get_enum_values(yaml_data["choices"])
167
- code_model = kwargs.pop("code_model")
168
-
169
- return cls(
170
- namespace=namespace,
171
- yaml_data=yaml_data,
172
- description=yaml_data["language"]["python"]["description"],
173
- name=name,
174
- values=values,
175
- enum_type=enum_type,
176
- enum_file_name=f"_{code_model.module_name}_enums"
177
- )
178
-
179
- def imports(self) -> FileImport:
180
- file_import = FileImport()
181
- file_import.add_submodule_import("typing", "Union", ImportType.STDLIB, TypingSection.CONDITIONAL)
182
- file_import.merge(self.enum_type.imports())
183
- return file_import
184
-
185
- def model_file_imports(self) -> FileImport:
186
- imports = self.imports()
187
- # we import every enum since we can get extremely long imports
188
- # if we import my name
189
- imports.add_submodule_import("." + self.enum_file_name, "*", ImportType.LOCAL)
190
- return imports
191
-
192
- class HiddenModelEnumSchema(EnumSchema):
193
-
194
- def imports(self) -> FileImport:
195
- file_import = FileImport()
196
- file_import.merge(self.enum_type.imports())
197
- return file_import
198
-
199
- @property
200
- def type_annotation(self) -> str:
201
- """The python type used for type annotation
202
-
203
- :return: The type annotation for this schema
204
- :rtype: str
205
- """
206
- return self.enum_type.type_annotation
207
-
208
- @property
209
- def operation_type_annotation(self) -> str:
210
- """The python type used for type annotation
211
-
212
- :return: The type annotation for this schema
213
- :rtype: str
214
- """
215
- return self.enum_type.type_annotation
216
-
217
- @property
218
- def docstring_text(self) -> str:
219
- return f"{self.enum_type.type_annotation}. {self.extra_description_information}"
220
-
221
- @property
222
- def extra_description_information(self):
223
- possible_values = [self.get_declaration(v.value) for v in self.values]
224
- if not possible_values:
225
- return ""
226
- if len(possible_values) == 1:
227
- return possible_values[0]
228
- if len(possible_values) == 2:
229
- possible_values_str = " or ".join(possible_values)
230
- else:
231
- possible_values_str = ", ".join(
232
- possible_values[: len(possible_values) - 1]
233
- ) + f", and {possible_values[-1]}"
234
-
235
- return "Possible values are: {}.".format(possible_values_str)
236
-
237
- @property
238
- def docstring_type(self) -> str:
239
- """The python type used for RST syntax input and type annotation.
240
- """
241
- return self.enum_type.type_annotation
242
-
243
- def get_enum_schema(code_model) -> Type[EnumSchema]:
244
- if code_model.options["models_mode"]:
245
- return EnumSchema
246
- return HiddenModelEnumSchema
@@ -1,113 +0,0 @@
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
- from typing import Any, Dict, Optional, Union
7
- from .base_schema import BaseSchema
8
- from .imports import FileImport, ImportType, TypingSection
9
-
10
-
11
- class ListSchema(BaseSchema):
12
- def __init__(
13
- self,
14
- namespace: str,
15
- yaml_data: Dict[str, Any],
16
- element_type: BaseSchema,
17
- *,
18
- max_items: Optional[int] = None,
19
- min_items: Optional[int] = None,
20
- unique_items: Optional[int] = None,
21
- ) -> None:
22
- super(ListSchema, self).__init__(namespace=namespace, yaml_data=yaml_data)
23
- self.element_type = element_type
24
- self.max_items = max_items
25
- self.min_items = min_items
26
- self.unique_items = unique_items
27
-
28
- @property
29
- def serialization_type(self) -> str:
30
- return f"[{self.element_type.serialization_type}]"
31
-
32
- @property
33
- def type_annotation(self) -> str:
34
- return f"List[{self.element_type.type_annotation}]"
35
-
36
- @property
37
- def operation_type_annotation(self) -> str:
38
- return f"List[{self.element_type.operation_type_annotation}]"
39
-
40
- @property
41
- def docstring_type(self) -> str:
42
- return f"list[{self.element_type.docstring_type}]"
43
-
44
- @property
45
- def docstring_text(self) -> str:
46
- return f"list of {self.element_type.docstring_text}"
47
-
48
- @property
49
- def validation_map(self) -> Optional[Dict[str, Union[bool, int, str]]]:
50
- validation_map: Dict[str, Union[bool, int, str]] = {}
51
- if self.max_items:
52
- validation_map["max_items"] = self.max_items
53
- validation_map["min_items"] = self.min_items or 0
54
- if self.min_items:
55
- validation_map["min_items"] = self.min_items
56
- if self.unique_items:
57
- validation_map["unique"] = True
58
- return validation_map or None
59
-
60
- @property
61
- def has_xml_serialization_ctxt(self) -> bool:
62
- return super().has_xml_serialization_ctxt or self.element_type.has_xml_serialization_ctxt
63
-
64
- def get_json_template_representation(self, **kwargs: Any) -> Any:
65
- return [self.element_type.get_json_template_representation(**kwargs)]
66
-
67
- def get_files_and_data_template_representation(self, **kwargs: Any) -> Any:
68
- return [self.element_type.get_files_and_data_template_representation(**kwargs)]
69
-
70
- def xml_serialization_ctxt(self) -> Optional[str]:
71
- attrs_list = []
72
- base_xml_map = super().xml_serialization_ctxt()
73
- if base_xml_map:
74
- attrs_list.append(base_xml_map)
75
-
76
- # Attribute at the list level
77
- if self.xml_metadata.get("wrapped", False):
78
- attrs_list.append("'wrapped': True")
79
-
80
- # Attributes of the items
81
- item_xml_metadata = self.element_type.xml_metadata
82
- if item_xml_metadata.get("name"):
83
- attrs_list.append(f"'itemsName': '{item_xml_metadata['name']}'")
84
- if item_xml_metadata.get("prefix", False):
85
- attrs_list.append(f"'itemsPrefix': '{item_xml_metadata['prefix']}'")
86
- if item_xml_metadata.get("namespace", False):
87
- attrs_list.append(f"'itemsNs': '{item_xml_metadata['namespace']}'")
88
-
89
- return ", ".join(attrs_list)
90
-
91
- @classmethod
92
- def from_yaml(cls, namespace: str, yaml_data: Dict[str, Any], **kwargs) -> "ListSchema":
93
- # TODO: for items, if the type is a primitive is it listed in type instead of $ref?
94
- element_schema = yaml_data["elementType"]
95
-
96
- from . import build_schema # pylint: disable=import-outside-toplevel
97
-
98
- element_type = build_schema(yaml_data=element_schema, **kwargs)
99
-
100
- return cls(
101
- namespace=namespace,
102
- yaml_data=yaml_data,
103
- element_type=element_type,
104
- max_items=yaml_data.get("maxItems"),
105
- min_items=yaml_data.get("minItems"),
106
- unique_items=yaml_data.get("uniqueItems"),
107
- )
108
-
109
- def imports(self) -> FileImport:
110
- file_import = FileImport()
111
- file_import.add_submodule_import("typing", "List", ImportType.STDLIB, TypingSection.CONDITIONAL)
112
- file_import.merge(self.element_type.imports())
113
- return file_import