Skip to content

Commit

Permalink
Rename encodable protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
Mauko Quiroga committed Sep 21, 2021
1 parent 5be77d1 commit 1e88559
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
19 changes: 14 additions & 5 deletions openfisca_core/indexed_enums/enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@

import numpy

from openfisca_core.types import ArrayType, Encodable
from openfisca_core.types import ArrayType, SupportsEncode

from .. import indexed_enums as enums
from .enum_array import EnumArray

A = Union[
EnumArray,
ArrayType[Encodable],
ArrayType[SupportsEncode],
ArrayType[bytes],
ArrayType[int],
ArrayType[str],
Expand Down Expand Up @@ -133,6 +133,18 @@ def __repr__(self) -> str:
def __str__(self) -> str:
return f"{self.__class__.__name__}.{self.name}"

def __eq__(self, other: object) -> bool:
if not isinstance(other, Enum):
return NotImplemented

return self.index == other.index

def __ne__(self, other: object) -> bool:
if not isinstance(other, Enum):
return NotImplemented

return self.index != other.index

def __lt__(self, other: object) -> bool:
if not isinstance(other, Enum):
return NotImplemented
Expand All @@ -157,9 +169,6 @@ def __ge__(self, other: object) -> bool:

return self.index >= other.index

__eq__ = object.__eq__
"""Bypass the slow :meth:`~enum.Enum.__eq__`."""

__hash__ = object.__hash__
""":meth:`.__hash__` must also be defined as so to stay hashable."""

Expand Down
6 changes: 3 additions & 3 deletions openfisca_core/indexed_enums/enum_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import numpy

from openfisca_core.types import ArrayLike, ArrayType, Encodable
from openfisca_core.types import ArrayLike, ArrayType, SupportsEncode


class EnumArray(numpy.ndarray):
Expand Down Expand Up @@ -72,7 +72,7 @@ class EnumArray(numpy.ndarray):
def __new__(
cls,
input_array: ArrayType[int],
possible_values: Optional[Type[Encodable]] = None,
possible_values: Optional[Type[SupportsEncode]] = None,
) -> EnumArray:
"""See comment above."""

Expand Down Expand Up @@ -195,7 +195,7 @@ def _forbidden_operation(self, other: Any) -> NoReturn:
__and__ = _forbidden_operation
__or__ = _forbidden_operation

def decode(self) -> ArrayLike[Encodable]:
def decode(self) -> ArrayLike[SupportsEncode]:
"""Decodes itself to a normal array.
Returns:
Expand Down

0 comments on commit 1e88559

Please sign in to comment.