Subclassing unittest.TestCase, providing keyword args to `super()__init__()` and getting `__init__ got an unexpected keyword argument - python-2.7

I'm attempting to compose a class hierarchy using unittest.TestCase. These aren't strictly unit-tests I'm running here, I'm trying to instead test a bunch of string parsing functionality that is reliant on a few parameters (customer-name and missing-value for example). The general idea is to simply utilize some of unittest's conveniences and keep things DRY.
import unittest
import parsingfunc as i
class CustomerTestCase(unittest.TestCase):
"""Base class, most general functionality and test cases"""
def __init__(self, testname):
super(CustomerTestCase, self).__init__(testname)
# helpers
def mess_with_spaces():
...
def mess_with_case():
...
# tests
def test_case(self, value):
"""
Test that parsing function produces the same
value regardless of case of input
"""
self.assertEqual(self.func(value, missing=self.missing, customer=self.name),
self.func(self.mess_with_case(value), missing=self.missing, customer=self.name)))
...
def test_spaces(self, value):
"""
Test that parsing function produces the same
value regardless of spacing present in input
"""
...
class AisleValues(CustomerTestCase):
"""Base class for testing aisle values"""
def __init__(self, testname, customername=None, missing=None):
super(CustomerTestCase, self).__init__(testname)
self.name = customername
self.missing = missing
self.func = i.aisle_to_num
...
class PeacockAisles(AisleValues):
"""Peacock aisle parsing test cases"""
def __init__(self, testname):
super(AisleValues, self).__init__(testname, customername='peacock', missing='nan')
...
And now try to create instances of these classes
In [6]: a = i.CustomerTestCase('space_test')
In [7]: a.__dict__
Out[7]:
{'_cleanups': [],
'_resultForDoCleanups': None,
'_testMethodDoc': '\n Test that parsing function produces the same\n value regardless of spacing present in input\n ',
'_testMethodName': 'test_spaces',
'_type_equality_funcs': {list: 'assertListEqual',
dict: 'assertDictEqual',
set: 'assertSetEqual',
frozenset: 'assertSetEqual',
tuple: 'assertTupleEqual',
unicode: 'assertMultiLineEqual'}}
In [8]: b = i.AisleValues('test_spaces')
In [9]: b.__dict__
Out[9]:
{'_cleanups': [],
'_resultForDoCleanups': None,
'_testMethodDoc': '\n Test that parsing function produces the same\n value regardless of spacing present in input\n ',
'_testMethodName': 'test_spaces',
'_type_equality_funcs': {list: 'assertListEqual',
dict: 'assertDictEqual',
set: 'assertSetEqual',
frozenset: 'assertSetEqual',
tuple: 'assertTupleEqual',
unicode: 'assertMultiLineEqual'},
'func': <function integration.aisle_to_num>,
'missing': None,
'name': None}
In [10]: b = i.AisleValues('test_spaces', customername='peacock', missing='nan')
In [11]: b.__dict__
Out[12]:
{'_cleanups': [],
'_resultForDoCleanups': None,
'_testMethodDoc': '\n Test that parsing function produces the same\n value regardless of spacing present in input\n ',
'_testMethodName': 'test_spaces',
'_type_equality_funcs': {list: 'assertListEqual',
dict: 'assertDictEqual',
set: 'assertSetEqual',
frozenset: 'assertSetEqual',
tuple: 'assertTupleEqual',
unicode: 'assertMultiLineEqual'},
'func': <function integration.aisle_to_num>,
'missing': 'nan',
'name': 'peacock'}
In [13]: c = i.PeacockAisles('test_spaces')
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-12-d4bba181b94e> in <module>()
----> 1 c = i.PeacockAisles('test_spaces')
/path/to/python.py in __init__(self, testname)
89
90 def __init__(self, testname):
---> 91 super(AisleValues, self).__init__(testname, customername='peacock', missing='nan')
92 pprint(self.__dict__)
93
TypeError: __init__() got an unexpected keyword argument 'customername'
So what's the deal? Thanks!

You're not calling super correctly. When you name a class in the super call, it should be the current class, not a base class (unless you really know what you're doing and you want to skip the base class's implementation too, in favor of a "grandparent" class).
Your current code has AisleValues.__init__ calling unittest.TestCase.__init__, bypassing CustomerTestCase.__init__. It happens to work because CustomerTestCase.__init__ doesn't do anything useful (you could delete it with no effect), but that's just luck. When PeacockAisles.__init__ calls CustomerTestCase.__init__ (bypassing AisleValues.__init__), it fails because the grandparent class doesn't allow all the same arguments as its child.
You want:
class AisleValues(CustomerTestCase):
"""Base class for testing aisle values"""
def __init__(self, testname, customername=None, missing=None):
super(AisleValues, self).__init__(testname) # change the class named here
...
And:
class PeacockAisles(AisleValues):
"""Peacock aisle parsing test cases"""
def __init__(self, testname):
super(PeacockAisles, self).__init__(testname, customername='peacock', missing='nan')
... # and on the previous line

Related

Python inheritance: confusing arg with kwarg

If I run the following code:
class A(object) :
def __init__(self, x, y, z=3.0):
self.x = x
self.y = y
self.z = z
class B(A):
def __init__(self, a, b, c="c", *args, **kwargs):
super(B, self).__init__(*args, **kwargs)
self.a = a
self.b = b
self.c = c
if __name__=="__main__":
thing = B("a", "b", 1, 2)
print thing.x # expect 1
print thing.y # expect 2
print thing.z # expect 3
print thing.a # expect a
print thing.b # expect b
print thing.c # expect c
Instead I get :
Traceback (most recent call last):
File "H:/Python/Random Scripts/python_inheritance.py", line 23, in <module>
thing = B(1,2,"a","b")
File "H:/Python/Random Scripts/python_inheritance.py", line 15, in __init__
super(B, self).__init__(*args, **kwargs)
TypeError: __init__() takes at least 3 arguments (2 given)
It seems like python is parsing the third argument "a" as the kwarg argment c instead of as an arg. How do I get the behaviour that I expect?
I can obviously do :
class B(A):
def __init__(self, a, b, *args, **kwargs):
self.c = kwargs.pop("c", "c")
super(B, self).__init__(*args, **kwargs)
self.a = a
self.b = b
but it seems in every way horrible.
Here are two lines from your code, aligned to show which value is assigned to each name:
def __init__(self, a, b, c="c", *args, **kwargs):
thing = B("a", "b", 1, 2)
As you can see, the 1 is assigned to c, leaving only one argument in the variadic args list. The problem is that there are not two "classes" of argument in the way that you assume: whereas you call c a "kwarg argument" it isn't really defined as such, any more than a is. Both could be addressed by calling B(b="b", **some_dict) assuming some_dict had both an 'a' and a 'c' entry. Instead of a definitional dichotomy between args and kwargs, there are just arguments that have specified default values, and arguments that do not.
I think you're right that kwargs.pop() is your best bet. My code is littered with such "horrible" examples.

ValueError: This solver needs samples of at least 2 classes in the data, but the data contains only one class: 0.0

I have applied Logistic Regression on train set after splitting the data set into test and train sets, but I got the above error. I tried to work it out, and when i tried to print my response vector y_train in the console it prints integer values like 0 or 1. But when i wrote it into a file I found the values were float numbers like 0.0 and 1.0. If thats the problem, how can I over come it.
lenreg = LogisticRegression()
print y_train[0:10]
y_train.to_csv(path='ytard.csv')
lenreg.fit(X_train, y_train)
y_pred = lenreg.predict(X_test)
print metics.accuracy_score(y_test, y_pred)
StrackTrace is as follows,
Traceback (most recent call last):
File "/home/amey/prog/pd.py", line 82, in <module>
lenreg.fit(X_train, y_train)
File "/usr/lib/python2.7/dist-packages/sklearn/linear_model/logistic.py", line 1154, in fit
self.max_iter, self.tol, self.random_state)
File "/usr/lib/python2.7/dist-packages/sklearn/svm/base.py", line 885, in _fit_liblinear
" class: %r" % classes_[0])
ValueError: This solver needs samples of at least 2 classes in the data, but the data contains only one class: 0.0
Meanwhile I've gone across the link which was unanswered. Is there a solution.
The problem here is that your y_train vector, for whatever reason, only has zeros. It is actually not your fault, and its kind of a bug ( I think ). The classifier needs 2 classes or else it throws this error.
It makes sense. If your y_train vector only has zeros, ( ie only 1 class ), then the classifier doesn't really need to do any work, since all predictions should just be the one class.
In my opinion the classifier should still complete and just predict the one class ( all zeros in this case ) and then throw a warning, but it doesn't. It throws the error in stead.
A way to check for this condition is like this:
lenreg = LogisticRegression()
print y_train[0:10]
y_train.to_csv(path='ytard.csv')
if len(np.sum(y_train)) in [len(y_train),0]:
print "all one class"
#do something else
else:
#OK to proceed
lenreg.fit(X_train, y_train)
y_pred = lenreg.predict(X_test)
print metics.accuracy_score(y_test, y_pred)
TO overcome the problem more easily i would recommend just including more samples in you test set, like 100 or 1000 instead of 10.
I had the same problem using learning_curve:
train_sizes, train_scores, test_scores = learning_curve(estimator,
X, y, cv=cv, n_jobs=n_jobs, train_sizes=train_sizes,
scoring="f1", random_state=RANDOM_SEED, shuffle=True)
add the suffle parameter that will randomize the sets.
This doesn't prevent error from happening but it's a way to increase the chances to have both classes in subsets used by the function.
I found it to be because of only 1's or 0's wound up in my y_test since my sample size was really small. Try chaning your test_size value.
# python3
import numpy as np
from sklearn.svm import LinearSVC
def upgrade_to_work_with_single_class(SklearnPredictor):
class UpgradedPredictor(SklearnPredictor):
def __init__(self, *args, **kwargs):
self._single_class_label = None
super().__init__(*args, **kwargs)
#staticmethod
def _has_only_one_class(y):
return len(np.unique(y)) == 1
def _fitted_on_single_class(self):
return self._single_class_label is not None
def fit(self, X, y=None):
if self._has_only_one_class(y):
self._single_class_label = y[0]
else:
super().fit(X, y)
return self
def predict(self, X):
if self._fitted_on_single_class():
return np.full(X.shape[0], self._single_class_label)
else:
return super().predict(X)
return UpgradedPredictor
LinearSVC = upgrade_to_work_with_single_class(LinearSVC)
or hard-way (more right):
import numpy as np
from sklearn.svm import LinearSVC
from copy import deepcopy, copy
from functools import wraps
def copy_class(cls):
copy_cls = type(f'{cls.__name__}', cls.__bases__, dict(cls.__dict__))
for name, attr in cls.__dict__.items():
try:
hash(attr)
except TypeError:
# Assume lack of __hash__ implies mutability. This is NOT
# a bullet proof assumption but good in many cases.
setattr(copy_cls, name, deepcopy(attr))
return copy_cls
def upgrade_to_work_with_single_class(SklearnPredictor):
SklearnPredictor = copy_class(SklearnPredictor)
original_init = deepcopy(SklearnPredictor.__init__)
original_fit = deepcopy(SklearnPredictor.fit)
original_predict = deepcopy(SklearnPredictor.predict)
#staticmethod
def _has_only_one_class(y):
return len(np.unique(y)) == 1
def _fitted_on_single_class(self):
return self._single_class_label is not None
#wraps(SklearnPredictor.__init__)
def new_init(self, *args, **kwargs):
self._single_class_label = None
original_init(self, *args, **kwargs)
#wraps(SklearnPredictor.fit)
def new_fit(self, X, y=None):
if self._has_only_one_class(y):
self._single_class_label = y[0]
else:
original_fit(self, X, y)
return self
#wraps(SklearnPredictor.predict)
def new_predict(self, X):
if self._fitted_on_single_class():
return np.full(X.shape[0], self._single_class_label)
else:
return original_predict(self, X)
setattr(SklearnPredictor, '_has_only_one_class', _has_only_one_class)
setattr(SklearnPredictor, '_fitted_on_single_class', _fitted_on_single_class)
SklearnPredictor.__init__ = new_init
SklearnPredictor.fit = new_fit
SklearnPredictor.predict = new_predict
return SklearnPredictor
LinearSVC = upgrade_to_work_with_single_class(LinearSVC)
You can find the indexes of the first (or any) occurrence of each of the classes and concatenate them on top of the arrays and delete them from their original positions, that way there will be at least one instance of each class in the training set.
This error related to the dataset you are using, the dataset contains a class for example 1/benign, whereas it must contain two classes 1 and 0 or Benign and Attack.

Execute 3 attribute class python

Here's my code
class Word:
def motivation(self):
return 'Dont Give up'
def __call__(self, x):
return 'love'
def __call__(self, c):
return 'hate'
def dude(self):
return 'dude'
def brother(self):
return 'brother'
word = Word()
with this code i must produce like this
>>> word.motivation.dude
'Dont Give Up Dude'
And must be like this too
>>> word.motivation.brother
'Dont Give Up Brother'
if i execute with this script, the output get error
AttributeError: 'Function' object has no attribute 'dude'
and
AttributeError: 'Function' object has no attribute 'brother'

Assert that two lists of objects are equal in django testing

Is there a way to check that two lists of objects are equal in django tests.
lets say I have some model:
class Tag(models.Model):
slug = models.SlugField(max_length=50, unique=True)
def __unicode__(self):
return self.slug
and I run this simple test:
def test_equal_list_fail(self):
tag_list = []
for tag in ['a', 'b', 'c']:
tag_list.append(Tag.objects.create(slug=tag))
tags = Tag.objects.all()
self.assertEqual(tag_list, tags)
this fails with:
======================================================================
FAIL: test_equal_list_fail (userAccount.tests.ProfileTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "path/to/tests.py", line 155, in test_equal_list_fail
self.assertEqual(tag_list, tags)
AssertionError: [<Tag: a>, <Tag: b>, <Tag: c>] != [<Tag: a>, <Tag: b>, <Tag: c>]
----------------------------------------------------------------------
this will work:
def test_equal_list_passes(self):
tag_list = []
for tag in ['a', 'b', 'c']:
tag_list.append(Tag.objects.create(slug=tag))
tags = Tag.objects.all()
for tag_set in zip(tags, tag_list):
self.assertEqual(*tag_set)
However, This fails:
def test_equal_list_fail(self):
tag_list = []
for tag in ['a', 'b', 'c']:
tag_list.append(Tag.objects.create(slug=tag))
tags = Tag.objects.all()
for tag_set in zip(tags, tag_list):
print "\n"
print tag_set[0].slug + "'s pk is %s" % tag_set[0].pk
print tag_set[1].slug + "'s pk is %s" % tag_set[1].pk
print "\n"
self.assertIs(*tag_set)
with:
Creating test database for alias 'default'...
.......
a's pk is 1
a's pk is 1
F.
======================================================================
FAIL: test_equal_list_fail (userAccount.tests.ProfileTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "path/to/tests.py", line 160, in test_equal_list_fail
self.assertIs(*tag_set)
AssertionError: <Tag: a> is not <Tag: a>
Is this expected behavior?
Edit in response to comment
This type of comparison works:
class Obj:
def __init__(self, x):
self.x = x
>>> one = Obj(1)
>>> two = Obj(2)
>>> a = [one, two]
>>> b = [one, two]
>>> a == b
True
Why is the test failing for the other arrays?
To test two lists
use: assertSequenceEqual
Because, in this case, tags = Tag.objects.all() generates a django.db.models.query.QuerySet where as tag_list.append(...) creates a list.
Other options in different situations are:
assertListEqual(a, b)
assertTupleEqual(a, b)
assertSetEqual(a, b)
assertDictEqual(a, b)
Why <Tag: a> is not <Tag: a>
The tags are the same model, but they've been loaded into different places in memory
for tag_set in zip(tags, tag_list):
print "\n"
print tag_set[0].slug + "'s pk is %s" % tag_set[0].pk + ' id is: ' + id(tag_set[0])
print tag_set[1].slug + "'s pk is %s" % tag_set[1].pk + ' id is: ' + id(tag_set[1])
print "\n"
self.assertIs(*tag_set)
returns
.......
a's pk is 1 id is: 4522000208
a's pk is 1 id is: 4522228112
F.
Therefore, is will retrun False
I think what you want to test is if the tags created have the same slugs as those in your test list.
For that, fetch only the slug as a list with values_list, and then compare that:
assertEqual(Tag.objects.values_list('slug', flat=True), ['a','b','c'])
I have to say, this isn't quite a useful test because you are checking django orm functionality, which has already been tested quite well.
Your tests should check for specifics of your own application.

How do I put docstrings on Enums?

Python 3.4 has a new enum module and Enum data type. If you are unable to switch to 3.4 yet, Enum has been backported.
Since Enum members support docstrings, as pretty much all python objects do, I would like to set them. Is there an easy way to do that?
Yes there is, and it's my favorite recipe so far. As a bonus, one does not have to specify the integer value either. Here's an example:
class AddressSegment(AutoEnum):
misc = "not currently tracked"
ordinal = "N S E W NE NW SE SW"
secondary = "apt bldg floor etc"
street = "st ave blvd etc"
You might ask why I don't just have "N S E W NE NW SE SW" be the value of ordinal? Because when I get its repr seeing <AddressSegment.ordinal: 'N S E W NE NW SE SW'> gets a bit clunky, but having that information readily available in the docstring is a good compromise.
Here's the recipe for the Enum:
class AutoEnum(enum.Enum):
"""
Automatically numbers enum members starting from 1.
Includes support for a custom docstring per member.
"""
#
def __new__(cls, *args):
"""Ignores arguments (will be handled in __init__."""
value = len(cls) + 1
obj = object.__new__(cls)
obj._value_ = value
return obj
#
def __init__(self, *args):
"""Can handle 0 or 1 argument; more requires a custom __init__.
0 = auto-number w/o docstring
1 = auto-number w/ docstring
2+ = needs custom __init__
"""
if len(args) == 1 and isinstance(args[0], (str, unicode)):
self.__doc__ = args[0]
elif args:
raise TypeError('%s not dealt with -- need custom __init__' % (args,))
And in use:
>>> list(AddressSegment)
[<AddressSegment.ordinal: 1>, <AddressSegment.secondary: 2>, <AddressSegment.misc: 3>, <AddressSegment.street: 4>]
>>> AddressSegment.secondary
<AddressSegment.secondary: 2>
>>> AddressSegment.secondary.__doc__
'apt bldg floor etc'
The reason I handle the arguments in __init__ instead of in __new__ is to make subclassing AutoEnum easier should I want to extend it further.
Anyone arriving here as a google search:
For many IDE's now in 2022, the following will populate intellisense:
class MyEnum(Enum):
"""
MyEnum purpose and general doc string
"""
VALUE = "Value"
"""
This is the Value selection. Use this for Values
"""
BUILD = "Build"
"""
This is the Build selection. Use this for Buildings
"""
Example in VSCode:
This does not directly answer the question, but I wanted to add a more robust version of #Ethan Furman's AutoEnum class which uses the auto enum function.
The implementation below works with Pydantic and does fuzzy-matching of values to the corresponding enum type.
Usage:
In [2]: class Weekday(AutoEnum): ## Assume AutoEnum class has been defined.
...: Monday = auto()
...: Tuesday = auto()
...: Wednesday = auto()
...: Thursday = auto()
...: Friday = auto()
...: Saturday = auto()
...: Sunday = auto()
...:
In [3]: Weekday('MONDAY') ## Fuzzy matching: case-insensitive
Out[3]: Monday
In [4]: Weekday(' MO NDAY') ## Fuzzy matching: ignores extra spaces
Out[4]: Monday
In [5]: Weekday('_M_onDa y') ## Fuzzy matching: ignores underscores
Out[5]: Monday
In [6]: %timeit Weekday('_M_onDay') ## Fuzzy matching takes ~1 microsecond.
1.15 µs ± 10.9 ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each)
In [7]: %timeit Weekday.from_str('_M_onDay') ## You can further speedup matching using from_str (this is because _missing_ is not called)
736 ns ± 8.89 ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each)
In [8]: list(Weekday) ## Get all the enums
Out[8]: [Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday]
In [9]: Weekday.Monday.matches('Tuesday') ## Check if a string matches a particular enum value
Out[9]: False
In [10]: Weekday.matches_any('__TUESDAY__') ## Check if a string matches any enum
Out[10]: True
In [11]: Weekday.Tuesday is Weekday(' Tuesday') and Weekday.Tuesday == Weekday('_Tuesday_') ## `is` and `==` work as expected
Out[11]: True
In [12]: Weekday.Tuesday == 'Tuesday' ## Strings don't match enum values, because strings aren't enums!
Out[12]: False
In [13]: Weekday.convert_keys({ ## Convert matching dict keys to an enum. Similar: .convert_list, .convert_set
'monday': 'alice',
'tuesday': 'bob',
'not_wednesday': 'charles',
'THURSDAY ': 'denise',
})
Out[13]:
{Monday: 'alice',
Tuesday: 'bob',
'not_wednesday': 'charles',
Thursday: 'denise'}
The code for AutoEnum can be found below.
If you want to change the fuzzy-matching logic, then override the classmethod _normalize (e.g. returning the input unchanged in _normalize, will perform exact matching).
from typing import *
from enum import Enum, auto
class AutoEnum(str, Enum):
"""
Utility class which can be subclassed to create enums using auto().
Also provides utility methods for common enum operations.
"""
#classmethod
def _missing_(cls, enum_value: Any):
## Ref: https://stackoverflow.com/a/60174274/4900327
## This is needed to allow Pydantic to perform case-insensitive conversion to AutoEnum.
return cls.from_str(enum_value=enum_value, raise_error=True)
def _generate_next_value_(name, start, count, last_values):
return name
#property
def str(self) -> str:
return self.__str__()
def __repr__(self):
return self.__str__()
def __str__(self):
return self.name
def __hash__(self):
return hash(self.__class__.__name__ + '.' + self.name)
def __eq__(self, other):
return self is other
def __ne__(self, other):
return self is not other
def matches(self, enum_value: str) -> bool:
return self is self.from_str(enum_value, raise_error=False)
#classmethod
def matches_any(cls, enum_value: str) -> bool:
return cls.from_str(enum_value, raise_error=False) is not None
#classmethod
def does_not_match_any(cls, enum_value: str) -> bool:
return not cls.matches_any(enum_value)
#classmethod
def _initialize_lookup(cls):
if '_value2member_map_normalized_' not in cls.__dict__: ## Caching values for fast retrieval.
cls._value2member_map_normalized_ = {}
for e in list(cls):
normalized_e_name: str = cls._normalize(e.value)
if normalized_e_name in cls._value2member_map_normalized_:
raise ValueError(
f'Cannot register enum "{e.value}"; '
f'another enum with the same normalized name "{normalized_e_name}" already exists.'
)
cls._value2member_map_normalized_[normalized_e_name] = e
#classmethod
def from_str(cls, enum_value: str, raise_error: bool = True) -> Optional:
"""
Performs a case-insensitive lookup of the enum value string among the members of the current AutoEnum subclass.
:param enum_value: enum value string
:param raise_error: whether to raise an error if the string is not found in the enum
:return: an enum value which matches the string
:raises: ValueError if raise_error is True and no enum value matches the string
"""
if isinstance(enum_value, cls):
return enum_value
if enum_value is None and raise_error is False:
return None
if not isinstance(enum_value, str) and raise_error is True:
raise ValueError(f'Input should be a string; found type {type(enum_value)}')
cls._initialize_lookup()
enum_obj: Optional[AutoEnum] = cls._value2member_map_normalized_.get(cls._normalize(enum_value))
if enum_obj is None and raise_error is True:
raise ValueError(f'Could not find enum with value {enum_value}; available values are: {list(cls)}.')
return enum_obj
#classmethod
def _normalize(cls, x: str) -> str:
## Found to be faster than .translate() and re.sub() on Python 3.10.6
return str(x).replace(' ', '').replace('-', '').replace('_', '').lower()
#classmethod
def convert_keys(cls, d: Dict) -> Dict:
"""
Converts string dict keys to the matching members of the current AutoEnum subclass.
Leaves non-string keys untouched.
:param d: dict to transform
:return: dict with matching string keys transformed to enum values
"""
out_dict = {}
for k, v in d.items():
if isinstance(k, str) and cls.from_str(k, raise_error=False) is not None:
out_dict[cls.from_str(k, raise_error=False)] = v
else:
out_dict[k] = v
return out_dict
#classmethod
def convert_keys_to_str(cls, d: Dict) -> Dict:
"""
Converts dict keys of the current AutoEnum subclass to the matching string key.
Leaves other keys untouched.
:param d: dict to transform
:return: dict with matching keys of the current AutoEnum transformed to strings.
"""
out_dict = {}
for k, v in d.items():
if isinstance(k, cls):
out_dict[str(k)] = v
else:
out_dict[k] = v
return out_dict
#classmethod
def convert_values(
cls,
d: Union[Dict, Set, List, Tuple],
raise_error: bool = False
) -> Union[Dict, Set, List, Tuple]:
"""
Converts string values to the matching members of the current AutoEnum subclass.
Leaves non-string values untouched.
:param d: dict, set, list or tuple to transform.
:param raise_error: raise an error if unsupported type.
:return: data structure with matching string values transformed to enum values.
"""
if isinstance(d, dict):
return cls.convert_dict_values(d)
if isinstance(d, list):
return cls.convert_list(d)
if isinstance(d, tuple):
return tuple(cls.convert_list(d))
if isinstance(d, set):
return cls.convert_set(d)
if raise_error:
raise ValueError(f'Unrecognized data structure of type {type(d)}')
return d
#classmethod
def convert_dict_values(cls, d: Dict) -> Dict:
"""
Converts string dict values to the matching members of the current AutoEnum subclass.
Leaves non-string values untouched.
:param d: dict to transform
:return: dict with matching string values transformed to enum values
"""
out_dict = {}
for k, v in d.items():
if isinstance(v, str) and cls.from_str(v, raise_error=False) is not None:
out_dict[k] = cls.from_str(v, raise_error=False)
else:
out_dict[k] = v
return out_dict
#classmethod
def convert_list(cls, l: List) -> List:
"""
Converts string list itmes to the matching members of the current AutoEnum subclass.
Leaves non-string items untouched.
:param l: list to transform
:return: list with matching string items transformed to enum values
"""
out_list = []
for item in l:
if isinstance(item, str) and cls.matches_any(item):
out_list.append(cls.from_str(item))
else:
out_list.append(item)
return out_list
#classmethod
def convert_set(cls, s: Set) -> Set:
"""
Converts string list itmes to the matching members of the current AutoEnum subclass.
Leaves non-string items untouched.
:param s: set to transform
:return: set with matching string items transformed to enum values
"""
out_set = set()
for item in s:
if isinstance(item, str) and cls.matches_any(item):
out_set.add(cls.from_str(item))
else:
out_set.add(item)
return out_set
#classmethod
def convert_values_to_str(cls, d: Dict) -> Dict:
"""
Converts dict values of the current AutoEnum subclass to the matching string value.
Leaves other values untouched.
:param d: dict to transform
:return: dict with matching values of the current AutoEnum transformed to strings.
"""
out_dict = {}
for k, v in d.items():
if isinstance(v, cls):
out_dict[k] = str(v)
else:
out_dict[k] = v
return out_dict
Functions and classes have docstrings, but most objects don't and do not even need them at all. There is no native docstring syntax for instance attributes, as they can be described exhaustively in the classes' docstring, which is also what I recommend you to do. Instances of classes normally also don't have their own docstrings, and enum members are nothing more than that.
Sure enough you could add a docstring to almost anything. Actually you can, indeed, add anything to almost anything, as this is the way python was designed. But it is neither useful nor clean, and even what #Ethan Furman posted seems like way to much overhead just for adding a docstring to a static property.
Long story short, even though you might not like it at first:
Just don't do it and go with your enum's docstring. It is more than enough to explain the meaning of its members.