Skip to content

Commit

Permalink
CustomEnum: Release infinite recursion (#11)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
LightArrowsEXE authored Oct 21, 2024
1 parent eba8a9a commit 88e1d02
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions stgpytools/enums/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)


Expand Down

0 comments on commit 88e1d02

Please sign in to comment.