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
6 changes: 6 additions & 0 deletions autorest/codegen/models/imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ def __init__(
TypingSection,
Dict[ImportType, Dict[str, Set[Optional[Union[str, Tuple[str, str]]]]]]
] = imports or dict()
# has sync and async type definitions
self.type_definitions: Dict[str, Tuple[str, str]] = {}

def _add_import(
self,
Expand Down Expand Up @@ -82,6 +84,10 @@ def imports(self) -> Dict[
]:
return self._imports

def define_mypy_type(self, type_name: str, type_value: str, async_type_value: Optional[str] = None):
self._add_import("typing", ImportType.STDLIB, "TypeVar", TypingSection.CONDITIONAL)
self.type_definitions[type_name] = (type_value, async_type_value or type_value)

def merge(self, file_import: "FileImport") -> None:
"""Merge the given file import format."""
for typing_section, import_type_dict in file_import.imports.items():
Expand Down
7 changes: 6 additions & 1 deletion autorest/codegen/models/operation_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,12 @@ def imports(self, async_mode: bool, has_schemas: bool) -> FileImport:
request_builder.name,
import_type=ImportType.LOCAL
)

type_value = "Optional[Callable[[PipelineResponse[HttpRequest, {}HttpResponse], T, Dict[str, Any]], Any]]"
file_import.define_mypy_type(
"ClsType",
type_value.format(""),
type_value.format("Async")
)
return file_import


Expand Down
21 changes: 18 additions & 3 deletions autorest/codegen/serializers/import_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,10 @@ def _get_import_clauses(


class FileImportSerializer:
def __init__(self, file_import: FileImport, is_python_3_file: bool) -> None:
def __init__(self, file_import: FileImport, is_python_3_file: bool, async_mode: bool = False) -> None:
self._file_import = file_import
self.is_python_3_file = is_python_3_file
self.async_mode = async_mode

def _switch_typing_section_key(self, new_key: TypingSection):
switched_dictionary = {}
Expand All @@ -70,6 +71,21 @@ def _add_type_checking_import(self):
):
self._file_import.add_from_import("typing", "TYPE_CHECKING", ImportType.STDLIB)

def _get_typing_definitions(self) -> str:
if not self._file_import.type_definitions:
return ""
spacing = "" if self.is_python_3_file else " "
declarations: List[str] = [f"\n{spacing}T = TypeVar('T')"]
declarations.extend([
"{}{} = {}".format(
spacing,
type_name,
values[1] if self.async_mode else values[0]
)
for type_name, values in self._file_import.type_definitions.items()
])
return "\n".join(declarations)

def __str__(self) -> str:
self._add_type_checking_import()
regular_imports = ""
Expand All @@ -89,5 +105,4 @@ def __str__(self) -> str:
if typing_imports_dict:
typing_imports += "\n\nif TYPE_CHECKING:\n # pylint: disable=unused-import,ungrouped-imports\n "
typing_imports += "\n\n ".join(_get_import_clauses(typing_imports_dict, "\n "))

return regular_imports + typing_imports
return regular_imports + typing_imports + self._get_typing_definitions()
3 changes: 2 additions & 1 deletion autorest/codegen/serializers/operation_group_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ def _is_paging(operation):
operation_group_temp.imports(
async_mode=self.async_mode,
has_schemas=bool(has_schemas)
), is_python_3_file=self.is_python_3_file
), is_python_3_file=self.is_python_3_file,
async_mode=self.async_mode
),
async_mode=self.async_mode,
is_python_3_file=self.is_python_3_file,
Expand Down
3 changes: 0 additions & 3 deletions autorest/codegen/templates/operations_container.py.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@
{{ code_model.options['license_header'] }}
{{ imports }}

{{ " " if not is_python_3_file }}T = TypeVar('T')
{{ " " if not is_python_3_file }}ClsType = Optional[Callable[[PipelineResponse[HttpRequest, {{ "Async" if async_mode else "" }}HttpResponse], T, Dict[str, Any]], Any]]

{% if code_model.options["builders_visibility"] == "embedded" and not async_mode %}
_SERIALIZER = Serializer()
{% if not is_python_3_file %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@
{{ code_model.options['license_header'] }}
{{ imports }}

{{ " " if not is_python_3_file }}T = TypeVar('T')
{{ " " if not is_python_3_file }}ClsType = Optional[Callable[[PipelineResponse[HttpRequest, {{ "Async" if async_mode else "" }}HttpResponse], T, Dict[str, Any]], Any]]

{% if code_model.options["builders_visibility"] == "embedded" and not async_mode %}
_SERIALIZER = Serializer()
{% if not is_python_3_file %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

from ..._vendor import _convert_request
from ...operations._http_success_operations import build_head200_request, build_head204_request, build_head404_request

T = TypeVar('T')
ClsType = Optional[Callable[[PipelineResponse[HttpRequest, AsyncHttpResponse], T, Dict[str, Any]], Any]]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
if TYPE_CHECKING:
# pylint: disable=unused-import,ungrouped-imports
from typing import Any, Callable, Dict, Generic, Optional, TypeVar

T = TypeVar('T')
ClsType = Optional[Callable[[PipelineResponse[HttpRequest, HttpResponse], T, Dict[str, Any]], Any]]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

from ..._vendor import _convert_request
from ...operations._http_success_operations import build_head200_request, build_head204_request, build_head404_request

T = TypeVar('T')
ClsType = Optional[Callable[[PipelineResponse[HttpRequest, AsyncHttpResponse], T, Dict[str, Any]], Any]]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
if TYPE_CHECKING:
# pylint: disable=unused-import,ungrouped-imports
from typing import Any, Callable, Dict, Generic, Optional, TypeVar

T = TypeVar('T')
ClsType = Optional[Callable[[PipelineResponse[HttpRequest, HttpResponse], T, Dict[str, Any]], Any]]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
from ... import models as _models
from ..._vendor import _convert_request
from ...operations._polling_paging_example_operations import build_basic_paging_request, build_basic_polling_request_initial

T = TypeVar('T')
ClsType = Optional[Callable[[PipelineResponse[HttpRequest, AsyncHttpResponse], T, Dict[str, Any]], Any]]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
if TYPE_CHECKING:
# pylint: disable=unused-import,ungrouped-imports
from typing import Any, Callable, Dict, Generic, Iterable, Optional, TypeVar, Union

T = TypeVar('T')
ClsType = Optional[Callable[[PipelineResponse[HttpRequest, HttpResponse], T, Dict[str, Any]], Any]]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

from ..._vendor import _convert_request
from ...operations._http_success_operations import build_head200_request, build_head204_request, build_head404_request

T = TypeVar('T')
ClsType = Optional[Callable[[PipelineResponse[HttpRequest, AsyncHttpResponse], T, Dict[str, Any]], Any]]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
if TYPE_CHECKING:
# pylint: disable=unused-import,ungrouped-imports
from typing import Any, Callable, Dict, Generic, Optional, TypeVar

T = TypeVar('T')
ClsType = Optional[Callable[[PipelineResponse[HttpRequest, HttpResponse], T, Dict[str, Any]], Any]]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
from ... import models as _models
from ..._vendor import _convert_request
from ...operations._multiapi_service_client_operations import build_test_different_calls_request, build_test_lro_and_paging_request_initial, build_test_lro_request_initial, build_test_one_request

T = TypeVar('T')
ClsType = Optional[Callable[[PipelineResponse[HttpRequest, AsyncHttpResponse], T, Dict[str, Any]], Any]]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
from ... import models as _models
from ..._vendor import _convert_request
from ...operations._operation_group_one_operations import build_test_two_request

T = TypeVar('T')
ClsType = Optional[Callable[[PipelineResponse[HttpRequest, AsyncHttpResponse], T, Dict[str, Any]], Any]]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
if TYPE_CHECKING:
# pylint: disable=unused-import,ungrouped-imports
from typing import Any, Callable, Dict, Generic, Iterable, Optional, TypeVar, Union

T = TypeVar('T')
ClsType = Optional[Callable[[PipelineResponse[HttpRequest, HttpResponse], T, Dict[str, Any]], Any]]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
if TYPE_CHECKING:
# pylint: disable=unused-import,ungrouped-imports
from typing import Any, Callable, Dict, Generic, Optional, TypeVar

T = TypeVar('T')
ClsType = Optional[Callable[[PipelineResponse[HttpRequest, HttpResponse], T, Dict[str, Any]], Any]]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
from ... import models as _models
from ..._vendor import _convert_request
from ...operations._multiapi_service_client_operations import build_test_different_calls_request, build_test_one_request

T = TypeVar('T')
ClsType = Optional[Callable[[PipelineResponse[HttpRequest, AsyncHttpResponse], T, Dict[str, Any]], Any]]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
from ... import models as _models
from ..._vendor import _convert_request
from ...operations._operation_group_one_operations import build_test_three_request, build_test_two_request

T = TypeVar('T')
ClsType = Optional[Callable[[PipelineResponse[HttpRequest, AsyncHttpResponse], T, Dict[str, Any]], Any]]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
from ... import models as _models
from ..._vendor import _convert_request
from ...operations._operation_group_two_operations import build_test_four_request

T = TypeVar('T')
ClsType = Optional[Callable[[PipelineResponse[HttpRequest, AsyncHttpResponse], T, Dict[str, Any]], Any]]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
if TYPE_CHECKING:
# pylint: disable=unused-import,ungrouped-imports
from typing import Any, Callable, Dict, Generic, Optional, TypeVar

T = TypeVar('T')
ClsType = Optional[Callable[[PipelineResponse[HttpRequest, HttpResponse], T, Dict[str, Any]], Any]]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
if TYPE_CHECKING:
# pylint: disable=unused-import,ungrouped-imports
from typing import Any, Callable, Dict, Generic, Optional, TypeVar

T = TypeVar('T')
ClsType = Optional[Callable[[PipelineResponse[HttpRequest, HttpResponse], T, Dict[str, Any]], Any]]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
if TYPE_CHECKING:
# pylint: disable=unused-import,ungrouped-imports
from typing import Any, Callable, Dict, Generic, Optional, TypeVar

T = TypeVar('T')
ClsType = Optional[Callable[[PipelineResponse[HttpRequest, HttpResponse], T, Dict[str, Any]], Any]]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
from ... import models as _models
from ..._vendor import _convert_request
from ...operations._multiapi_service_client_operations import build_test_different_calls_request, build_test_paging_request

T = TypeVar('T')
ClsType = Optional[Callable[[PipelineResponse[HttpRequest, AsyncHttpResponse], T, Dict[str, Any]], Any]]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
from ... import models as _models
from ..._vendor import _convert_request
from ...operations._operation_group_one_operations import build_test_two_request

T = TypeVar('T')
ClsType = Optional[Callable[[PipelineResponse[HttpRequest, AsyncHttpResponse], T, Dict[str, Any]], Any]]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
from ... import models as _models
from ..._vendor import _convert_request
from ...operations._operation_group_two_operations import build_test_five_request, build_test_four_request

T = TypeVar('T')
ClsType = Optional[Callable[[PipelineResponse[HttpRequest, AsyncHttpResponse], T, Dict[str, Any]], Any]]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
if TYPE_CHECKING:
# pylint: disable=unused-import,ungrouped-imports
from typing import Any, Callable, Dict, Generic, Iterable, Optional, TypeVar

T = TypeVar('T')
ClsType = Optional[Callable[[PipelineResponse[HttpRequest, HttpResponse], T, Dict[str, Any]], Any]]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
if TYPE_CHECKING:
# pylint: disable=unused-import,ungrouped-imports
from typing import Any, Callable, Dict, Generic, Optional, TypeVar

T = TypeVar('T')
ClsType = Optional[Callable[[PipelineResponse[HttpRequest, HttpResponse], T, Dict[str, Any]], Any]]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
if TYPE_CHECKING:
# pylint: disable=unused-import,ungrouped-imports
from typing import Any, Callable, Dict, Generic, IO, Optional, TypeVar, Union

T = TypeVar('T')
ClsType = Optional[Callable[[PipelineResponse[HttpRequest, HttpResponse], T, Dict[str, Any]], Any]]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
from ... import models as _models
from ..._vendor import _convert_request
from ...operations._paging_operations import build_first_response_empty_request, build_get_multiple_pages_failure_request, build_get_multiple_pages_failure_uri_request, build_get_multiple_pages_fragment_next_link_request, build_get_multiple_pages_fragment_with_grouping_next_link_request, build_get_multiple_pages_lro_request_initial, build_get_multiple_pages_request, build_get_multiple_pages_retry_first_request, build_get_multiple_pages_retry_second_request, build_get_multiple_pages_with_offset_request, build_get_no_item_name_pages_request, build_get_null_next_link_name_pages_request, build_get_odata_multiple_pages_request, build_get_paging_model_with_item_name_with_xms_client_name_request, build_get_single_pages_failure_request, build_get_single_pages_request, build_get_with_query_params_request, build_next_fragment_request, build_next_fragment_with_grouping_request, build_next_operation_with_query_params_request

T = TypeVar('T')
ClsType = Optional[Callable[[PipelineResponse[HttpRequest, AsyncHttpResponse], T, Dict[str, Any]], Any]]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
if TYPE_CHECKING:
# pylint: disable=unused-import,ungrouped-imports
from typing import Any, Callable, Dict, Generic, Iterable, Optional, TypeVar, Union

T = TypeVar('T')
ClsType = Optional[Callable[[PipelineResponse[HttpRequest, HttpResponse], T, Dict[str, Any]], Any]]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
from custompollerpagerdefinitions.aio import AsyncCustomPager, AsyncCustomPoller

from ...operations._operations import build_paging_first_response_empty_request, build_paging_get_multiple_pages_failure_request, build_paging_get_multiple_pages_failure_uri_request, build_paging_get_multiple_pages_fragment_next_link_request, build_paging_get_multiple_pages_fragment_with_grouping_next_link_request, build_paging_get_multiple_pages_lro_request_initial, build_paging_get_multiple_pages_request, build_paging_get_multiple_pages_retry_first_request, build_paging_get_multiple_pages_retry_second_request, build_paging_get_multiple_pages_with_offset_request, build_paging_get_no_item_name_pages_request, build_paging_get_null_next_link_name_pages_request, build_paging_get_odata_multiple_pages_request, build_paging_get_paging_model_with_item_name_with_xms_client_name_request, build_paging_get_single_pages_failure_request, build_paging_get_single_pages_request, build_paging_get_with_query_params_request, build_paging_next_fragment_request, build_paging_next_fragment_with_grouping_request, build_paging_next_operation_with_query_params_request

T = TypeVar('T')
ClsType = Optional[Callable[[PipelineResponse[HttpRequest, AsyncHttpResponse], T, Dict[str, Any]], Any]]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
if TYPE_CHECKING:
# pylint: disable=unused-import,ungrouped-imports
from typing import Any, Callable, Dict, Generic, Iterable, Optional, TypeVar, Union

T = TypeVar('T')
ClsType = Optional[Callable[[PipelineResponse[HttpRequest, HttpResponse], T, Dict[str, Any]], Any]]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
from ... import models as _models
from ..._vendor import _convert_request
from ...operations._operation_group_one_operations import build_test_two_request

T = TypeVar('T')
ClsType = Optional[Callable[[PipelineResponse[HttpRequest, AsyncHttpResponse], T, Dict[str, Any]], Any]]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
if TYPE_CHECKING:
# pylint: disable=unused-import,ungrouped-imports
from typing import Any, Callable, Dict, Generic, Optional, TypeVar

T = TypeVar('T')
ClsType = Optional[Callable[[PipelineResponse[HttpRequest, HttpResponse], T, Dict[str, Any]], Any]]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
from ... import models as _models
from ..._vendor import _convert_request
from ...operations._multiapi_service_client_operations import build_test_different_calls_request, build_test_lro_and_paging_request_initial, build_test_lro_request_initial, build_test_one_request

T = TypeVar('T')
ClsType = Optional[Callable[[PipelineResponse[HttpRequest, AsyncHttpResponse], T, Dict[str, Any]], Any]]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
from ... import models as _models
from ..._vendor import _convert_request
from ...operations._operation_group_one_operations import build_test_two_request

T = TypeVar('T')
ClsType = Optional[Callable[[PipelineResponse[HttpRequest, AsyncHttpResponse], T, Dict[str, Any]], Any]]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
if TYPE_CHECKING:
# pylint: disable=unused-import,ungrouped-imports
from typing import Any, Callable, Dict, Generic, Iterable, Optional, TypeVar, Union

T = TypeVar('T')
ClsType = Optional[Callable[[PipelineResponse[HttpRequest, HttpResponse], T, Dict[str, Any]], Any]]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
if TYPE_CHECKING:
# pylint: disable=unused-import,ungrouped-imports
from typing import Any, Callable, Dict, Generic, Optional, TypeVar

T = TypeVar('T')
ClsType = Optional[Callable[[PipelineResponse[HttpRequest, HttpResponse], T, Dict[str, Any]], Any]]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
from ... import models as _models
from ..._vendor import _convert_request
from ...operations._multiapi_service_client_operations import build_test_different_calls_request, build_test_one_request

T = TypeVar('T')
ClsType = Optional[Callable[[PipelineResponse[HttpRequest, AsyncHttpResponse], T, Dict[str, Any]], Any]]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
from ... import models as _models
from ..._vendor import _convert_request
from ...operations._operation_group_one_operations import build_test_three_request, build_test_two_request

T = TypeVar('T')
ClsType = Optional[Callable[[PipelineResponse[HttpRequest, AsyncHttpResponse], T, Dict[str, Any]], Any]]

Expand Down
Loading