@autorest/python 5.14.0 → 5.15.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 (58) hide show
  1. package/ChangeLog.md +29 -0
  2. package/autorest/codegen/__init__.py +87 -47
  3. package/autorest/codegen/models/base_schema.py +2 -6
  4. package/autorest/codegen/models/client.py +6 -0
  5. package/autorest/codegen/models/code_model.py +40 -74
  6. package/autorest/codegen/models/constant_schema.py +7 -3
  7. package/autorest/codegen/models/credential_model.py +47 -0
  8. package/autorest/codegen/models/credential_schema.py +5 -4
  9. package/autorest/codegen/models/dictionary_schema.py +7 -7
  10. package/autorest/codegen/models/enum_schema.py +8 -39
  11. package/autorest/codegen/models/imports.py +3 -1
  12. package/autorest/codegen/models/list_schema.py +18 -8
  13. package/autorest/codegen/models/lro_operation.py +3 -3
  14. package/autorest/codegen/models/lro_paging_operation.py +3 -3
  15. package/autorest/codegen/models/object_schema.py +17 -13
  16. package/autorest/codegen/models/operation.py +27 -6
  17. package/autorest/codegen/models/operation_group.py +7 -2
  18. package/autorest/codegen/models/paging_operation.py +3 -3
  19. package/autorest/codegen/models/parameter.py +39 -15
  20. package/autorest/codegen/models/parameter_list.py +1 -1
  21. package/autorest/codegen/models/primitive_schemas.py +15 -25
  22. package/autorest/codegen/models/property.py +5 -5
  23. package/autorest/codegen/models/request_builder.py +4 -4
  24. package/autorest/codegen/models/request_builder_parameter.py +12 -5
  25. package/autorest/codegen/models/schema_response.py +23 -10
  26. package/autorest/codegen/models/utils.py +20 -0
  27. package/autorest/codegen/serializers/__init__.py +49 -25
  28. package/autorest/codegen/serializers/builder_serializer.py +79 -37
  29. package/autorest/codegen/serializers/client_serializer.py +16 -6
  30. package/autorest/codegen/serializers/general_serializer.py +24 -3
  31. package/autorest/codegen/serializers/import_serializer.py +1 -1
  32. package/autorest/codegen/serializers/metadata_serializer.py +1 -1
  33. package/autorest/codegen/serializers/model_base_serializer.py +8 -0
  34. package/autorest/codegen/serializers/model_python3_serializer.py +2 -2
  35. package/autorest/codegen/serializers/operation_groups_serializer.py +1 -0
  36. package/autorest/codegen/serializers/patch_serializer.py +12 -3
  37. package/autorest/codegen/serializers/utils.py +29 -4
  38. package/autorest/codegen/templates/config.py.jinja2 +4 -4
  39. package/autorest/codegen/templates/enum.py.jinja2 +1 -1
  40. package/autorest/codegen/templates/enum_container.py.jinja2 +0 -1
  41. package/autorest/codegen/templates/init.py.jinja2 +9 -6
  42. package/autorest/codegen/templates/keywords.jinja2 +14 -1
  43. package/autorest/codegen/templates/lro_operation.py.jinja2 +1 -1
  44. package/autorest/codegen/templates/lro_paging_operation.py.jinja2 +1 -1
  45. package/autorest/codegen/templates/metadata.json.jinja2 +3 -3
  46. package/autorest/codegen/templates/model.py.jinja2 +1 -6
  47. package/autorest/codegen/templates/model_init.py.jinja2 +7 -4
  48. package/autorest/codegen/templates/operation.py.jinja2 +2 -3
  49. package/autorest/codegen/templates/operation_group.py.jinja2 +12 -5
  50. package/autorest/codegen/templates/operation_groups_container.py.jinja2 +0 -1
  51. package/autorest/codegen/templates/operations_folder_init.py.jinja2 +4 -0
  52. package/autorest/codegen/templates/paging_operation.py.jinja2 +1 -1
  53. package/autorest/codegen/templates/patch.py.jinja2 +18 -29
  54. package/autorest/codegen/templates/request_builder.py.jinja2 +4 -6
  55. package/autorest/codegen/templates/vendor.py.jinja2 +12 -2
  56. package/autorest/multiapi/models/imports.py +21 -11
  57. package/autorest/multiapi/serializers/import_serializer.py +3 -1
  58. package/package.json +2 -2
@@ -92,9 +92,9 @@
92
92
  },
