-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ComparableError to enable comparing exceptions by value; implemen…
…t implicit support in Option/Either monads.
- Loading branch information
Showing
6 changed files
with
128 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
''' | ||
Chicory ML Workflow Manager | ||
Copyright (C) 2024 Alexis Maya-Isabelle Shuping | ||
This program is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
This program is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
You should have received a copy of the GNU General Public License | ||
along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
''' | ||
|
||
from typing import List | ||
|
||
class ComparableError: | ||
''' Encapsulates an Exception, providing a sensible `__eq__` operation. | ||
''' | ||
def __init__(self, exc: Exception): | ||
self.__exc = exc | ||
|
||
@property | ||
def exc(self): | ||
return self.__exc | ||
|
||
@classmethod | ||
def encapsulate(cls, exc: any): | ||
''' Encapsulate an Exception in a ComparableError. | ||
If this method is called on anything other than an Exception, the | ||
parameter is returned unchanged. | ||
''' | ||
if issubclass(type(exc), Exception): | ||
return cls(exc) | ||
else: | ||
return exc | ||
|
||
@classmethod | ||
def array_encapsulate(cls, arr: List[any]): | ||
''' Encapsulate an entire array of Exceptions | ||
This method is useful for performing array comparisons. | ||
''' | ||
return [ComparableError.encapsulate(e) for e in arr] | ||
|
||
def __eq__(self, other): | ||
''' Check whether the encapsulated Exceptions are equal. | ||
Two Exceptions are defined as equal if: | ||
- Both are of the same type | ||
- If one has an `args` attribute: | ||
- The other has an `args` attribute | ||
- The two `args` attributes evaluate to equal. | ||
''' | ||
other = ComparableError.encapsulate(other) # ensure `other` is comparable | ||
if type(self.exc) != type(other.exc): | ||
return False | ||
|
||
if hasattr(self.exc, 'args'): | ||
if not hasattr(other.exc, 'args'): | ||
return False | ||
else: | ||
return self.exc.args == other.exc.args | ||
else: | ||
return True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
from modules.types.ComparableError import ComparableError | ||
|
||
def test_get_exc(): | ||
x = TypeError("test error") | ||
assert ComparableError(x).exc == x | ||
|
||
def test_encapsulate(): | ||
class CustomException(Exception): | ||
pass | ||
|
||
class NotAnException(): | ||
pass | ||
|
||
assert type(ComparableError.encapsulate(TypeError())) == ComparableError | ||
assert type(ComparableError.encapsulate(CustomException())) == ComparableError | ||
assert type(ComparableError.encapsulate(12)) == int | ||
assert type(ComparableError.encapsulate("test")) == str | ||
assert type(ComparableError.encapsulate(NotAnException())) == NotAnException | ||
|
||
def test_eq(): | ||
assert ComparableError(TypeError("test")) == ComparableError(TypeError("test")) | ||
assert ComparableError(TypeError("test")) != ComparableError(TypeError("test2")) | ||
assert ComparableError(TypeError("test")) != ComparableError(ArithmeticError("test")) | ||
|
||
def test_auto_encapsulate(): | ||
assert ComparableError(TypeError("test")) == TypeError("test") | ||
assert ComparableError(TypeError("test")) != TypeError("test2") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters