Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 15 additions & 17 deletions autorest/codegen/templates/operation.py.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,24 @@
{% import 'operation_tools.jinja2' as op_tools %}
{% set trace_decorator = "@distributed_trace_async" if async_mode else "@distributed_trace" %}
{% set stream_request_parameter = "stream=" ~ ("True" if operation.is_stream_response else "False") %}
{% macro return_docstring(async_mode) %}
{% if operation.responses | selectattr('has_body') | first %}
:return: {{ operation.responses|selectattr('has_body')|map(attribute='schema')| map(attribute='docstring_text')|unique|join(' or ') }}, or the result of cls(response)
:rtype: {{ operation.responses|selectattr('has_body')|map(attribute='schema')| map(attribute='docstring_type')|unique|join(' or ') }}{{ " or None" if operation.responses|selectattr('has_body', 'false') | first }}
{%- else %}
:return: None, or the result of cls(response)
:rtype: None
{%- endif -%}
{% endmacro %}
{# return_type is a variable that holds the return type if we already know what it is #}
{% set return_type = "bool" if operation.method == 'head' and code_model.options['head_as_boolean'] else None %}
{% macro param_documentation_string(parameter) %}:param {{ parameter.serialized_name }}: {{ parameter.description }}{% endmacro %}
{% macro return_docstring(async_mode) %}
{% if operation.responses | selectattr('has_body') | first %}
{% macro return_docstring(async_mode, return_type=None) %}
{%- if return_type -%}
:return: {{ return_type }}, or the result of cls(response)
:rtype: {{ return_type }}
{%- else -%}
{% if operation.responses | selectattr('has_body') | first %}
:return: {{ operation.responses|selectattr('has_body')|map(attribute='schema')| map(attribute='docstring_text')|unique|join(' or ') }}, or the result of cls(response)
:rtype: {{ operation.responses|selectattr('has_body')|map(attribute='schema')| map(attribute='docstring_type')|unique|join(' or ') }}{{ " or None" if operation.has_optional_return_type }}
{%- else %}
{%- else %}
:return: None, or the result of cls(response)
:rtype: None
{%- endif -%}
{%- endif -%}
{% endmacro %}
{% macro operation_docstring(async_mode) %}
{% macro operation_docstring(async_mode, return_type=None) %}
"""{{ operation.summary if operation.summary else operation.description | wordwrap(width=95, break_long_words=False, wrapstring='\n') }}
{% if operation.summary and operation.description %}

Expand All @@ -45,19 +43,19 @@
Allowed values are: "{{ operation.requests | map(attribute="media_types") | sum(start = []) | unique | list | join ('", "') }}".
{% endif %}
:keyword callable cls: A custom type or function that will be passed the direct response
{{ return_docstring(async_mode) }}
{{ return_docstring(async_mode, return_type=return_type) }}
:raises: ~azure.core.exceptions.HttpResponseError
"""{% endmacro %}
{# actual template starts here #}
{%- if code_model.options['tracing'] and operation.want_tracing -%}
{{ trace_decorator }}
{% endif %}
{{ op_tools.method_signature(operation, operation.python_name, async_mode=async_mode, coroutine=async_mode, return_type_wrapper="") }}
{{ op_tools.method_signature(operation, operation.python_name, async_mode=async_mode, coroutine=async_mode, return_type_wrapper="", return_type=return_type) }}
{%- if not async_mode %}
{{ op_tools.sync_return_type_annotation(operation, "") }}
{{ op_tools.sync_return_type_annotation(operation, "", return_type=return_type) }}
{% endif %}
{% if operation.want_description_docstring %}
{{ operation_docstring(async_mode)|indent }}
{{ operation_docstring(async_mode, return_type=return_type)|indent }}
{% endif %}
cls = kwargs.pop('cls', None) # type: {{ op_tools.return_type_annotation(operation, "ClsType") }}
{% if operation.deprecated %}
Expand Down
19 changes: 11 additions & 8 deletions autorest/codegen/templates/operation_tools.jinja2
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
{% macro return_type_annotation(operation, return_type_wrapper) %}
{% macro return_type_annotation(operation, return_type_wrapper, return_type=None) %}
{%- if return_type -%}
{{ return_type }}
{%- else -%}
{{ ((return_type_wrapper + "[") if return_type_wrapper else "") ~ ("Optional[" if operation.has_optional_return_type else "" ) ~ ("Union[" if operation.responses | selectattr('has_body') | map(attribute='schema') | map(attribute='operation_type_annotation') | unique | list | length > 1 else "") ~
(operation.responses | selectattr('has_body') | map(attribute='schema') | map(attribute='operation_type_annotation') | unique | join(', ')) ~
("]" if operation.responses | selectattr('has_body') | map(attribute='schema') | map(attribute='operation_type_annotation')| unique | list | length > 1 else "") ~ ("]" if operation.has_optional_return_type else "") ~ ( "]" if return_type_wrapper else "")
if operation.responses | selectattr('has_body') | first
else ((return_type_wrapper + "[") if return_type_wrapper else "") ~ ("Optional[" if operation.has_optional_return_type else "" ) ~ "None" ~ ("]" if operation.has_optional_return_type else "") ~ ( "]" if return_type_wrapper else "") }}{% endmacro %}
else ((return_type_wrapper + "[") if return_type_wrapper else "") ~ ("Optional[" if operation.has_optional_return_type else "" ) ~ "None" ~ ("]" if operation.has_optional_return_type else "") ~ ( "]" if return_type_wrapper else "") }}{%- endif -%}{% endmacro %}
{# get async mypy typing #}
{% macro async_return_type_annotation(operation, return_type_wrapper) %}
{{ " -> " + return_type_annotation(operation, return_type_wrapper) }}{% endmacro %}
{% macro async_return_type_annotation(operation, return_type_wrapper, return_type=None) %}
{{ " -> " + return_type_annotation(operation, return_type_wrapper, return_type) }}{% endmacro %}
{# get sync mypy typing #}
{% macro sync_return_type_annotation(operation, return_type_wrapper) %}
{{ "# type: (...) -> " + return_type_annotation(operation, return_type_wrapper) }}{% endmacro %}
{% macro sync_return_type_annotation(operation, return_type_wrapper, return_type=None) %}
{{ "# type: (...) -> " + return_type_annotation(operation, return_type_wrapper, return_type) }}{% endmacro %}
{# get method signature #}
{% macro method_signature(operation, operation_name, async_mode, coroutine, return_type_wrapper) %}
{% macro method_signature(operation, operation_name, async_mode, coroutine, return_type_wrapper, return_type=None) %}
{{ "async " if coroutine else "" }}def {{ operation_name }}(
{% if async_mode %}
self,
{% for param_signature in operation.parameters.async_method_signature %}
{{ param_signature }},
{% endfor %}
**kwargs
){{ async_return_type_annotation(operation, return_type_wrapper) }}:
){{ async_return_type_annotation(operation, return_type_wrapper, return_type) }}:
{% else %}
self,
{% for param_signature in operation.parameters.sync_method_signature %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,14 +143,14 @@ async def custom_named_request_id_head(
self,
foo_client_request_id: str,
**kwargs
) -> None:
) -> bool:
"""Send foo-client-request-id = 9C4D50EE-2D56-4CD3-8152-34347DC9F2B0 in the header of the request.

:param foo_client_request_id: The fooRequestId.
:type foo_client_request_id: str
:keyword callable cls: A custom type or function that will be passed the direct response
:return: None, or the result of cls(response)
:rtype: None
:return: bool, or the result of cls(response)
:rtype: bool
:raises: ~azure.core.exceptions.HttpResponseError
"""
cls = kwargs.pop('cls', None) # type: ClsType[None]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,14 +150,14 @@ def custom_named_request_id_head(
foo_client_request_id, # type: str
**kwargs # type: Any
):
# type: (...) -> None
# type: (...) -> bool
"""Send foo-client-request-id = 9C4D50EE-2D56-4CD3-8152-34347DC9F2B0 in the header of the request.

:param foo_client_request_id: The fooRequestId.
:type foo_client_request_id: str
:keyword callable cls: A custom type or function that will be passed the direct response
:return: None, or the result of cls(response)
:rtype: None
:return: bool, or the result of cls(response)
:rtype: bool
:raises: ~azure.core.exceptions.HttpResponseError
"""
cls = kwargs.pop('cls', None) # type: ClsType[None]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ def __init__(self, client, config, serializer, deserializer) -> None:
async def head200(
self,
**kwargs
) -> None:
) -> bool:
"""Return 200 status code if successful.

:keyword callable cls: A custom type or function that will be passed the direct response
:return: None, or the result of cls(response)
:rtype: None
:return: bool, or the result of cls(response)
:rtype: bool
:raises: ~azure.core.exceptions.HttpResponseError
"""
cls = kwargs.pop('cls', None) # type: ClsType[None]
Expand Down Expand Up @@ -78,12 +78,12 @@ async def head200(
async def head204(
self,
**kwargs
) -> None:
) -> bool:
"""Return 204 status code if successful.

:keyword callable cls: A custom type or function that will be passed the direct response
:return: None, or the result of cls(response)
:rtype: None
:return: bool, or the result of cls(response)
:rtype: bool
:raises: ~azure.core.exceptions.HttpResponseError
"""
cls = kwargs.pop('cls', None) # type: ClsType[None]
Expand Down Expand Up @@ -117,12 +117,12 @@ async def head204(
async def head404(
self,
**kwargs
) -> None:
) -> bool:
"""Return 404 status code if successful.

:keyword callable cls: A custom type or function that will be passed the direct response
:return: None, or the result of cls(response)
:rtype: None
:return: bool, or the result of cls(response)
:rtype: bool
:raises: ~azure.core.exceptions.HttpResponseError
"""
cls = kwargs.pop('cls', None) # type: ClsType[None]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ def head200(
self,
**kwargs # type: Any
):
# type: (...) -> None
# type: (...) -> bool
"""Return 200 status code if successful.

:keyword callable cls: A custom type or function that will be passed the direct response
:return: None, or the result of cls(response)
:rtype: None
:return: bool, or the result of cls(response)
:rtype: bool
:raises: ~azure.core.exceptions.HttpResponseError
"""
cls = kwargs.pop('cls', None) # type: ClsType[None]
Expand Down Expand Up @@ -84,12 +84,12 @@ def head204(
self,
**kwargs # type: Any
):
# type: (...) -> None
# type: (...) -> bool
"""Return 204 status code if successful.

:keyword callable cls: A custom type or function that will be passed the direct response
:return: None, or the result of cls(response)
:rtype: None
:return: bool, or the result of cls(response)
:rtype: bool
:raises: ~azure.core.exceptions.HttpResponseError
"""
cls = kwargs.pop('cls', None) # type: ClsType[None]
Expand Down Expand Up @@ -124,12 +124,12 @@ def head404(
self,
**kwargs # type: Any
):
# type: (...) -> None
# type: (...) -> bool
"""Return 404 status code if successful.

:keyword callable cls: A custom type or function that will be passed the direct response
:return: None, or the result of cls(response)
:rtype: None
:return: bool, or the result of cls(response)
:rtype: bool
:raises: ~azure.core.exceptions.HttpResponseError
"""
cls = kwargs.pop('cls', None) # type: ClsType[None]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ def __init__(self, client, config, serializer, deserializer) -> None:
async def head200(
self,
**kwargs
) -> None:
) -> bool:
"""Return 200 status code if successful.

:keyword callable cls: A custom type or function that will be passed the direct response
:return: None, or the result of cls(response)
:rtype: None
:return: bool, or the result of cls(response)
:rtype: bool
:raises: ~azure.core.exceptions.HttpResponseError
"""
cls = kwargs.pop('cls', None) # type: ClsType[None]
Expand Down Expand Up @@ -78,12 +78,12 @@ async def head200(
async def head204(
self,
**kwargs
) -> None:
) -> bool:
"""Return 204 status code if successful.

:keyword callable cls: A custom type or function that will be passed the direct response
:return: None, or the result of cls(response)
:rtype: None
:return: bool, or the result of cls(response)
:rtype: bool
:raises: ~azure.core.exceptions.HttpResponseError
"""
cls = kwargs.pop('cls', None) # type: ClsType[None]
Expand Down Expand Up @@ -117,12 +117,12 @@ async def head204(
async def head404(
self,
**kwargs
) -> None:
) -> bool:
"""Return 404 status code if successful.

:keyword callable cls: A custom type or function that will be passed the direct response
:return: None, or the result of cls(response)
:rtype: None
:return: bool, or the result of cls(response)
:rtype: bool
:raises: ~azure.core.exceptions.HttpResponseError
"""
cls = kwargs.pop('cls', None) # type: ClsType[None]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ def head200(
self,
**kwargs # type: Any
):
# type: (...) -> None
# type: (...) -> bool
"""Return 200 status code if successful.

:keyword callable cls: A custom type or function that will be passed the direct response
:return: None, or the result of cls(response)
:rtype: None
:return: bool, or the result of cls(response)
:rtype: bool
:raises: ~azure.core.exceptions.HttpResponseError
"""
cls = kwargs.pop('cls', None) # type: ClsType[None]
Expand Down Expand Up @@ -84,12 +84,12 @@ def head204(
self,
**kwargs # type: Any
):
# type: (...) -> None
# type: (...) -> bool
"""Return 204 status code if successful.

:keyword callable cls: A custom type or function that will be passed the direct response
:return: None, or the result of cls(response)
:rtype: None
:return: bool, or the result of cls(response)
:rtype: bool
:raises: ~azure.core.exceptions.HttpResponseError
"""
cls = kwargs.pop('cls', None) # type: ClsType[None]
Expand Down Expand Up @@ -124,12 +124,12 @@ def head404(
self,
**kwargs # type: Any
):
# type: (...) -> None
# type: (...) -> bool
"""Return 404 status code if successful.

:keyword callable cls: A custom type or function that will be passed the direct response
:return: None, or the result of cls(response)
:rtype: None
:return: bool, or the result of cls(response)
:rtype: bool
:raises: ~azure.core.exceptions.HttpResponseError
"""
cls = kwargs.pop('cls', None) # type: ClsType[None]
Expand Down