Skip to content

Commit

Permalink
Fix enumerations
Browse files Browse the repository at this point in the history
  • Loading branch information
lycantropos committed Nov 11, 2024
1 parent d0ae5df commit 362dce3
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions rithm/_enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ def __init_subclass__(cls, /, **_kwargs: Any) -> NoReturn:
)

def __new__(cls, value: int, /) -> Self:
if not isinstance(value, int):
raise TypeError(type(value))
if not (0 <= value < len(get_type_hints(cls, globals()))):
if not (
isinstance(value, int)
and 0 <= value < len(get_type_hints(cls, globals()))
):
raise ValueError(f'{value} is not a valid {cls.__qualname__}')
try:
return cls._cache[value]
Expand All @@ -37,6 +38,9 @@ def __new__(cls, value: int, /) -> Self:
cls._cache[value] = self
return self

def __getnewargs__(self) -> tuple[int]:
return (self._value,)

def __repr__(self) -> str:
name = next(
name for name, field in vars(type(self)).items() if field is self
Expand Down Expand Up @@ -70,9 +74,10 @@ def __init_subclass__(cls, /, **_kwargs: Any) -> NoReturn:
)

def __new__(cls, value: int, /) -> Self:
if not isinstance(value, int):
raise TypeError(type(value))
if not (0 <= value < len(get_type_hints(cls, globals()))):
if not (
isinstance(value, int)
and 0 <= value < len(get_type_hints(cls, globals()))
):
raise ValueError(f'{value} is not a valid {cls.__qualname__}')
try:
return cls._cache[value]
Expand All @@ -82,6 +87,9 @@ def __new__(cls, value: int, /) -> Self:
cls._cache[value] = self
return self

def __getnewargs__(self) -> tuple[int]:
return (self._value,)

def __repr__(self) -> str:
name = next(
name for name, field in vars(type(self)).items() if field is self
Expand Down

0 comments on commit 362dce3

Please sign in to comment.