From 95284ae0560eedbb1ff06bd7659a2aff007d1fda Mon Sep 17 00:00:00 2001 From: afernand Date: Tue, 6 May 2025 09:42:40 +0200 Subject: [PATCH 1/9] feat: Add common exceptions module --- src/ansys/tools/exceptions.py | 45 +++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 src/ansys/tools/exceptions.py diff --git a/src/ansys/tools/exceptions.py b/src/ansys/tools/exceptions.py new file mode 100644 index 00000000..1ace8de5 --- /dev/null +++ b/src/ansys/tools/exceptions.py @@ -0,0 +1,45 @@ +class AnsysError(Exception): + """Base class for all exceptions raised by the Ansys API. + + Can be used to catch all Ansys-related exceptions.""" + + def __init__(self, message: str) -> None: + super().__init__(message) + self.message = message + + +class AnsysTypeException(AnsysError): + """Exception raised when python wise types would work, but internal + Ansys specific typing is not right. + + Parameters + ---------- + expected_type : str + The expected type of the argument. + actual_type : str + The actual type of the argument. + """ + + def __init__(self, expected_type: str = None, actual_type: str = None) -> None: + """Initialize the exception with expected and actual types.""" + if expected_type is not None and actual_type is not None: + message = f"Expected type {expected_type}, but got {actual_type}." + else: + message = "Ansys type used is not compatible." + super().__init__(message) + self.expected_type = expected_type + self.actual_type = actual_type + +class AnsysLogicError(AnsysError): + """Exception raised when an unexpected logical condition occurs. + + Parameters + ---------- + message : str + The error message. + """ + + def __init__(self, message: str) -> None: + """Initialize the exception with a message.""" + super().__init__(message) + self.message = message \ No newline at end of file From 9295d7c31152bd9a3cfb08730a2270ee070caf07 Mon Sep 17 00:00:00 2001 From: afernand Date: Tue, 6 May 2025 09:43:20 +0200 Subject: [PATCH 2/9] fix: Add EOL --- src/ansys/tools/exceptions.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ansys/tools/exceptions.py b/src/ansys/tools/exceptions.py index 1ace8de5..ca786613 100644 --- a/src/ansys/tools/exceptions.py +++ b/src/ansys/tools/exceptions.py @@ -42,4 +42,4 @@ class AnsysLogicError(AnsysError): def __init__(self, message: str) -> None: """Initialize the exception with a message.""" super().__init__(message) - self.message = message \ No newline at end of file + self.message = message From 634b4a3c14ba90509323463c730a94025c250efe Mon Sep 17 00:00:00 2001 From: afernand Date: Tue, 6 May 2025 10:17:02 +0200 Subject: [PATCH 3/9] fix: pre-commit --- src/ansys/tools/exceptions.py | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/src/ansys/tools/exceptions.py b/src/ansys/tools/exceptions.py index ca786613..161865b4 100644 --- a/src/ansys/tools/exceptions.py +++ b/src/ansys/tools/exceptions.py @@ -1,15 +1,40 @@ +# Copyright (C) 2024 - 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. + class AnsysError(Exception): """Base class for all exceptions raised by the Ansys API. - Can be used to catch all Ansys-related exceptions.""" + Can be used to catch all Ansys-related exceptions. + """ def __init__(self, message: str) -> None: super().__init__(message) self.message = message -class AnsysTypeException(AnsysError): - """Exception raised when python wise types would work, but internal +class AnsysTypeError(AnsysError): + """Error raised when an argument is of the wrong type. + + Exception raised when python wise types would work, but internal Ansys specific typing is not right. Parameters From c5e1bc7c536bc221c83f20edd9f12b11e0da3df6 Mon Sep 17 00:00:00 2001 From: afernand Date: Tue, 6 May 2025 10:19:32 +0200 Subject: [PATCH 4/9] fix: Missing docs --- src/ansys/tools/exceptions.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/ansys/tools/exceptions.py b/src/ansys/tools/exceptions.py index 161865b4..b6fb3319 100644 --- a/src/ansys/tools/exceptions.py +++ b/src/ansys/tools/exceptions.py @@ -19,6 +19,8 @@ # 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. +"""Exceptions module.""" + class AnsysError(Exception): """Base class for all exceptions raised by the Ansys API. @@ -27,6 +29,7 @@ class AnsysError(Exception): """ def __init__(self, message: str) -> None: + """Initialize the exception with a message.""" super().__init__(message) self.message = message From 1a4c30630a37799633353a6ea54150d8ddd619dd Mon Sep 17 00:00:00 2001 From: Alex Fernandez Date: Wed, 7 May 2025 12:56:32 +0200 Subject: [PATCH 5/9] Update src/ansys/tools/exceptions.py Co-authored-by: Roberto Pastor Muela <37798125+RobPasMue@users.noreply.github.com> --- src/ansys/tools/exceptions.py | 1 - 1 file changed, 1 deletion(-) diff --git a/src/ansys/tools/exceptions.py b/src/ansys/tools/exceptions.py index b6fb3319..6ced6f52 100644 --- a/src/ansys/tools/exceptions.py +++ b/src/ansys/tools/exceptions.py @@ -70,4 +70,3 @@ class AnsysLogicError(AnsysError): def __init__(self, message: str) -> None: """Initialize the exception with a message.""" super().__init__(message) - self.message = message From a134e0d7169c3f24dbdfe926ccdcddc97837160b Mon Sep 17 00:00:00 2001 From: afernand Date: Wed, 7 May 2025 13:08:37 +0200 Subject: [PATCH 6/9] fix: Admit type, make params mandatory --- src/ansys/tools/exceptions.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/ansys/tools/exceptions.py b/src/ansys/tools/exceptions.py index 6ced6f52..b41d4837 100644 --- a/src/ansys/tools/exceptions.py +++ b/src/ansys/tools/exceptions.py @@ -48,12 +48,10 @@ class AnsysTypeError(AnsysError): The actual type of the argument. """ - def __init__(self, expected_type: str = None, actual_type: str = None) -> None: + def __init__(self, expected_type: str | type, actual_type: str | type = None) -> None: """Initialize the exception with expected and actual types.""" - if expected_type is not None and actual_type is not None: - message = f"Expected type {expected_type}, but got {actual_type}." - else: - message = "Ansys type used is not compatible." + + message = f"Expected type {expected_type}, but got {actual_type}." super().__init__(message) self.expected_type = expected_type self.actual_type = actual_type From 581a74a0270551781ed2d4f4795913ce60c47e2d Mon Sep 17 00:00:00 2001 From: afernand Date: Wed, 7 May 2025 13:12:08 +0200 Subject: [PATCH 7/9] fix: pre-commit --- src/ansys/tools/exceptions.py | 1 - 1 file changed, 1 deletion(-) diff --git a/src/ansys/tools/exceptions.py b/src/ansys/tools/exceptions.py index b41d4837..b403ca5c 100644 --- a/src/ansys/tools/exceptions.py +++ b/src/ansys/tools/exceptions.py @@ -50,7 +50,6 @@ class AnsysTypeError(AnsysError): def __init__(self, expected_type: str | type, actual_type: str | type = None) -> None: """Initialize the exception with expected and actual types.""" - message = f"Expected type {expected_type}, but got {actual_type}." super().__init__(message) self.expected_type = expected_type From 96d1e2b7794ad85f8cc569b3558e0d3b7e2adbb5 Mon Sep 17 00:00:00 2001 From: Alex Fernandez Date: Wed, 7 May 2025 15:10:46 +0200 Subject: [PATCH 8/9] Update src/ansys/tools/exceptions.py Co-authored-by: Roberto Pastor Muela <37798125+RobPasMue@users.noreply.github.com> --- src/ansys/tools/exceptions.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/ansys/tools/exceptions.py b/src/ansys/tools/exceptions.py index b403ca5c..c5695851 100644 --- a/src/ansys/tools/exceptions.py +++ b/src/ansys/tools/exceptions.py @@ -50,6 +50,8 @@ class AnsysTypeError(AnsysError): def __init__(self, expected_type: str | type, actual_type: str | type = None) -> None: """Initialize the exception with expected and actual types.""" + expected_type = expected_type if isinstance(expected_type, str) else expected_type.__name__ + actual_type= actual_type if isinstance(actual_type, str) else actual_type.__name__ message = f"Expected type {expected_type}, but got {actual_type}." super().__init__(message) self.expected_type = expected_type From 81f23b0149ff6c2642e5ce7bf2783fe150bd7fcb Mon Sep 17 00:00:00 2001 From: Alex Fernandez Date: Wed, 7 May 2025 15:19:53 +0200 Subject: [PATCH 9/9] Update src/ansys/tools/exceptions.py Co-authored-by: Roberto Pastor Muela <37798125+RobPasMue@users.noreply.github.com> --- src/ansys/tools/exceptions.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ansys/tools/exceptions.py b/src/ansys/tools/exceptions.py index c5695851..e4118c5b 100644 --- a/src/ansys/tools/exceptions.py +++ b/src/ansys/tools/exceptions.py @@ -42,9 +42,9 @@ class AnsysTypeError(AnsysError): Parameters ---------- - expected_type : str + expected_type : str | type The expected type of the argument. - actual_type : str + actual_type : str | type The actual type of the argument. """