Use the Enum objects as dictionary keys directly instead of the string value
Categories
(Testing :: Performance, task, P2)
Tracking
(Not tracked)
People
(Reporter: sparky, Unassigned)
References
(Blocks 1 open bug)
Details
After bug 1803361 lands, we'll have Enum classes for all of our settings. These are special enum classes (ClassificationEnum) that allow us to use them as indices to arrays/matrices and also store a string-value representation.
We access the string element with enum.CLASSIFICATION.value
but in many places in the code we could simply use enum.CLASSIFICATION
. We should see if we can replace the usages to get rid of the .value
.
Comment 1•1 year ago
|
||
I tried to get rid of the .value
attribute[0] in some ways. (There is one in the link[0], But We should remove all .value
)
I couldn't fix it in the following way.
- I used the
__str__
(or__repr__
) method as follow:
class ClassificationEnum(enum.Enum):
+ def __str__(self):
+ return self.value
class Platforms(ClassificationEnum):
platforms = {
- Platforms.ANDROID_A51.value: {
+ Platforms.ANDROID_A51: {
It's not working well. Platforms.ANDROID_A51.value
is str
type. But Platforms.ANDROID_A51
type is Platforms
class type.
If I use this method, I should cover the str() to all the enums(e.g, str(Platforms.ANDROID_A51)
).
This isn't the way we want it to be.
- I used the
str mixin
in the ClassificationEnum as follows:
-class ClassificationEnum(enum.Enum):
+class ClassificationEnum(str, enum.Enum):
@property
def value(self):
- return self._value_["value"]
+ return eval(self._value_)["value"]
def __index__(self):
- return self._value_["index"]
+ return eval(self._value_)["index"]
def __int__(self):
- return self._value_["index"]
+ return eval(self._value_)["index"]
+ def __str__(self):
+ return self.value
class Platforms(ClassificationEnum):
platforms = {
- Platforms.ANDROID_A51.value: {
+ Platforms.ANDROID_A51: {
I'd like to make the enum(e.g, Platforms.ANDROID_A51) to the str type.
In this way, It is same necessary to cover the str()
(e.g, str(Platforms.ANDROID_A51)
).
Description
•