93
93
  "config": {
94
94
  "credential": {{ code_model.options['credential'] | tojson }},
95
- "credential_scopes": {{ (code_model.credential_schema_policy.credential_scopes if code_model.options['credential'] and code_model.credential_schema_policy.credential_scopes is defined else None)| tojson}},
96
- "credential_call_sync": {{ (code_model.credential_schema_policy.call(async_mode=False) if code_model.options['credential'] else None) | tojson }},
97
- "credential_call_async": {{ (code_model.credential_schema_policy.call(async_mode=True) if code_model.options['credential'] else None) | tojson }},
95
+ "credential_scopes": {{ (code_model.credential_model.credential_schema_policy.credential_scopes if code_model.options['credential'] and code_model.credential_model.credential_schema_policy.credential_scopes is defined else None)| tojson}},
96
+ "credential_call_sync": {{ (code_model.credential_model.credential_schema_policy.call(async_mode=False) if code_model.options['credential'] else None) | tojson }},
97
+ "credential_call_async": {{ (code_model.credential_model.credential_schema_policy.call(async_mode=True) if code_model.options['credential'] else None) | tojson }},
98
98
  "sync_imports": {{ sync_config_imports | tojson }},
99
99
  "async_imports": {{ async_config_imports | tojson }}
100
100
  },
