From cfd174996acbc7588989c058c7a625db72e47b7d Mon Sep 17 00:00:00 2001 From: Mauko Quiroga Date: Wed, 9 Oct 2024 04:22:03 +0200 Subject: [PATCH] fix(enums): fix __repr__ (#1267) --- openfisca_core/data_storage/on_disk_storage.py | 2 +- openfisca_core/indexed_enums/_enum_type.py | 4 ++-- openfisca_core/indexed_enums/enum.py | 15 +++++++++------ openfisca_core/indexed_enums/enum_array.py | 11 ++++++----- 4 files changed, 18 insertions(+), 14 deletions(-) diff --git a/openfisca_core/data_storage/on_disk_storage.py b/openfisca_core/data_storage/on_disk_storage.py index 3d0ef7fc1..99cfe56dd 100644 --- a/openfisca_core/data_storage/on_disk_storage.py +++ b/openfisca_core/data_storage/on_disk_storage.py @@ -87,7 +87,7 @@ def _decode_file(self, file: str) -> t.Array[t.DTypeGeneric]: ... storage = data_storage.OnDiskStorage(directory) ... storage.put(value, period) ... storage._decode_file(storage._files[period]) - EnumArray([]) + EnumArray(Housing.TENANT) """ enum = self._enums.get(file) diff --git a/openfisca_core/indexed_enums/_enum_type.py b/openfisca_core/indexed_enums/_enum_type.py index fe098e2c7..4208ab3ce 100644 --- a/openfisca_core/indexed_enums/_enum_type.py +++ b/openfisca_core/indexed_enums/_enum_type.py @@ -57,7 +57,7 @@ class EnumType(t.EnumType): ... TENANT = "Tenant" >>> Housing.items - rec.array([(0, 'OWNER', ), ...]) + rec.array([(0, 'OWNER', Housing.OWNER), (1, 'TENANT', Housing.TENAN...) >>> Housing.indices array([0, 1], dtype=int16) @@ -66,7 +66,7 @@ class EnumType(t.EnumType): array(['OWNER', 'TENANT'], dtype='>> Housing.enums - array([, ], dtype...) + array([Housing.OWNER, Housing.TENANT], dtype=object) """ diff --git a/openfisca_core/indexed_enums/enum.py b/openfisca_core/indexed_enums/enum.py index 069cc8fa9..938335bcb 100644 --- a/openfisca_core/indexed_enums/enum.py +++ b/openfisca_core/indexed_enums/enum.py @@ -27,22 +27,22 @@ class Enum(t.Enum, metaclass=EnumType): "" >>> repr(Housing.TENANT) - "" + 'Housing.TENANT' >>> str(Housing.TENANT) 'Housing.TENANT' >>> dict([(Housing.TENANT, Housing.TENANT.value)]) - {: 'Tenant'} + {Housing.TENANT: 'Tenant'} >>> list(Housing) - [, , ...] + [Housing.OWNER, Housing.TENANT, Housing.FREE_LODGER, Housing.HOMELESS] >>> Housing["TENANT"] - + Housing.TENANT >>> Housing("Tenant") - + Housing.TENANT >>> Housing.TENANT in Housing True @@ -106,6 +106,9 @@ def __init__(self, *__args: object, **__kwargs: object) -> None: """ self.index = len(self._member_names_) + def __repr__(self) -> str: + return f"{self.__class__.__name__}.{self.name}" + def __eq__(self, other: object) -> bool: if not isinstance(other, Enum): return NotImplemented @@ -158,7 +161,7 @@ def encode( >>> array = numpy.array([1]) >>> enum_array = enum.EnumArray(array, Housing) >>> Housing.encode(enum_array) - EnumArray([]) + EnumArray(Housing.TENANT) # Array of Enum diff --git a/openfisca_core/indexed_enums/enum_array.py b/openfisca_core/indexed_enums/enum_array.py index 2e9ebf148..807be2ec5 100644 --- a/openfisca_core/indexed_enums/enum_array.py +++ b/openfisca_core/indexed_enums/enum_array.py @@ -36,7 +36,7 @@ class EnumArray(t.EnumArray): "" >>> repr(enum_array) - "EnumArray([])" + 'EnumArray(Housing.TENANT)' >>> str(enum_array) "['TENANT']" @@ -55,14 +55,14 @@ class EnumArray(t.EnumArray): >>> enum_array = enum.EnumArray(list(Housing), Housing) >>> enum_array[Housing.TENANT.index] - + Housing.TENANT >>> class OccupancyStatus(variables.Variable): ... value_type = enum.Enum ... possible_values = Housing >>> enum.EnumArray(array, OccupancyStatus.possible_values) - EnumArray([]) + EnumArray(Housing.TENANT) .. _Subclassing ndarray: https://numpy.org/doc/stable/user/basics.subclassing.html @@ -229,7 +229,7 @@ def decode(self) -> numpy.object_: >>> array = numpy.array([1]) >>> enum_array = enum.EnumArray(array, Housing) >>> enum_array.decode() - array([], dtype=object) + array([Housing.TENANT], dtype=object) """ return numpy.select( @@ -264,7 +264,8 @@ def decode_to_str(self) -> numpy.str_: ) def __repr__(self) -> str: - return f"{self.__class__.__name__}({self.decode()!s})" + items = ", ".join(str(item) for item in self.decode()) + return f"{self.__class__.__name__}({items})" def __str__(self) -> str: return str(self.decode_to_str())