From b8fccce176ad73b48e61f65c3316d3fbf2507f41 Mon Sep 17 00:00:00 2001 From: Ludovic Steinbach Date: Tue, 4 Mar 2025 11:31:19 +0100 Subject: [PATCH 1/6] Remove assertion --- src/ansys/openapi/common/_api_client.py | 1 - 1 file changed, 1 deletion(-) diff --git a/src/ansys/openapi/common/_api_client.py b/src/ansys/openapi/common/_api_client.py index 01a87064..9c7fdd79 100644 --- a/src/ansys/openapi/common/_api_client.py +++ b/src/ansys/openapi/common/_api_client.py @@ -409,7 +409,6 @@ def __deserialize(self, data: SerializedType, klass_name: str) -> DeserializedTy klass = self.models[klass_name] if issubclass(klass, Enum): - assert isinstance(data, str) return klass(data) else: assert isinstance(data, (dict, str)) From 879023545379a48bfece5d69630123e8cf5ac6b9 Mon Sep 17 00:00:00 2001 From: Ludovic Steinbach Date: Tue, 4 Mar 2025 11:42:25 +0100 Subject: [PATCH 2/6] Add some tests --- tests/models/__init__.py | 1 + tests/models/example_int_enum.py | 28 ++++++++++++++++++++++++++++ tests/test_api_client.py | 28 ++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+) create mode 100644 tests/models/example_int_enum.py diff --git a/tests/models/__init__.py b/tests/models/__init__.py index ed06b55a..7a000964 100644 --- a/tests/models/__init__.py +++ b/tests/models/__init__.py @@ -22,5 +22,6 @@ from .example_base_model import ExampleBaseModel from .example_enum import ExampleEnum +from .example_int_enum import ExampleIntEnum from .example_model import ExampleModel from .example_model_with_enum import ExampleModelWithEnum diff --git a/tests/models/example_int_enum.py b/tests/models/example_int_enum.py new file mode 100644 index 00000000..187e0dc7 --- /dev/null +++ b/tests/models/example_int_enum.py @@ -0,0 +1,28 @@ +# Copyright (C) 2022 - 2025 ANSYS, Inc. and/or its affiliates. +# SPDX-License-Identifier: MIT +# +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + +from enum import Enum + + +class ExampleIntEnum(Enum): + _200 = 200 + _404 = 404 diff --git a/tests/test_api_client.py b/tests/test_api_client.py index a6957bc9..a92ad707 100644 --- a/tests/test_api_client.py +++ b/tests/test_api_client.py @@ -401,6 +401,34 @@ def test_deserialize_enum(self): assert isinstance(serialized_enum, models.ExampleEnum) assert serialized_enum == models.ExampleEnum.GOOD + def test_deserialize_int_enum(self): + from . import models + + self._client.setup_client(models) + value = 200 + type_ref = "ExampleIntEnum" + serialized_enum = self._client._ApiClient__deserialize(value, type_ref) + assert isinstance(serialized_enum, models.ExampleIntEnum) + assert serialized_enum == models.ExampleIntEnum._200 + + @pytest.mark.parametrize( + ["value", "target_enum", "expected_error_msg"], + [ + ("200", "ExampleIntEnum", "'200' is not a valid ExampleIntEnum"), + (4.5, "ExampleIntEnum", "4.5 is not a valid ExampleIntEnum"), + (4, "ExampleIntEnum", "4 is not a valid ExampleIntEnum"), + ("SomeValue", "ExampleEnum", "'SomeValue' is not a valid ExampleEnum"), + (4.5, "ExampleEnum", "4.5 is not a valid ExampleEnum"), + (4, "ExampleEnum", "4 is not a valid ExampleEnum"), + ] + ) + def test_deserialize_enums_raises_helpful_message_on_wrong_value(self, value, target_enum, expected_error_msg): + from . import models + + self._client.setup_client(models) + with pytest.raises(ValueError, match=expected_error_msg): + serialized_enum = self._client._ApiClient__deserialize(value, target_enum) + @pytest.mark.parametrize( ("data", "target_type"), ( From 06338370e3eab28fb246c2ab51312383658367fa Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 4 Mar 2025 10:45:57 +0000 Subject: [PATCH 3/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tests/test_api_client.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/test_api_client.py b/tests/test_api_client.py index a92ad707..2e07a8e0 100644 --- a/tests/test_api_client.py +++ b/tests/test_api_client.py @@ -420,9 +420,11 @@ def test_deserialize_int_enum(self): ("SomeValue", "ExampleEnum", "'SomeValue' is not a valid ExampleEnum"), (4.5, "ExampleEnum", "4.5 is not a valid ExampleEnum"), (4, "ExampleEnum", "4 is not a valid ExampleEnum"), - ] + ], ) - def test_deserialize_enums_raises_helpful_message_on_wrong_value(self, value, target_enum, expected_error_msg): + def test_deserialize_enums_raises_helpful_message_on_wrong_value( + self, value, target_enum, expected_error_msg + ): from . import models self._client.setup_client(models) From 1fd8503e397eab37d0eb31702a820f7fa4ca4d08 Mon Sep 17 00:00:00 2001 From: pyansys-ci-bot <92810346+pyansys-ci-bot@users.noreply.github.com> Date: Tue, 4 Mar 2025 10:46:31 +0000 Subject: [PATCH 4/6] chore: adding changelog file 751.added.md [dependabot-skip] --- doc/changelog.d/751.added.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 doc/changelog.d/751.added.md diff --git a/doc/changelog.d/751.added.md b/doc/changelog.d/751.added.md new file mode 100644 index 00000000..81ee58b8 --- /dev/null +++ b/doc/changelog.d/751.added.md @@ -0,0 +1 @@ +Feat: support int enums \ No newline at end of file From ba15b852748cf469229ee92d2fee645fe34ff31d Mon Sep 17 00:00:00 2001 From: Ludovic Steinbach Date: Tue, 4 Mar 2025 12:14:31 +0100 Subject: [PATCH 5/6] Remove unused variable --- tests/test_api_client.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_api_client.py b/tests/test_api_client.py index 2e07a8e0..bbeed32e 100644 --- a/tests/test_api_client.py +++ b/tests/test_api_client.py @@ -429,7 +429,7 @@ def test_deserialize_enums_raises_helpful_message_on_wrong_value( self._client.setup_client(models) with pytest.raises(ValueError, match=expected_error_msg): - serialized_enum = self._client._ApiClient__deserialize(value, target_enum) + _ = self._client._ApiClient__deserialize(value, target_enum) @pytest.mark.parametrize( ("data", "target_type"), From 905e759d258de347f3dc381f3f4248426fe7a719 Mon Sep 17 00:00:00 2001 From: pyansys-ci-bot <92810346+pyansys-ci-bot@users.noreply.github.com> Date: Fri, 21 Mar 2025 11:31:20 +0000 Subject: [PATCH 6/6] chore: adding changelog file 751.added.md [dependabot-skip] --- doc/changelog.d/751.added.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/changelog.d/751.added.md b/doc/changelog.d/751.added.md index 81ee58b8..8384f21b 100644 --- a/doc/changelog.d/751.added.md +++ b/doc/changelog.d/751.added.md @@ -1 +1 @@ -Feat: support int enums \ No newline at end of file +Relax validation on enum values deserialization \ No newline at end of file