You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
See #7226 (comment). After that PR is merged, we should do something like:
diff --git a/src/python/pants/util/objects.py b/src/python/pants/util/objects.py
index e22e4b9..8208d0f 100644
--- a/src/python/pants/util/objects.py+++ b/src/python/pants/util/objects.py@@ -6,7 +6,7 @@ from __future__ import absolute_import, division, print_function, unicode_litera
import sys
from abc import abstractmethod
-from builtins import object, zip+from builtins import object, zip, str
from collections import OrderedDict, namedtuple
from future.utils import PY2
@@ -208,7 +208,16 @@ def enum(*args):
Any enum subclass can be constructed with its create() classmethod. This method will use the first
element of `all_values` as the default value, but enum classes can override this behavior by
- setting `default_value` in the class body.+ setting `default_value` in the class body.++ If `all_values` contains only strings, then each variant is made into an attribute on the+ generated enum class object. This allows code such as the following:++ class MyResult(enum(['success', 'failure'])):+ pass++ MyResult.success # The same as: MyResult(success)+ MyResult.failure # The same as: MyResult(failure)
:param string field_name: A string used as the field for the datatype. This positional argument is
optional, and defaults to 'value'. Note that `enum()` does not yet
@@ -332,6 +342,11 @@ def enum(*args):
"""
return [cls.create(variant_value) for variant_value in cls.allowed_values]
+ if all(isinstance(v, str) for v in all_values_realized):+ for variant in ChoiceDatatype.iterate_enum_variants():+ setattr(ChoiceDatatype, ChoiceDatatype._get_value(variant), variant)+
return ChoiceDatatype
The text was updated successfully, but these errors were encountered:
### Problem
Resolves#7232, resolves#7248, and addresses #7249 (comment).
### Solution
- Remove enum defaults and the `.create()` method with complicated argument handling.
- Allow enums to be `==` compared as long as they're the same type.
- Allow accessing enum instances with class attributes.
### Result
enums are nicer!
See #7226 (comment). After that PR is merged, we should do something like:
The text was updated successfully, but these errors were encountered: