Skip to content

Commit

Permalink
Fix internal model typing (#2517)
Browse files Browse the repository at this point in the history
* fix internal model typing

* optimize docstring for internal model

* update

* fix msrest

* add changelog

* add changelog

* review

* update

* review
  • Loading branch information
msyyc committed Apr 28, 2024
1 parent afc98f0 commit 1d708d1
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
changeKind: fix
packages:
- "@autorest/python"
- "@azure-tools/typespec-python"
---

Fix typing annotation for internal model #2517
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,9 @@ def type_annotation(self, **kwargs: Any) -> str:
:rtype: str
"""
if self.code_model.options["models_mode"]:
model_name = f"_models.{self.name}"
module_name = "_models." if kwargs.get("need_module_name", True) else ""
file_name = f"{self.code_model.enums_filename}." if self.internal else ""
model_name = module_name + file_name + self.name
# we don't need quoted annotation in operation files, and need it in model folder files.
if not kwargs.get("is_operation_file", False):
model_name = f'"{model_name}"'
Expand Down
25 changes: 16 additions & 9 deletions packages/autorest.python/autorest/codegen/models/model_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
from collections import OrderedDict
from typing import Any, Dict, List, Optional, TYPE_CHECKING, cast
import sys
from autorest.codegen.models.utils import add_to_pylint_disable, NAME_LENGTH_LIMIT
from autorest.codegen.models.utils import (
add_to_pylint_disable,
NAME_LENGTH_LIMIT,
)
from .base import BaseType
from .constant_type import ConstantType
from .property import Property
Expand Down Expand Up @@ -312,13 +315,14 @@ def imports(self, **kwargs: Any) -> FileImport:
class GeneratedModelType(ModelType): # pylint: disable=abstract-method
def type_annotation(self, **kwargs: Any) -> str:
is_operation_file = kwargs.pop("is_operation_file", False)
retval = f"_models.{self.name}"
if self.internal:
retval = f"{self.code_model.models_filename}.{retval}"
return retval if is_operation_file else f'"{retval}"'
skip_quote = kwargs.get("skip_quote", False)
module_name = "_models." if kwargs.get("need_module_name", True) else ""
file_name = f"{self.code_model.models_filename}." if self.internal else ""
retval = module_name + file_name + self.name
return retval if is_operation_file or skip_quote else f'"{retval}"'

def docstring_type(self, **kwargs: Any) -> str:
return f"~{self.code_model.namespace}.models.{self.name}"
return f"~{self.code_model.namespace}.models.{self.type_annotation(need_module_name=False, skip_quote=True)}"

def docstring_text(self, **kwargs: Any) -> str:
return self.name
Expand Down Expand Up @@ -362,8 +366,7 @@ class MsrestModelType(GeneratedModelType):

@property
def serialization_type(self) -> str:
private_model_path = f"_models.{self.code_model.models_filename}."
return f"{private_model_path if self.internal else ''}{self.name}"
return self.type_annotation(skip_quote=True) if self.internal else self.name

@property
def instance_check_template(self) -> str:
Expand All @@ -382,7 +385,11 @@ class DPGModelType(GeneratedModelType):

@property
def serialization_type(self) -> str:
return f"{'_models.' if self.internal else ''}_models.{self.name}"
return (
self.type_annotation(skip_quote=True)
if self.internal
else self.type_annotation(need_module_name=False, skip_quote=True)
)

@property
def instance_check_template(self) -> str:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ def default_error_deserialization(self) -> Optional[str]:
return None
excep_schema = default_exceptions[0].type
if isinstance(excep_schema, ModelType):
return f"_models.{excep_schema.name}"
return excep_schema.type_annotation(skip_quote=True)
# in this case, it's just an AnyType
return "'object'"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ async def _no_decorator_in_internal( # pylint: disable=protected-access
:paramtype name: str
:return: NoDecoratorModelInInternal. The NoDecoratorModelInInternal is compatible with
MutableMapping
:rtype: ~specs.azure.clientgenerator.core.access.models.NoDecoratorModelInInternal
:rtype: ~specs.azure.clientgenerator.core.access.models._models.NoDecoratorModelInInternal
:raises ~azure.core.exceptions.HttpResponseError:
Example:
Expand Down Expand Up @@ -280,7 +280,8 @@ async def _internal_decorator_in_internal( # pylint: disable=protected-access
:paramtype name: str
:return: InternalDecoratorModelInInternal. The InternalDecoratorModelInInternal is compatible
with MutableMapping
:rtype: ~specs.azure.clientgenerator.core.access.models.InternalDecoratorModelInInternal
:rtype:
~specs.azure.clientgenerator.core.access.models._models.InternalDecoratorModelInInternal
:raises ~azure.core.exceptions.HttpResponseError:
Example:
Expand Down Expand Up @@ -580,7 +581,7 @@ async def _operation( # pylint: disable=protected-access
:keyword name: Required.
:paramtype name: str
:return: OuterModel. The OuterModel is compatible with MutableMapping
:rtype: ~specs.azure.clientgenerator.core.access.models.OuterModel
:rtype: ~specs.azure.clientgenerator.core.access.models._models.OuterModel
:raises ~azure.core.exceptions.HttpResponseError:
Example:
Expand Down Expand Up @@ -654,7 +655,7 @@ async def _discriminator( # pylint: disable=protected-access
:keyword kind: Required.
:paramtype kind: str
:return: AbstractModel. The AbstractModel is compatible with MutableMapping
:rtype: ~specs.azure.clientgenerator.core.access.models.AbstractModel
:rtype: ~specs.azure.clientgenerator.core.access.models._models.AbstractModel
:raises ~azure.core.exceptions.HttpResponseError:
Example:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ class OuterModel(BaseModel):
:ivar name: Required.
:vartype name: str
:ivar inner: Required.
:vartype inner: ~specs.azure.clientgenerator.core.access.models.InnerModel
:vartype inner: ~specs.azure.clientgenerator.core.access.models._models.InnerModel
"""

inner: "_models._models.InnerModel" = rest_field()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ def _no_decorator_in_internal( # pylint: disable=protected-access
:paramtype name: str
:return: NoDecoratorModelInInternal. The NoDecoratorModelInInternal is compatible with
MutableMapping
:rtype: ~specs.azure.clientgenerator.core.access.models.NoDecoratorModelInInternal
:rtype: ~specs.azure.clientgenerator.core.access.models._models.NoDecoratorModelInInternal
:raises ~azure.core.exceptions.HttpResponseError:
Example:
Expand Down Expand Up @@ -454,7 +454,8 @@ def _internal_decorator_in_internal( # pylint: disable=protected-access
:paramtype name: str
:return: InternalDecoratorModelInInternal. The InternalDecoratorModelInInternal is compatible
with MutableMapping
:rtype: ~specs.azure.clientgenerator.core.access.models.InternalDecoratorModelInInternal
:rtype:
~specs.azure.clientgenerator.core.access.models._models.InternalDecoratorModelInInternal
:raises ~azure.core.exceptions.HttpResponseError:
Example:
Expand Down Expand Up @@ -750,7 +751,7 @@ def _operation(self, *, name: str, **kwargs: Any) -> _models._models.OuterModel:
:keyword name: Required.
:paramtype name: str
:return: OuterModel. The OuterModel is compatible with MutableMapping
:rtype: ~specs.azure.clientgenerator.core.access.models.OuterModel
:rtype: ~specs.azure.clientgenerator.core.access.models._models.OuterModel
:raises ~azure.core.exceptions.HttpResponseError:
Example:
Expand Down Expand Up @@ -824,7 +825,7 @@ def _discriminator( # pylint: disable=protected-access
:keyword kind: Required.
:paramtype kind: str
:return: AbstractModel. The AbstractModel is compatible with MutableMapping
:rtype: ~specs.azure.clientgenerator.core.access.models.AbstractModel
:rtype: ~specs.azure.clientgenerator.core.access.models._models.AbstractModel
:raises ~azure.core.exceptions.HttpResponseError:
Example:
Expand Down

0 comments on commit 1d708d1

Please sign in to comment.