From 88e1d026eb9ebd1230da49aa04a7df3344404694 Mon Sep 17 00:00:00 2001 From: LightArrowsEXE Date: Mon, 21 Oct 2024 02:13:42 -0700 Subject: [PATCH] CustomEnum: Release infinite recursion (#11) Currently, an issue occurs if you pass i.e. Matrix(-1). It will continually call `cls(value)`, which in turn calls `cls.from_param`, which eventually circles back to `_missing_`. This at least puts a stopgap to this issue and properly raises the error, although I'm sure there's a better solution that fundamentally deals with the problem. --- stgpytools/enums/base.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/stgpytools/enums/base.py b/stgpytools/enums/base.py index 182b872..54686f6 100644 --- a/stgpytools/enums/base.py +++ b/stgpytools/enums/base.py @@ -46,19 +46,20 @@ def from_param(cls: type[SelfEnum], value: Any, func_except: FuncExceptT | None try: return cls(value) - except ValueError: - ... + except Exception: + pass if isinstance(func_except, tuple): func_name, var_name = func_except else: - func_name, var_name = func_except, '' + func_name, var_name = func_except, cls raise NotFoundEnumValue( - 'Value for "{var_name}" argument must be a valid {enum_name}.\n' - 'It can be a value in [{readable_enum}].', func_name, - var_name=var_name, enum_name=cls, - readable_enum=iter([f'{x.name} ({x.value})' for x in cls]) + 'The given value for "{var_name}" argument must be a valid {enum_name}, not "{value}"!\n' + 'Valid values are: [{readable_enum}].', func_name, + var_name=var_name, enum_name=cls, value=value, + readable_enum=iter([f'{x.name} ({x.value})' for x in cls]), + reason=value )