Skip to content

Commit

Permalink
Adopt whole common types in TCGC (#2476)
Browse files Browse the repository at this point in the history
  • Loading branch information
tadelesh authored Apr 8, 2024
1 parent b175a88 commit e73ec3f
Show file tree
Hide file tree
Showing 1,025 changed files with 21,128 additions and 15,386 deletions.
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"cwd": "${workspaceFolder}/packages/typespec-python",
"args": [
"compile",
"${workspaceFolder}/packages/typespec-python/main.tsp",
"${workspaceFolder}/packages/typespec-python/node_modules/@azure-tools/cadl-ranch-specs/http/server/versions/not-versioned",
"--emit",
"${workspaceFolder}/packages/typespec-python/dist/src/index.js",
"--option=@azure-tools/typespec-python.debug=true"
Expand Down
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@
},
"homepage": "https://github.com/Azure/autorest.python#readme",
"devDependencies": {
"@azure-tools/cadl-ranch": "~0.11.3",
"@typespec/prettier-plugin-typespec": "~0.51.0",
"@azure-tools/cadl-ranch": "~0.11.2",
"autorest": "3.6.3",
"eslint": "^8.44.0",
"eslint-plugin-import": "^2.27.5",
"eslint-plugin-unicorn": "^46.0.0",
"eslint": "^8.57.0",
"eslint-plugin-import": "^2.29.1",
"eslint-plugin-unicorn": "^46.0.1",
"prettier": "^2.8.8",
"typescript": "~5.1.3",
"syncpack": "^9.8.6"
"syncpack": "^9.8.6",
"typescript": "~5.1.6"
},
"syncpack": {
"dependencyTypes": [
Expand Down
16 changes: 16 additions & 0 deletions packages/autorest.python/ChangeLog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
# Release

## 2024-04-05 - 6.13.9

| Library | Min Version |
| ----------------------------------------------------------------------- | ----------- |
| `@autorest/core` | `3.9.2` |
| `@autorest/modelerfour` | `4.24.3` |
| `azure-core` dep of generated code | `1.30.0` |
| `isodate` dep of generated code | `0.6.1` |
| `msrest` dep of generated code (If generating legacy code) | `0.7.1` |
| `azure-mgmt-core` dep of generated code (If generating mgmt plane code) | `1.3.2` |
| `typing-extensions` dep of generated code (If generating with constants)| `4.0.1` |

**Other Changes**

- Refactor code to use the type ecosystem from "@azure-tools/typespec-client-generator-core" #2476

## 2024-03-22 - 6.13.8

| Library | Min Version |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# license information.
# --------------------------------------------------------------------------
import logging
from typing import Dict, Any, Optional, TYPE_CHECKING
from typing import Dict, Any, Optional, TYPE_CHECKING, Union
from .base import BaseType
from .imports import FileImport, ImportType, TypingSection
from .primitive_types import IntegerType, BinaryType, StringType, BooleanType
Expand All @@ -31,7 +31,7 @@ def __init__(
yaml_data: Dict[str, Any],
code_model: "CodeModel",
value_type: BaseType,
value: Optional[str],
value: Optional[Union[str, int, float]],
) -> None:
super().__init__(yaml_data=yaml_data, code_model=code_model)
self.value_type = value_type
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,10 @@ def from_yaml(
"""
from . import build_type

enum_type = cast(EnumType, code_model.lookup_type(id(yaml_data["enumType"])))
return cls(
yaml_data=yaml_data,
code_model=code_model,
enum_type=enum_type,
enum_type=cast(EnumType, build_type(yaml_data["enumType"], code_model)),
value_type=build_type(yaml_data["valueType"], code_model),
)

Expand Down
16 changes: 16 additions & 0 deletions packages/autorest.python/autorest/codegen/models/operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,18 @@ def imports( # pylint: disable=too-many-branches, disable=too-many-statements
file_import.add_submodule_import(
"azure.mgmt.core.exceptions", "ARMErrorFormat", ImportType.SDKCORE
)
file_import.add_submodule_import(
"typing",
"Type",
ImportType.STDLIB,
)
file_import.add_mutable_mapping_import()
if self.non_default_error_status_codes:
file_import.add_submodule_import(
"typing",
"cast",
ImportType.STDLIB,
)

if self.has_kwargs_to_pop_with_default(
self.parameters.kwargs_to_pop, ParameterLocation.HEADER # type: ignore
Expand Down Expand Up @@ -481,6 +493,10 @@ def imports( # pylint: disable=too-many-branches, disable=too-many-statements
)
if self.overloads:
file_import.add_submodule_import("typing", "overload", ImportType.STDLIB)
if self.non_default_errors and self.code_model.options["models_mode"] == "dpg":
file_import.add_submodule_import(
f"{relative_path}_model_base", "_deserialize", ImportType.LOCAL
)
return file_import

def get_response_from_status(
Expand Down
4 changes: 2 additions & 2 deletions packages/autorest.python/autorest/codegen/models/parameter.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def __init__(
type: BaseType,
) -> None:
super().__init__(yaml_data, code_model)
self.wire_name: str = yaml_data["wireName"]
self.wire_name: str = yaml_data.get("wireName", "")
self.client_name: str = self.yaml_data["clientName"]
self.optional: bool = self.yaml_data["optional"]
self.location: ParameterLocation = self.yaml_data["location"]
Expand Down Expand Up @@ -373,7 +373,7 @@ def method_location( # pylint: disable=too-many-return-statements
return ParameterMethodLocation.KEYWORD_ONLY
if self.grouper:
return ParameterMethodLocation.POSITIONAL
if self.constant:
if self.constant and self.wire_name != "Content-Type":
return ParameterMethodLocation.KWARG
if self.is_content_type:
if self.in_overload:
Expand Down
4 changes: 3 additions & 1 deletion packages/autorest.python/autorest/codegen/models/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,12 @@ def serialization_type(self) -> str:
def from_yaml(
cls, yaml_data: Dict[str, Any], code_model: "CodeModel"
) -> "ResponseHeader":
from . import build_type

return cls(
yaml_data=yaml_data,
code_model=code_model,
type=code_model.lookup_type(id(yaml_data["type"])),
type=build_type(yaml_data["type"], code_model),
)


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1281,7 +1281,7 @@ def handle_response(self, builder: OperationType) -> List[str]:
return retval

def error_map(self, builder: OperationType) -> List[str]:
retval = ["error_map = {"]
retval = ["error_map: MutableMapping[int, Type[HttpResponseError]] = {"]
if builder.non_default_errors:
if not 401 in builder.non_default_error_status_codes:
retval.append(" 401: ClientAuthenticationError,")
Expand Down Expand Up @@ -1309,30 +1309,35 @@ def error_map(self, builder: OperationType) -> List[str]:
for status_code in excep.status_codes:
if status_code == 401:
retval.append(
" 401: lambda response: ClientAuthenticationError(response=response"
f"{error_model_str}{error_format_str}),"
" 401: cast(Type[HttpResponseError], "
"lambda response: ClientAuthenticationError(response=response"
f"{error_model_str}{error_format_str})),"
)
elif status_code == 404:
retval.append(
" 404: lambda response: ResourceNotFoundError(response=response"
f"{error_model_str}{error_format_str}),"
" 404: cast(Type[HttpResponseError], "
"lambda response: ResourceNotFoundError(response=response"
f"{error_model_str}{error_format_str})),"
)
elif status_code == 409:
retval.append(
" 409: lambda response: ResourceExistsError(response=response"
f"{error_model_str}{error_format_str}),"
" 409: cast(Type[HttpResponseError], "
"lambda response: ResourceExistsError(response=response"
f"{error_model_str}{error_format_str})),"
)
elif status_code == 304:
retval.append(
" 304: lambda response: ResourceNotModifiedError(response=response"
f"{error_model_str}{error_format_str}),"
" 304: cast(Type[HttpResponseError], "
"lambda response: ResourceNotModifiedError(response=response"
f"{error_model_str}{error_format_str})),"
)
elif not error_model_str and not error_format_str:
retval.append(f" {status_code}: HttpResponseError,")
else:
retval.append(
f" {status_code}: lambda response: HttpResponseError(response=response"
f"{error_model_str}{error_format_str}),"
f" {status_code}: cast(Type[HttpResponseError], "
"lambda response: HttpResponseError(response=response"
f"{error_model_str}{error_format_str})),"
)
else:
retval.append(
Expand Down
26 changes: 17 additions & 9 deletions packages/autorest.python/autorest/preprocess/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,18 @@ def update_overload_section(
yaml_data: Dict[str, Any],
section: str,
):
for overload_s, original_s in zip(overload[section], yaml_data[section]):
if overload_s.get("type"):
overload_s["type"] = original_s["type"]
if overload_s.get("headers"):
for overload_h, original_h in zip(
overload_s["headers"], original_s["headers"]
):
if overload_h.get("type"):
overload_h["type"] = original_h["type"]
try:
for overload_s, original_s in zip(overload[section], yaml_data[section]):
if overload_s.get("type"):
overload_s["type"] = original_s["type"]
if overload_s.get("headers"):
for overload_h, original_h in zip(
overload_s["headers"], original_s["headers"]
):
if overload_h.get("type"):
overload_h["type"] = original_h["type"]
except KeyError as exc:
raise ValueError(overload["name"]) from exc


def add_overload(
Expand Down Expand Up @@ -390,6 +393,11 @@ def update_parameter(self, yaml_data: Dict[str, Any]) -> None:
and wire_name_lower in HEADERS_CONVERT_IN_METHOD
):
headers_convert(yaml_data, HEADERS_CONVERT_IN_METHOD[wire_name_lower])
if (
wire_name_lower in ["$host", "content-type", "accept"]
and yaml_data["type"]["type"] == "constant"
):
yaml_data["clientDefaultValue"] = yaml_data["type"]["value"]

def update_operation(
self,
Expand Down
2 changes: 1 addition & 1 deletion packages/autorest.python/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@autorest/python",
"version": "6.13.8",
"version": "6.13.9",
"description": "The Python extension for generators in AutoRest.",
"scripts": {
"prepare": "node run-python3.js prepare.py",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
# Code generated by Microsoft (R) AutoRest Code Generator.
# Changes may cause incorrect behavior and will be lost if the code is regenerated.
# --------------------------------------------------------------------------
from typing import Any, Callable, Dict, Optional, TypeVar
import sys
from typing import Any, Callable, Dict, Optional, Type, TypeVar

from azure.core.exceptions import (
ClientAuthenticationError,
Expand All @@ -26,6 +27,10 @@
build_http_success_head404_request,
)

if sys.version_info >= (3, 9):
from collections.abc import MutableMapping
else:
from typing import MutableMapping # type: ignore # pylint: disable=ungrouped-imports
T = TypeVar("T")
ClsType = Optional[Callable[[PipelineResponse[HttpRequest, AsyncHttpResponse], T, Dict[str, Any]], Any]]

Expand Down Expand Up @@ -55,7 +60,7 @@ async def head200(self, **kwargs: Any) -> None: # pylint: disable=inconsistent-
:rtype: None
:raises ~azure.core.exceptions.HttpResponseError:
"""
error_map = {
error_map: MutableMapping[int, Type[HttpResponseError]] = {
401: ClientAuthenticationError,
404: ResourceNotFoundError,
409: ResourceExistsError,
Expand Down Expand Up @@ -98,7 +103,7 @@ async def head204(self, **kwargs: Any) -> None: # pylint: disable=inconsistent-
:rtype: None
:raises ~azure.core.exceptions.HttpResponseError:
"""
error_map = {
error_map: MutableMapping[int, Type[HttpResponseError]] = {
401: ClientAuthenticationError,
404: ResourceNotFoundError,
409: ResourceExistsError,
Expand Down Expand Up @@ -141,7 +146,7 @@ async def head404(self, **kwargs: Any) -> None: # pylint: disable=inconsistent-
:rtype: None
:raises ~azure.core.exceptions.HttpResponseError:
"""
error_map = {
error_map: MutableMapping[int, Type[HttpResponseError]] = {
401: ClientAuthenticationError,
404: ResourceNotFoundError,
409: ResourceExistsError,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
# Code generated by Microsoft (R) AutoRest Code Generator.
# Changes may cause incorrect behavior and will be lost if the code is regenerated.
# --------------------------------------------------------------------------
from typing import Any, Callable, Dict, Optional, TypeVar
import sys
from typing import Any, Callable, Dict, Optional, Type, TypeVar

from azure.core.exceptions import (
ClientAuthenticationError,
Expand All @@ -22,6 +23,10 @@

from .._serialization import Serializer

if sys.version_info >= (3, 9):
from collections.abc import MutableMapping
else:
from typing import MutableMapping # type: ignore # pylint: disable=ungrouped-imports
T = TypeVar("T")
ClsType = Optional[Callable[[PipelineResponse[HttpRequest, HttpResponse], T, Dict[str, Any]], Any]]

Expand Down Expand Up @@ -75,7 +80,7 @@ def head200(self, **kwargs: Any) -> None: # pylint: disable=inconsistent-return
:rtype: None
:raises ~azure.core.exceptions.HttpResponseError:
"""
error_map = {
error_map: MutableMapping[int, Type[HttpResponseError]] = {
401: ClientAuthenticationError,
404: ResourceNotFoundError,
409: ResourceExistsError,
Expand Down Expand Up @@ -118,7 +123,7 @@ def head204(self, **kwargs: Any) -> None: # pylint: disable=inconsistent-return
:rtype: None
:raises ~azure.core.exceptions.HttpResponseError:
"""
error_map = {
error_map: MutableMapping[int, Type[HttpResponseError]] = {
401: ClientAuthenticationError,
404: ResourceNotFoundError,
409: ResourceExistsError,
Expand Down Expand Up @@ -161,7 +166,7 @@ def head404(self, **kwargs: Any) -> None: # pylint: disable=inconsistent-return
:rtype: None
:raises ~azure.core.exceptions.HttpResponseError:
"""
error_map = {
error_map: MutableMapping[int, Type[HttpResponseError]] = {
401: ClientAuthenticationError,
404: ResourceNotFoundError,
409: ResourceExistsError,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
# Code generated by Microsoft (R) AutoRest Code Generator.
# Changes may cause incorrect behavior and will be lost if the code is regenerated.
# --------------------------------------------------------------------------
from typing import Any, Callable, Dict, Optional, TypeVar
import sys
from typing import Any, Callable, Dict, Optional, Type, TypeVar

from azure.core.exceptions import (
ClientAuthenticationError,
Expand All @@ -26,6 +27,10 @@
build_http_success_head404_request,
)

if sys.version_info >= (3, 9):
from collections.abc import MutableMapping
else:
from typing import MutableMapping # type: ignore # pylint: disable=ungrouped-imports
T = TypeVar("T")
ClsType = Optional[Callable[[PipelineResponse[HttpRequest, AsyncHttpResponse], T, Dict[str, Any]], Any]]

Expand Down Expand Up @@ -55,7 +60,7 @@ async def head200(self, **kwargs: Any) -> None: # pylint: disable=inconsistent-
:rtype: None
:raises ~azure.core.exceptions.HttpResponseError:
"""
error_map = {
error_map: MutableMapping[int, Type[HttpResponseError]] = {
401: ClientAuthenticationError,
404: ResourceNotFoundError,
409: ResourceExistsError,
Expand Down Expand Up @@ -98,7 +103,7 @@ async def head204(self, **kwargs: Any) -> None: # pylint: disable=inconsistent-
:rtype: None
:raises ~azure.core.exceptions.HttpResponseError:
"""
error_map = {
error_map: MutableMapping[int, Type[HttpResponseError]] = {
401: ClientAuthenticationError,
404: ResourceNotFoundError,
409: ResourceExistsError,
Expand Down Expand Up @@ -141,7 +146,7 @@ async def head404(self, **kwargs: Any) -> None: # pylint: disable=inconsistent-
:rtype: None
:raises ~azure.core.exceptions.HttpResponseError:
"""
error_map = {
error_map: MutableMapping[int, Type[HttpResponseError]] = {
401: ClientAuthenticationError,
404: ResourceNotFoundError,
409: ResourceExistsError,
Expand Down
Loading

0 comments on commit e73ec3f

Please sign in to comment.