Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make enum singletons into attributes of the enum class #7232

Closed
cosmicexplorer opened this issue Feb 9, 2019 · 0 comments · Fixed by #7269
Closed

make enum singletons into attributes of the enum class #7232

cosmicexplorer opened this issue Feb 9, 2019 · 0 comments · Fixed by #7269

Comments

@cosmicexplorer
Copy link
Contributor

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
This was referenced Feb 20, 2019
cosmicexplorer added a commit that referenced this issue Mar 2, 2019
### 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!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant