diff --git a/.chronus/changes/do-not-pad-for-enum-2024-5-14-16-6-32.md b/.chronus/changes/do-not-pad-for-enum-2024-5-14-16-6-32.md new file mode 100644 index 00000000000..0f628053141 --- /dev/null +++ b/.chronus/changes/do-not-pad-for-enum-2024-5-14-16-6-32.md @@ -0,0 +1,7 @@ +--- +changeKind: fix +packages: + - "@autorest/python" +--- + +Do not pad name for enum when generate SDK from Typespec \ No newline at end of file diff --git a/packages/autorest.python/autorest/preprocess/__init__.py b/packages/autorest.python/autorest/preprocess/__init__.py index e212aa2eecc..a38719ac4d7 100644 --- a/packages/autorest.python/autorest/preprocess/__init__.py +++ b/packages/autorest.python/autorest/preprocess/__init__.py @@ -17,7 +17,7 @@ from .python_mappings import CADL_RESERVED_WORDS, RESERVED_WORDS, PadType from .. import YamlUpdatePlugin, YamlUpdatePluginAutorest -from .._utils import parse_args, get_body_type_for_description, JSON_REGEXP, KNOWN_TYPES +from .._utils import parse_args, get_body_type_for_description, JSON_REGEXP, KNOWN_TYPES, update_enum_value def update_overload_section( @@ -241,11 +241,24 @@ def update_types(self, yaml_data: List[Dict[str, Any]]) -> None: type["snakeCaseName"] = to_snake_case(type["name"]) if type.get("values"): # we're enums + values_to_add = [] for value in type["values"]: padded_name = self.pad_reserved_words(value["name"].lower(), PadType.ENUM).upper() - if padded_name[0] in "0123456789": - padded_name = "ENUM_" + padded_name - value["name"] = padded_name + if self.version_tolerant: + if padded_name[0] in "0123456789": + padded_name = "ENUM_" + padded_name + value["name"] = padded_name + else: + if value["name"] != padded_name: + values_to_add.append( + update_enum_value( + name=padded_name, + value=value["value"], + description=value["description"], + enum_type=value["enumType"], + ) + ) + type["values"].extend(values_to_add) # add type for reference for v in HEADERS_CONVERT_IN_METHOD.values(): diff --git a/packages/autorest.python/test/vanilla/legacy/AcceptanceTests/asynctests/test_reserved_words.py b/packages/autorest.python/test/vanilla/legacy/AcceptanceTests/asynctests/test_reserved_words.py index 9c70f27c289..8c362e6323f 100644 --- a/packages/autorest.python/test/vanilla/legacy/AcceptanceTests/asynctests/test_reserved_words.py +++ b/packages/autorest.python/test/vanilla/legacy/AcceptanceTests/asynctests/test_reserved_words.py @@ -65,4 +65,5 @@ async def test_operation_with_url(client): @pytest.mark.asyncio async def test_operation_with_enum(client): + await client.reserved_enum(models.MyEnum.IMPORT) await client.reserved_enum(models.MyEnum.IMPORT_ENUM) diff --git a/packages/autorest.python/test/vanilla/legacy/AcceptanceTests/test_reserved_words.py b/packages/autorest.python/test/vanilla/legacy/AcceptanceTests/test_reserved_words.py index 3774520f8a6..fbfceb2fe25 100644 --- a/packages/autorest.python/test/vanilla/legacy/AcceptanceTests/test_reserved_words.py +++ b/packages/autorest.python/test/vanilla/legacy/AcceptanceTests/test_reserved_words.py @@ -58,4 +58,5 @@ def test_operation_with_url(client): def test_operation_with_enum(client): + client.reserved_enum(models.MyEnum.IMPORT) client.reserved_enum(models.MyEnum.IMPORT_ENUM) diff --git a/packages/autorest.python/test/vanilla/legacy/Expected/AcceptanceTests/ReservedWords/reservedwords/aio/operations/_reserved_words_client_operations.py b/packages/autorest.python/test/vanilla/legacy/Expected/AcceptanceTests/ReservedWords/reservedwords/aio/operations/_reserved_words_client_operations.py index df0449c8e3e..bd1f75e3517 100644 --- a/packages/autorest.python/test/vanilla/legacy/Expected/AcceptanceTests/ReservedWords/reservedwords/aio/operations/_reserved_words_client_operations.py +++ b/packages/autorest.python/test/vanilla/legacy/Expected/AcceptanceTests/ReservedWords/reservedwords/aio/operations/_reserved_words_client_operations.py @@ -329,8 +329,8 @@ async def operation_with_url( async def reserved_enum(self, enum_parameter: Union[str, _models.MyEnum], **kwargs: Any) -> JSON: """Operation that accepts a reserved enum value. - :param enum_parameter: Pass in MyEnum.IMPORT to pass. Known values are: "import" and "other". - Required. + :param enum_parameter: Pass in MyEnum.IMPORT to pass. Known values are: "import", "other", and + "import". Required. :type enum_parameter: str or ~reservedwords.models.MyEnum :return: JSON or the result of cls(response) :rtype: JSON diff --git a/packages/autorest.python/test/vanilla/legacy/Expected/AcceptanceTests/ReservedWords/reservedwords/models/_reserved_words_client_enums.py b/packages/autorest.python/test/vanilla/legacy/Expected/AcceptanceTests/ReservedWords/reservedwords/models/_reserved_words_client_enums.py index 67513eae646..4dbf78eda21 100644 --- a/packages/autorest.python/test/vanilla/legacy/Expected/AcceptanceTests/ReservedWords/reservedwords/models/_reserved_words_client_enums.py +++ b/packages/autorest.python/test/vanilla/legacy/Expected/AcceptanceTests/ReservedWords/reservedwords/models/_reserved_words_client_enums.py @@ -13,5 +13,6 @@ class MyEnum(str, Enum, metaclass=CaseInsensitiveEnumMeta): """MyEnum.""" - IMPORT_ENUM = "import" + IMPORT = "import" OTHER = "other" + IMPORT_ENUM = "import" diff --git a/packages/autorest.python/test/vanilla/legacy/Expected/AcceptanceTests/ReservedWords/reservedwords/operations/_reserved_words_client_operations.py b/packages/autorest.python/test/vanilla/legacy/Expected/AcceptanceTests/ReservedWords/reservedwords/operations/_reserved_words_client_operations.py index d4a7137b4ae..e396c7f7b9c 100644 --- a/packages/autorest.python/test/vanilla/legacy/Expected/AcceptanceTests/ReservedWords/reservedwords/operations/_reserved_words_client_operations.py +++ b/packages/autorest.python/test/vanilla/legacy/Expected/AcceptanceTests/ReservedWords/reservedwords/operations/_reserved_words_client_operations.py @@ -435,8 +435,8 @@ def operation_with_url( def reserved_enum(self, enum_parameter: Union[str, _models.MyEnum], **kwargs: Any) -> JSON: """Operation that accepts a reserved enum value. - :param enum_parameter: Pass in MyEnum.IMPORT to pass. Known values are: "import" and "other". - Required. + :param enum_parameter: Pass in MyEnum.IMPORT to pass. Known values are: "import", "other", and + "import". Required. :type enum_parameter: str or ~reservedwords.models.MyEnum :return: JSON or the result of cls(response) :rtype: JSON