@@ -1,12 +1,7 @@
1
- {% set basename = model.base_models | join(', ', attribute='name') if model.base_models else "" %}
2
1
  {# actual template starts here #}
3
2
 
4
3
 
5
- {% if basename %}
6
- class {{ model.name }}({{ basename }}):
7
- {% else %}
8
- class {{ model.name }}(msrest.serialization.Model):
9
- {% endif %}
4
+ {{ declare_model(model) }}
10
5
  """{{ model.description }}
11
6
  {% if model.discriminator_name %}
12
7
 
@@ -1,8 +1,9 @@
1
+ {% import 'keywords.jinja2' as keywords %}
1
2
  # coding=utf-8
2
3
  {{ code_model.options['license_header'] }}
3
4
  {% macro iterate_models_py3() %}
4
5
  {% for schema in schemas %}
5
- from ._models_py3 import {{ schema }}
6
+ from .{{ code_model.get_models_filename(is_python3_file=True) }} import {{ schema }}
6
7
  {% endfor %}
7
8
  {% endmacro %}
8
9
 
@@ -14,19 +15,19 @@ try:
14
15
  {{ iterate_models_py3() | indent-}}
15
16
  except (SyntaxError, ImportError):
16
17
  {% for schema in schemas %}
17
- from ._models import {{ schema }} # type: ignore
18
+ from .{{ code_model.get_models_filename(is_python3_file=False) }} import {{ schema }} # type: ignore
18
19
  {% endfor %}
19
20
  {% endif %}
20
21
  {% endif %}
21
22
  {% if enums %}
22
23
 
23
- from ._{{ code_model.module_name }}_enums import (
24
+ from .{{ code_model.enums_filename }} import (
24
25
  {% for enum in enums %}
25
26
  {{ enum }},
26
27
  {% endfor %}
27
28
  )
28
29
  {% endif %}
29
-
30
+ {{ keywords.patch_imports() }}
30
31
  __all__ = [
31
32
  {% for schema in schemas %}
32
33
  '{{ schema }}',
@@ -37,3 +38,5 @@ __all__ = [
37
38
  {% endfor %}
38
39
  {% endif %}
39
40
  ]
41
+ {{ keywords.extend_all }}
42
+ _patch_sdk()
@@ -9,16 +9,15 @@
9
9
  {{ operation_serializer.method_signature_and_response_type_annotation(operation) }}
10
10
  {% if operation.want_description_docstring %}
11
11
  {{ op_tools.description(operation, operation_serializer) | indent }}{% endif %}
12
- cls = kwargs.pop('cls', None) {{ operation_serializer.cls_type_annotation(operation) }}
13
12
  {% if operation.deprecated %}
14
13
  warnings.warn('Method {{operation.name}} is deprecated', DeprecationWarning)
15
14
  {% endif %}
16
15
  {{ op_tools.serialize(operation_serializer.error_map(operation)) | indent }}
17
- {% if operation.parameters.kwargs_to_pop(async_mode) %}
16
+ {% if operation_serializer.pop_kwargs_from_signature(operation) %}
18
17
  {{ op_tools.serialize(operation_serializer.pop_kwargs_from_signature(operation)) | indent }}
19
18
  {% endif %}
20
19
  {{ op_tools.serialize(operation_serializer.call_request_builder(operation)) | indent }}
21
- pipeline_response = {{ keywords.await }}self._client._pipeline.run( # pylint: disable=protected-access
20
+ pipeline_response = {{ keywords.await }}self._client._pipeline.run( # type: ignore # pylint: disable=protected-access
22
21
  request,
23
22
  {{ stream_request_parameter }},
24
23
  **kwargs
@@ -1,4 +1,11 @@
1
1
  {% set disable = " # pylint: disable=too-many-public-methods" if operation_group.operations | length > 20 else "" %}
2
+ {% if operation_group.is_empty_operation_group and code_model.need_mixin_abc %}
3
+ {% set object_base_class = "(MixinABC)" %}
4
+ {% elif not (async_mode or code_model.options["python3_only"]) %}
5
+ {% set object_base_class = "(object)" %}
6
+ {% else %}
7
+ {% set object_base_class = "" %}
8
+ {% endif %}
2
9
  class {{ operation_group.class_name }}{{ object_base_class }}:{{ disable }}
3
10
  {% if not operation_group.is_empty_operation_group %}
4
11
  """
@@ -15,11 +22,11 @@ class {{ operation_group.class_name }}{{ object_base_class }}:{{ disable }}
15
22
 
16
23
  {% endif %}
17
24
  def __init__(self, *args, **kwargs){{ return_none_type_annotation }}:
18
- args = list(args)
19
- self._client = args.pop(0) if args else kwargs.pop("client")
20
- self._config = args.pop(0) if args else kwargs.pop("config")
21
- self._serialize = args.pop(0) if args else kwargs.pop("serializer")
22
- self._deserialize = args.pop(0) if args else kwargs.pop("deserializer")
25
+ input_args = list(args)
26
+ self._client = input_args.pop(0) if input_args else kwargs.pop("client")
27
+ self._config = input_args.pop(0) if input_args else kwargs.pop("config")
28
+ self._serialize = input_args.pop(0) if input_args else kwargs.pop("serializer")
29
+ self._deserialize = input_args.pop(0) if input_args else kwargs.pop("deserializer")
23
30
 
24
31
  {% endif %}
25
32
  {% for operation in operation_group.operations %}
@@ -1,5 +1,4 @@
1
1
  {% import 'operation_tools.jinja2' as op_tools %}
2
- {% set object_base_class = "" if async_mode else "(object)" %}
3
2
  {% set operations_description = "async operations" if async_mode else "operations" %}
4
3
  {% set return_none_type_annotation = " -> None" if async_mode else "" %}
5
4
  # pylint: disable=too-many-lines
@@ -1,9 +1,11 @@
1
1
  {% import 'operation_tools.jinja2' as op_tools %}
2
+ {% import 'keywords.jinja2' as keywords %}
2
3
  {# actual template starts here #}
3
4
  # coding=utf-8
4
5
  {{ code_model.options['license_header'] }}
5
6
 
6
7
  {{ op_tools.serialize(operation_group_imports()) }}
8
+ {{ keywords.patch_imports() }}
7
9
  {% if operation_groups %}
8
10
  __all__ = [
9
11
  {% for operation_group in operation_groups %}
@@ -11,3 +13,5 @@ __all__ = [
11
13
  {% endfor %}
12
14
  ]
13
15
  {% endif %}
16
+ {{ keywords.extend_all }}
17
+ _patch_sdk()
@@ -9,7 +9,7 @@
9
9
  {% if operation.deprecated %}
10
10
  warnings.warn('Method {{operation.name}} is deprecated', DeprecationWarning)
11
11
  {% endif %}
12
- {% if operation.parameters.kwargs_to_pop(async_mode) %}
12
+ {% if operation_serializer.pop_kwargs_from_signature(operation) %}
13
13
  {{ op_tools.serialize(operation_serializer.pop_kwargs_from_signature(operation)) | indent }}
14
14
  {% endif %}
15
15
  {{ op_tools.serialize(operation_serializer.set_up_params_for_pager(operation)) | indent }}
@@ -1,31 +1,20 @@
1
- # coding=utf-8
2
- # --------------------------------------------------------------------------
3
- #
4
- # Copyright (c) Microsoft Corporation. All rights reserved.
5
- #
6
- # The MIT License (MIT)
7
- #
8
- # Permission is hereby granted, free of charge, to any person obtaining a copy
9
- # of this software and associated documentation files (the ""Software""), to
10
- # deal in the Software without restriction, including without limitation the
11
- # rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
12
- # sell copies of the Software, and to permit persons to whom the Software is
13
- # furnished to do so, subject to the following conditions:
14
- #
15
- # The above copyright notice and this permission notice shall be included in
16
- # all copies or substantial portions of the Software.
17
- #
18
- # THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
- # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
- # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
- # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
- # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23
- # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24
- # IN THE SOFTWARE.
25
- #
26
- # --------------------------------------------------------------------------
1
+ # ------------------------------------
2
+ # Copyright (c) Microsoft Corporation.
3
+ # Licensed under the MIT License.
4
+ # ------------------------------------
5
+ """Customize generated code here.
6
+
7
+ Follow our quickstart for examples: https://aka.ms/azsdk/python/dpcodegen/python/customize
8
+ """
9
+ {{ imports }}
10
+
11
+ {% set type_annotation = ": List[str]" %}
12
+ __all__{{ type_annotation if is_python3_file else "" }} = []{{ "" if is_python3_file else (" # type" + type_annotation) }} # Add all objects you want publicly available to users at this package level
27
13
 
28
- # This file is used for handwritten extensions to the generated code. Example:
29
- # https://github.com/Azure/azure-sdk-for-python/blob/main/doc/dev/customize_code/how-to-patch-sdk-code.md
30
14
  def patch_sdk():
31
- pass
15
+ """Do not remove from this file.
16
+
17
+ `patch_sdk` is a last resort escape hatch that allows you to do customizations
18
+ you can't accomplish using the techniques described in
19
+ https://aka.ms/azsdk/python/dpcodegen/python/customize
20
+ """
@@ -4,13 +4,11 @@
4
4
  {% if code_model.options["builders_visibility"] == "public" %}
5
5
  {{ op_tools.description(request_builder, request_builder_serializer) | indent }}
6
6
  {% endif %}
7
- {% if request_builder.parameters.kwargs_to_pop(is_python3_file) %}
7
+ {% if request_builder_serializer.pop_kwargs_from_signature(request_builder) %}
8
8
  {{ op_tools.serialize(request_builder_serializer.pop_kwargs_from_signature(request_builder)) | indent }}
9
- {% endif %}
10
- {% if request_builder.parameters.constant|selectattr("original_parameter", "equalto", None)|selectattr("in_method_code")|selectattr("in_method_signature", "equalto", False) %}
11
- {% for constant_parameter in request_builder.parameters.constant|selectattr("original_parameter", "equalto", None)|selectattr("in_method_code")|selectattr("in_method_signature", "equalto", False) %}
12
- {{ constant_parameter.serialized_name }} = {{ constant_parameter.constant_declaration }}
13
- {% endfor %}
9
+ {%- endif -%}
10
+ {% if request_builder_serializer.declare_non_inputtable_constants(request_builder) %}
11
+ {{ op_tools.serialize(request_builder_serializer.declare_non_inputtable_constants(request_builder)) | indent }}
14
12
  {% endif %}
15
13
  # Construct URL
16
14
  {{ request_builder_serializer.construct_url(request_builder) }}
@@ -1,8 +1,9 @@
1
+ {% import 'keywords.jinja2' as keywords with context %}
1
2
  {{ code_model.options['license_header'] }}
2
3
 
3
4
  {{ imports }}
4
5
 
5
- {% if code_model.need_request_converter %}
6
+ {% if code_model.need_request_converter and not async_mode %}
6
7
  def _convert_request(request, files=None):
7
8
  data = request.content if not files else None
8
9
  request = HttpRequest(method=request.method, url=request.url, headers=request.headers, data=data)
@@ -10,7 +11,7 @@ def _convert_request(request, files=None):
10
11
  request.set_formdata_body(files)
11
12
  return request
12
13
  {% endif %}
13
- {% if code_model.need_format_url %}
14
+ {% if code_model.need_format_url and not async_mode %}
14
15
 
15
16
  def _format_url_section(template, **kwargs):
16
17
  components = template.split("/")
@@ -24,3 +25,12 @@ def _format_url_section(template, **kwargs):
24
25
  ]
25
26
  template = "/".join(components)
26
27
  {% endif %}
28
+ {% if code_model.need_mixin_abc %}
29
+
30
+ class MixinABC(ABC):
31
+ """DO NOT use this class. It is for internal typing use only."""
32
+ _client: "{{ keywords.async_class }}PipelineClient"
33
+ _config: {{ code_model.class_name }}Configuration
34
+ _serialize: "Serializer"
35
+ _deserialize: "Deserializer"
36
+ {% endif %}
@@ -4,7 +4,7 @@
4
4
  # license information.
5
5
  # --------------------------------------------------------------------------
6
6
  from enum import Enum
7
- from typing import Dict, Optional, Set
7
+ from typing import Dict, Optional, Set, Union, Tuple
8
8
 
9
9
 
10
10
  class ImportType(str, Enum):
@@ -21,29 +21,39 @@ class TypingSection(str, Enum):
21
21
 
22
22
  class FileImport:
23
23
  def __init__(
24
- self, imports: Dict[TypingSection, Dict[ImportType, Dict[str, Set[Optional[str]]]]] = None
24
+ self,
25
+ imports: Dict[
26
+ TypingSection, Dict[ImportType, Dict[str, Set[Optional[Union[str, Tuple[str, str]]]]]]
27
+ ] = None
25
28
  ) -> None:
26
29
  # Basic implementation
27
30
  # First level dict: TypingSection
28
31
  # Second level dict: ImportType
29
32
  # Third level dict: the package name.
30
33
  # Fourth level set: None if this import is a "import", the name to import if it's a "from"
31
- self._imports: Dict[TypingSection, Dict[ImportType, Dict[str, Set[Optional[str]]]]] = imports or dict()
34
+ self._imports: Dict[
35
+ TypingSection, Dict[ImportType, Dict[str, Set[Optional[Union[str, Tuple[str, str]]]]]]
36
+ ] = imports or dict()
32
37
 
33
38
  def _add_import(
34
39
  self,
35
40
  from_section: str,
36
41
  import_type: ImportType,
37
- name_import: Optional[str] = None,
42
+ name_import: Optional[Union[str, Tuple[str, str]]] = None,
38
43
  typing_section: TypingSection = TypingSection.REGULAR
39
44
  ) -> None:
45
+ name_input: Optional[Union[str, Tuple[str, str]]] = None
46
+ if isinstance(name_import, list):
47
+ name_input = tuple(name_import)
48
+ else:
49
+ name_input = name_import
40
50
  self._imports.setdefault(
41
- typing_section, dict()
42
- ).setdefault(
43
- import_type, dict()
44
- ).setdefault(
45
- from_section, set()
46
- ).add(name_import)
51
+ typing_section, dict()
52
+ ).setdefault(
53
+ import_type, dict()
54
+ ).setdefault(
55
+ from_section, set()
56
+ ).add(name_input)
47
57
 
48
58
  def add_submodule_import(
49
59
  self,
@@ -66,7 +76,7 @@ class FileImport:
66
76
  self._add_import(name_import, import_type, None, typing_section)
67
77
 
68
78
  @property
69
- def imports(self) -> Dict[TypingSection, Dict[ImportType, Dict[str, Set[Optional[str]]]]]:
79
+ def imports(self) -> Dict[TypingSection, Dict[ImportType, Dict[str, Set[Optional[Union[str, Tuple[str, str]]]]]]]:
70
80
  return self._imports
71
81
 
72
82
  def merge(self, file_import: "FileImport") -> None:
@@ -14,7 +14,9 @@ def _serialize_package(package_name: str, module_list: Set[Optional[str]], delim
14
14
  if module_list != {None}:
15
15
  buffer.append(
16
16
  "from {} import {}".format(
17
- package_name, ", ".join(sorted([mod for mod in module_list if mod is not None]))
17
+ package_name, ", ".join(sorted([
18
+ mod if isinstance(mod, str) else f"{mod[0]} as {mod[1]}" for mod in module_list if mod is not None
19
+ ]))
18
20
  )
19
21
  )
20
22
  return delimiter.join(buffer)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@autorest/python",
3
- "version": "5.14.0",
3
+ "version": "5.15.0",
4
4
  "description": "The Python extension for generators in AutoRest.",
5
5
  "scripts": {
6
6
  "prepare": "node run-python3.js prepare.py",
@@ -27,7 +27,7 @@
27
27
  "@azure-tools/extension": "~3.2.1"
28
28
  },
29
29
  "devDependencies": {
30
- "@microsoft.azure/autorest.testserver": "^3.3.20"
30
+ "@microsoft.azure/autorest.testserver": "3.3.23"
31
31
  },
32
32
  "files": [
33
33
  "autorest/**/*.py",