From fd59de539c1cac4b2c10aac5b57db09e7ff94dd1 Mon Sep 17 00:00:00 2001 From: msyyc <70930885+msyyc@users.noreply.github.com> Date: Mon, 15 Apr 2024 14:46:59 +0800 Subject: [PATCH 1/9] fix internal model typing --- .../autorest/codegen/models/enum_type.py | 7 ++++- .../autorest/codegen/models/model_type.py | 28 +++++++++---------- .../autorest/codegen/models/operation.py | 2 +- .../autorest/codegen/models/utils.py | 4 +++ 4 files changed, 25 insertions(+), 16 deletions(-) diff --git a/packages/autorest.python/autorest/codegen/models/enum_type.py b/packages/autorest.python/autorest/codegen/models/enum_type.py index d5fa5505884..7c4bb2173c0 100644 --- a/packages/autorest.python/autorest/codegen/models/enum_type.py +++ b/packages/autorest.python/autorest/codegen/models/enum_type.py @@ -7,6 +7,7 @@ from .base import BaseType from .imports import FileImport, ImportType, TypingSection +from .utils import typing_name if TYPE_CHECKING: from .code_model import CodeModel @@ -168,6 +169,10 @@ def description( enum_description = f"Known values are: {possible_values_str}." return enum_description + @property + def typing_name(self) -> str: + return f"_models.{typing_name(self.code_model.enums_filename, self.internal)}{self.name}" + def type_annotation(self, **kwargs: Any) -> str: """The python type used for type annotation @@ -175,7 +180,7 @@ def type_annotation(self, **kwargs: Any) -> str: :rtype: str """ if self.code_model.options["models_mode"]: - model_name = f"_models.{self.name}" + model_name = self.typing_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}"' diff --git a/packages/autorest.python/autorest/codegen/models/model_type.py b/packages/autorest.python/autorest/codegen/models/model_type.py index bbcac0db202..4850b789166 100644 --- a/packages/autorest.python/autorest/codegen/models/model_type.py +++ b/packages/autorest.python/autorest/codegen/models/model_type.py @@ -6,7 +6,11 @@ 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, + typing_name, +) from .base import BaseType from .constant_type import ConstantType from .property import Property @@ -79,6 +83,10 @@ def __init__( "crossLanguageDefinitionId" ) + @property + def typing_name(self) -> str: + return f"_models.{typing_name(self.code_model.models_filename, self.internal)}{self.name}" + @property def flattened_property(self) -> Optional[Property]: try: @@ -308,10 +316,7 @@ 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}"' + return self.typing_name if is_operation_file else f'"{self.typing_name}"' def docstring_type(self, **kwargs: Any) -> str: return f"~{self.code_model.namespace}.models.{self.name}" @@ -348,15 +353,14 @@ def imports(self, **kwargs: Any) -> FileImport: ) return file_import + @property + def serialization_type(self) -> str: + return self.typing_name + class MsrestModelType(GeneratedModelType): base = "msrest" - @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}" - @property def instance_check_template(self) -> str: return "isinstance({}, msrest.Model)" @@ -372,10 +376,6 @@ def imports(self, **kwargs: Any) -> FileImport: class DPGModelType(GeneratedModelType): base = "dpg" - @property - def serialization_type(self) -> str: - return f"{'_models.' if self.internal else ''}_models.{self.name}" - @property def instance_check_template(self) -> str: return "isinstance({}, _model_base.Model)" diff --git a/packages/autorest.python/autorest/codegen/models/operation.py b/packages/autorest.python/autorest/codegen/models/operation.py index ea472d87cb9..e1e5471e251 100644 --- a/packages/autorest.python/autorest/codegen/models/operation.py +++ b/packages/autorest.python/autorest/codegen/models/operation.py @@ -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.typing_name # in this case, it's just an AnyType return "'object'" diff --git a/packages/autorest.python/autorest/codegen/models/utils.py b/packages/autorest.python/autorest/codegen/models/utils.py index 374d659135a..ef6f8ac815e 100644 --- a/packages/autorest.python/autorest/codegen/models/utils.py +++ b/packages/autorest.python/autorest/codegen/models/utils.py @@ -21,3 +21,7 @@ def add_to_pylint_disable(curr_str: str, entry: str) -> str: if curr_str: return f"{curr_str},{entry}" return f" # pylint: disable={entry}" + + +def typing_name(file_name: str, internal: bool) -> str: + return f"{file_name}." if internal else "" From 525f402c0c7734bb88689421d5391c32b8dfbc41 Mon Sep 17 00:00:00 2001 From: msyyc <70930885+msyyc@users.noreply.github.com> Date: Mon, 15 Apr 2024 15:10:51 +0800 Subject: [PATCH 2/9] optimize docstring for internal model --- .../autorest/codegen/models/enum_type.py | 8 ++++---- .../autorest/codegen/models/model_type.py | 12 ++++++------ .../autorest/codegen/models/operation.py | 2 +- .../core/access/aio/operations/_operations.py | 9 +++++---- .../clientgenerator/core/access/models/_models.py | 2 +- .../core/access/operations/_operations.py | 9 +++++---- 6 files changed, 22 insertions(+), 20 deletions(-) diff --git a/packages/autorest.python/autorest/codegen/models/enum_type.py b/packages/autorest.python/autorest/codegen/models/enum_type.py index 7c4bb2173c0..a7a6074584a 100644 --- a/packages/autorest.python/autorest/codegen/models/enum_type.py +++ b/packages/autorest.python/autorest/codegen/models/enum_type.py @@ -169,9 +169,9 @@ def description( enum_description = f"Known values are: {possible_values_str}." return enum_description - @property - def typing_name(self) -> str: - return f"_models.{typing_name(self.code_model.enums_filename, self.internal)}{self.name}" + def typing_name(self, need_module_name: bool = True) -> str: + module = "_models." if need_module_name else "" + return f"{module}{typing_name(self.code_model.enums_filename, self.internal)}{self.name}" def type_annotation(self, **kwargs: Any) -> str: """The python type used for type annotation @@ -180,7 +180,7 @@ def type_annotation(self, **kwargs: Any) -> str: :rtype: str """ if self.code_model.options["models_mode"]: - model_name = self.typing_name + model_name = self.typing_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}"' diff --git a/packages/autorest.python/autorest/codegen/models/model_type.py b/packages/autorest.python/autorest/codegen/models/model_type.py index 4850b789166..07f4747c4cd 100644 --- a/packages/autorest.python/autorest/codegen/models/model_type.py +++ b/packages/autorest.python/autorest/codegen/models/model_type.py @@ -83,9 +83,9 @@ def __init__( "crossLanguageDefinitionId" ) - @property - def typing_name(self) -> str: - return f"_models.{typing_name(self.code_model.models_filename, self.internal)}{self.name}" + def typing_name(self, need_module_name: bool = True) -> str: + module = "_models." if need_module_name else "" + return f"{module}{typing_name(self.code_model.models_filename, self.internal)}{self.name}" @property def flattened_property(self) -> Optional[Property]: @@ -316,10 +316,10 @@ 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) - return self.typing_name if is_operation_file else f'"{self.typing_name}"' + return self.typing_name() if is_operation_file else f'"{self.typing_name()}"' def docstring_type(self, **kwargs: Any) -> str: - return f"~{self.code_model.namespace}.models.{self.name}" + return f"~{self.code_model.namespace}.models.{self.typing_name(False)}" def docstring_text(self, **kwargs: Any) -> str: return self.name @@ -355,7 +355,7 @@ def imports(self, **kwargs: Any) -> FileImport: @property def serialization_type(self) -> str: - return self.typing_name + return self.typing_name() class MsrestModelType(GeneratedModelType): diff --git a/packages/autorest.python/autorest/codegen/models/operation.py b/packages/autorest.python/autorest/codegen/models/operation.py index e1e5471e251..93a8b51b4c8 100644 --- a/packages/autorest.python/autorest/codegen/models/operation.py +++ b/packages/autorest.python/autorest/codegen/models/operation.py @@ -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 excep_schema.typing_name + return excep_schema.typing_name() # in this case, it's just an AnyType return "'object'" diff --git a/packages/typespec-python/test/azure/generated/azure-client-generator-core-access/specs/azure/clientgenerator/core/access/aio/operations/_operations.py b/packages/typespec-python/test/azure/generated/azure-client-generator-core-access/specs/azure/clientgenerator/core/access/aio/operations/_operations.py index c3c33031467..5a096a9c735 100644 --- a/packages/typespec-python/test/azure/generated/azure-client-generator-core-access/specs/azure/clientgenerator/core/access/aio/operations/_operations.py +++ b/packages/typespec-python/test/azure/generated/azure-client-generator-core-access/specs/azure/clientgenerator/core/access/aio/operations/_operations.py @@ -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: @@ -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: @@ -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: @@ -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: diff --git a/packages/typespec-python/test/azure/generated/azure-client-generator-core-access/specs/azure/clientgenerator/core/access/models/_models.py b/packages/typespec-python/test/azure/generated/azure-client-generator-core-access/specs/azure/clientgenerator/core/access/models/_models.py index a12e11d11fd..7e3544d9e5c 100644 --- a/packages/typespec-python/test/azure/generated/azure-client-generator-core-access/specs/azure/clientgenerator/core/access/models/_models.py +++ b/packages/typespec-python/test/azure/generated/azure-client-generator-core-access/specs/azure/clientgenerator/core/access/models/_models.py @@ -129,7 +129,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() diff --git a/packages/typespec-python/test/azure/generated/azure-client-generator-core-access/specs/azure/clientgenerator/core/access/operations/_operations.py b/packages/typespec-python/test/azure/generated/azure-client-generator-core-access/specs/azure/clientgenerator/core/access/operations/_operations.py index 21b97d101d7..0fd32334f2c 100644 --- a/packages/typespec-python/test/azure/generated/azure-client-generator-core-access/specs/azure/clientgenerator/core/access/operations/_operations.py +++ b/packages/typespec-python/test/azure/generated/azure-client-generator-core-access/specs/azure/clientgenerator/core/access/operations/_operations.py @@ -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: @@ -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: @@ -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: @@ -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: From 34c5f004c04eaff5f19c5dacabd1e541b7b2fd02 Mon Sep 17 00:00:00 2001 From: msyyc <70930885+msyyc@users.noreply.github.com> Date: Mon, 15 Apr 2024 15:28:15 +0800 Subject: [PATCH 3/9] update --- .../autorest.python/autorest/codegen/models/enum_type.py | 8 ++++++-- .../autorest.python/autorest/codegen/models/model_type.py | 8 ++++++-- packages/autorest.python/autorest/codegen/models/utils.py | 7 +++++-- 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/packages/autorest.python/autorest/codegen/models/enum_type.py b/packages/autorest.python/autorest/codegen/models/enum_type.py index a7a6074584a..b7e53a07f2d 100644 --- a/packages/autorest.python/autorest/codegen/models/enum_type.py +++ b/packages/autorest.python/autorest/codegen/models/enum_type.py @@ -170,8 +170,12 @@ def description( return enum_description def typing_name(self, need_module_name: bool = True) -> str: - module = "_models." if need_module_name else "" - return f"{module}{typing_name(self.code_model.enums_filename, self.internal)}{self.name}" + return typing_name( + file_name=self.code_model.enums_filename, + internal=self.internal, + need_module_name=need_module_name, + type_name=self.name, + ) def type_annotation(self, **kwargs: Any) -> str: """The python type used for type annotation diff --git a/packages/autorest.python/autorest/codegen/models/model_type.py b/packages/autorest.python/autorest/codegen/models/model_type.py index 07f4747c4cd..5a9e324a538 100644 --- a/packages/autorest.python/autorest/codegen/models/model_type.py +++ b/packages/autorest.python/autorest/codegen/models/model_type.py @@ -84,8 +84,12 @@ def __init__( ) def typing_name(self, need_module_name: bool = True) -> str: - module = "_models." if need_module_name else "" - return f"{module}{typing_name(self.code_model.models_filename, self.internal)}{self.name}" + return typing_name( + file_name=self.code_model.models_filename, + internal=self.internal, + need_module_name=need_module_name, + type_name=self.name, + ) @property def flattened_property(self) -> Optional[Property]: diff --git a/packages/autorest.python/autorest/codegen/models/utils.py b/packages/autorest.python/autorest/codegen/models/utils.py index ef6f8ac815e..fd134ccfb75 100644 --- a/packages/autorest.python/autorest/codegen/models/utils.py +++ b/packages/autorest.python/autorest/codegen/models/utils.py @@ -23,5 +23,8 @@ def add_to_pylint_disable(curr_str: str, entry: str) -> str: return f" # pylint: disable={entry}" -def typing_name(file_name: str, internal: bool) -> str: - return f"{file_name}." if internal else "" +def typing_name( + file_name: str, internal: bool, need_module_name: bool, type_name: str +) -> str: + module = "_models." if need_module_name else "" + return module + (f"{file_name}." if internal else "") + type_name From 18d81357962990c7202a49164e3a7fd55378d8c6 Mon Sep 17 00:00:00 2001 From: msyyc <70930885+msyyc@users.noreply.github.com> Date: Mon, 15 Apr 2024 16:10:27 +0800 Subject: [PATCH 4/9] fix msrest --- .../autorest/codegen/models/model_type.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/packages/autorest.python/autorest/codegen/models/model_type.py b/packages/autorest.python/autorest/codegen/models/model_type.py index 5a9e324a538..4745fb93f79 100644 --- a/packages/autorest.python/autorest/codegen/models/model_type.py +++ b/packages/autorest.python/autorest/codegen/models/model_type.py @@ -357,14 +357,14 @@ def imports(self, **kwargs: Any) -> FileImport: ) return file_import - @property - def serialization_type(self) -> str: - return self.typing_name() - class MsrestModelType(GeneratedModelType): base = "msrest" + @property + def serialization_type(self) -> str: + return self.typing_name() if self.internal else self.name + @property def instance_check_template(self) -> str: return "isinstance({}, msrest.Model)" @@ -380,6 +380,10 @@ def imports(self, **kwargs: Any) -> FileImport: class DPGModelType(GeneratedModelType): base = "dpg" + @property + def serialization_type(self) -> str: + return self.typing_name() if self.internal else self.typing_name(False) + @property def instance_check_template(self) -> str: return "isinstance({}, _model_base.Model)" From 3c16ac11ed0681aa7898ae2ae813f5bb3014d0b5 Mon Sep 17 00:00:00 2001 From: msyyc <70930885+msyyc@users.noreply.github.com> Date: Tue, 16 Apr 2024 17:54:44 +0800 Subject: [PATCH 5/9] add changelog --- .changeset/sweet-eggs-film.md | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 .changeset/sweet-eggs-film.md diff --git a/.changeset/sweet-eggs-film.md b/.changeset/sweet-eggs-film.md new file mode 100644 index 00000000000..7a1d01363da --- /dev/null +++ b/.changeset/sweet-eggs-film.md @@ -0,0 +1,6 @@ +--- +"@autorest/python": patch +"@azure-tools/typespec-python": patch +--- + +Fix typing annotation for internal model #2517 From 99e737f75b1606ac36c3f730546220715df3893e Mon Sep 17 00:00:00 2001 From: msyyc <70930885+msyyc@users.noreply.github.com> Date: Thu, 18 Apr 2024 13:57:49 +0800 Subject: [PATCH 6/9] add changelog --- .changeset/sweet-eggs-film.md | 6 ------ .../fix-internal-model-typing-2024-3-18-13-57-33.md | 8 ++++++++ 2 files changed, 8 insertions(+), 6 deletions(-) delete mode 100644 .changeset/sweet-eggs-film.md create mode 100644 .chronus/changes/fix-internal-model-typing-2024-3-18-13-57-33.md diff --git a/.changeset/sweet-eggs-film.md b/.changeset/sweet-eggs-film.md deleted file mode 100644 index 7a1d01363da..00000000000 --- a/.changeset/sweet-eggs-film.md +++ /dev/null @@ -1,6 +0,0 @@ ---- -"@autorest/python": patch -"@azure-tools/typespec-python": patch ---- - -Fix typing annotation for internal model #2517 diff --git a/.chronus/changes/fix-internal-model-typing-2024-3-18-13-57-33.md b/.chronus/changes/fix-internal-model-typing-2024-3-18-13-57-33.md new file mode 100644 index 00000000000..285bc521533 --- /dev/null +++ b/.chronus/changes/fix-internal-model-typing-2024-3-18-13-57-33.md @@ -0,0 +1,8 @@ +--- +changeKind: fix +packages: + - "@autorest/python" + - "@azure-tools/typespec-python" +--- + +Fix typing annotation for internal model #2517 \ No newline at end of file From a5f568ad2d37f051769d1f45c0f59d21b84be2fe Mon Sep 17 00:00:00 2001 From: msyyc <70930885+msyyc@users.noreply.github.com> Date: Wed, 24 Apr 2024 15:27:29 +0800 Subject: [PATCH 7/9] review --- .../autorest/codegen/models/enum_type.py | 15 +++++------ .../autorest/codegen/models/model_type.py | 27 ++++++++++--------- .../autorest/codegen/models/operation.py | 2 +- .../autorest/codegen/models/utils.py | 1 + 4 files changed, 23 insertions(+), 22 deletions(-) diff --git a/packages/autorest.python/autorest/codegen/models/enum_type.py b/packages/autorest.python/autorest/codegen/models/enum_type.py index 273ad11efe6..32d68e9380a 100644 --- a/packages/autorest.python/autorest/codegen/models/enum_type.py +++ b/packages/autorest.python/autorest/codegen/models/enum_type.py @@ -169,14 +169,6 @@ def description( enum_description = f"Known values are: {possible_values_str}." return enum_description - def typing_name(self, need_module_name: bool = True) -> str: - return typing_name( - file_name=self.code_model.enums_filename, - internal=self.internal, - need_module_name=need_module_name, - type_name=self.name, - ) - def type_annotation(self, **kwargs: Any) -> str: """The python type used for type annotation @@ -184,7 +176,12 @@ def type_annotation(self, **kwargs: Any) -> str: :rtype: str """ if self.code_model.options["models_mode"]: - model_name = self.typing_name() + model_name = typing_name( + file_name=self.code_model.enums_filename, + internal=self.internal, + need_module_name=kwargs.get("need_module_name", True), + type_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}"' diff --git a/packages/autorest.python/autorest/codegen/models/model_type.py b/packages/autorest.python/autorest/codegen/models/model_type.py index 08772d95600..1e3ef9dd7ae 100644 --- a/packages/autorest.python/autorest/codegen/models/model_type.py +++ b/packages/autorest.python/autorest/codegen/models/model_type.py @@ -83,14 +83,6 @@ def __init__( "crossLanguageDefinitionId" ) - def typing_name(self, need_module_name: bool = True) -> str: - return typing_name( - file_name=self.code_model.models_filename, - internal=self.internal, - need_module_name=need_module_name, - type_name=self.name, - ) - @property def flattened_property(self) -> Optional[Property]: try: @@ -320,10 +312,17 @@ 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) - return self.typing_name() if is_operation_file else f'"{self.typing_name()}"' + in_doc = kwargs.get("in_doc", False) + retval = typing_name( + file_name=self.code_model.models_filename, + internal=self.internal, + need_module_name=kwargs.get("need_module_name", True), + type_name=self.name, + ) + return retval if is_operation_file or in_doc else f'"{retval}"' def docstring_type(self, **kwargs: Any) -> str: - return f"~{self.code_model.namespace}.models.{self.typing_name(False)}" + return f"~{self.code_model.namespace}.models.{self.type_annotation(need_module_name=False, in_doc=True)}" def docstring_text(self, **kwargs: Any) -> str: return self.name @@ -367,7 +366,7 @@ class MsrestModelType(GeneratedModelType): @property def serialization_type(self) -> str: - return self.typing_name() if self.internal else self.name + return self.type_annotation() if self.internal else self.name @property def instance_check_template(self) -> str: @@ -386,7 +385,11 @@ class DPGModelType(GeneratedModelType): @property def serialization_type(self) -> str: - return self.typing_name() if self.internal else self.typing_name(False) + return ( + self.type_annotation() + if self.internal + else self.type_annotation(need_module_name=False) + ) @property def instance_check_template(self) -> str: diff --git a/packages/autorest.python/autorest/codegen/models/operation.py b/packages/autorest.python/autorest/codegen/models/operation.py index 93a8b51b4c8..f4159ecf75c 100644 --- a/packages/autorest.python/autorest/codegen/models/operation.py +++ b/packages/autorest.python/autorest/codegen/models/operation.py @@ -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 excep_schema.typing_name() + return excep_schema.type_annotation(is_operation_file=True) # in this case, it's just an AnyType return "'object'" diff --git a/packages/autorest.python/autorest/codegen/models/utils.py b/packages/autorest.python/autorest/codegen/models/utils.py index fd134ccfb75..1dba30ecbc5 100644 --- a/packages/autorest.python/autorest/codegen/models/utils.py +++ b/packages/autorest.python/autorest/codegen/models/utils.py @@ -26,5 +26,6 @@ def add_to_pylint_disable(curr_str: str, entry: str) -> str: def typing_name( file_name: str, internal: bool, need_module_name: bool, type_name: str ) -> str: + # in _operations.py, models is imported with alias "_models" module = "_models." if need_module_name else "" return module + (f"{file_name}." if internal else "") + type_name From 92b2f5284842d022c5e5f36b86029652c025d169 Mon Sep 17 00:00:00 2001 From: msyyc <70930885+msyyc@users.noreply.github.com> Date: Wed, 24 Apr 2024 15:44:35 +0800 Subject: [PATCH 8/9] update --- .../autorest/codegen/models/model_type.py | 12 ++++++------ .../autorest/codegen/models/operation.py | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/packages/autorest.python/autorest/codegen/models/model_type.py b/packages/autorest.python/autorest/codegen/models/model_type.py index 1e3ef9dd7ae..ea75f60f956 100644 --- a/packages/autorest.python/autorest/codegen/models/model_type.py +++ b/packages/autorest.python/autorest/codegen/models/model_type.py @@ -312,17 +312,17 @@ 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) - in_doc = kwargs.get("in_doc", False) + skip_quote = kwargs.get("skip_quote", False) retval = typing_name( file_name=self.code_model.models_filename, internal=self.internal, need_module_name=kwargs.get("need_module_name", True), type_name=self.name, ) - return retval if is_operation_file or in_doc else f'"{retval}"' + 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.type_annotation(need_module_name=False, in_doc=True)}" + 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 @@ -366,7 +366,7 @@ class MsrestModelType(GeneratedModelType): @property def serialization_type(self) -> str: - return self.type_annotation() 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: @@ -386,9 +386,9 @@ class DPGModelType(GeneratedModelType): @property def serialization_type(self) -> str: return ( - self.type_annotation() + self.type_annotation(skip_quote=True) if self.internal - else self.type_annotation(need_module_name=False) + else self.type_annotation(need_module_name=False, skip_quote=True) ) @property diff --git a/packages/autorest.python/autorest/codegen/models/operation.py b/packages/autorest.python/autorest/codegen/models/operation.py index f4159ecf75c..b1cc8aea26d 100644 --- a/packages/autorest.python/autorest/codegen/models/operation.py +++ b/packages/autorest.python/autorest/codegen/models/operation.py @@ -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 excep_schema.type_annotation(is_operation_file=True) + return excep_schema.type_annotation(skip_quote=True) # in this case, it's just an AnyType return "'object'" From 66c97504406ae7e32ad3970aaeca768b6bdd2995 Mon Sep 17 00:00:00 2001 From: msyyc <70930885+msyyc@users.noreply.github.com> Date: Fri, 26 Apr 2024 13:28:07 +0800 Subject: [PATCH 9/9] review --- .../autorest/codegen/models/enum_type.py | 10 +++------- .../autorest/codegen/models/model_type.py | 10 +++------- .../autorest.python/autorest/codegen/models/utils.py | 8 -------- 3 files changed, 6 insertions(+), 22 deletions(-) diff --git a/packages/autorest.python/autorest/codegen/models/enum_type.py b/packages/autorest.python/autorest/codegen/models/enum_type.py index 32d68e9380a..a4438527877 100644 --- a/packages/autorest.python/autorest/codegen/models/enum_type.py +++ b/packages/autorest.python/autorest/codegen/models/enum_type.py @@ -7,7 +7,6 @@ from .base import BaseType from .imports import FileImport, ImportType, TypingSection -from .utils import typing_name if TYPE_CHECKING: from .code_model import CodeModel @@ -176,12 +175,9 @@ def type_annotation(self, **kwargs: Any) -> str: :rtype: str """ if self.code_model.options["models_mode"]: - model_name = typing_name( - file_name=self.code_model.enums_filename, - internal=self.internal, - need_module_name=kwargs.get("need_module_name", True), - type_name=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}"' diff --git a/packages/autorest.python/autorest/codegen/models/model_type.py b/packages/autorest.python/autorest/codegen/models/model_type.py index ea75f60f956..20ea5795fe8 100644 --- a/packages/autorest.python/autorest/codegen/models/model_type.py +++ b/packages/autorest.python/autorest/codegen/models/model_type.py @@ -9,7 +9,6 @@ from autorest.codegen.models.utils import ( add_to_pylint_disable, NAME_LENGTH_LIMIT, - typing_name, ) from .base import BaseType from .constant_type import ConstantType @@ -313,12 +312,9 @@ class GeneratedModelType(ModelType): # pylint: disable=abstract-method def type_annotation(self, **kwargs: Any) -> str: is_operation_file = kwargs.pop("is_operation_file", False) skip_quote = kwargs.get("skip_quote", False) - retval = typing_name( - file_name=self.code_model.models_filename, - internal=self.internal, - need_module_name=kwargs.get("need_module_name", True), - type_name=self.name, - ) + 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: diff --git a/packages/autorest.python/autorest/codegen/models/utils.py b/packages/autorest.python/autorest/codegen/models/utils.py index 1dba30ecbc5..374d659135a 100644 --- a/packages/autorest.python/autorest/codegen/models/utils.py +++ b/packages/autorest.python/autorest/codegen/models/utils.py @@ -21,11 +21,3 @@ def add_to_pylint_disable(curr_str: str, entry: str) -> str: if curr_str: return f"{curr_str},{entry}" return f" # pylint: disable={entry}" - - -def typing_name( - file_name: str, internal: bool, need_module_name: bool, type_name: str -) -> str: - # in _operations.py, models is imported with alias "_models" - module = "_models." if need_module_name else "" - return module + (f"{file_name}." if internal else "") + type_name