When inheriting from a :class:`~dataclasses.dataclass`,
the :meth:`~Enum.__repr__` omits the inherited class' name. For example::
+ >>> from dataclasses import dataclass, field
>>> @dataclass
... class CreatureDataMixin:
... size: str
:meth:`__reduce_ex__` in the enumeration class. The default method is by-value,
but enums with complicated values may want to use by-name::
- >>> class MyEnum(Enum):
+ >>> import enum
+ >>> class MyEnum(enum.Enum):
... __reduce_ex__ = enum.pickle_by_enum_name
.. note::
>>> Perm.X | 4
<Perm.R|X: 5>
- >>> Perm.X | 8
+ >>> Perm.X + 8
9
.. note::
... GRENE = 2
...
Traceback (most recent call last):
- ...
+ ...
ValueError: aliases not allowed in DuplicateFreeEnum: 'GRENE' --> 'GREEN'
+ Error calling __set_name__ on '_proto_member' instance 'GRENE' in 'Color'
.. note::
def __dir__(self):
"""
- Returns all members and all public methods
+ Returns public methods and other interesting attributes.
"""
- if self.__class__._member_type_ is object:
- interesting = set(['__class__', '__doc__', '__eq__', '__hash__', '__module__', 'name', 'value'])
- else:
+ interesting = set()
+ if self.__class__._member_type_ is not object:
interesting = set(object.__dir__(self))
for name in getattr(self, '__dict__', []):
- if name[0] != '_':
+ if name[0] != '_' and name not in self._member_map_:
interesting.add(name)
for cls in self.__class__.mro():
for name, obj in cls.__dict__.items():
else:
# in case it was added by `dir(self)`
interesting.discard(name)
- else:
+ elif name not in self._member_map_:
interesting.add(name)
names = sorted(
set(['__class__', '__doc__', '__eq__', '__hash__', '__module__'])
from io import StringIO
from pickle import dumps, loads, PicklingError, HIGHEST_PROTOCOL
from test import support
-from test.support import ALWAYS_EQ
+from test.support import ALWAYS_EQ, REPO_ROOT
from test.support import threading_helper
from datetime import timedelta
def load_tests(loader, tests, ignore):
tests.addTests(doctest.DocTestSuite(enum))
- if os.path.exists('Doc/library/enum.rst'):
+
+ lib_tests = os.path.join(REPO_ROOT, 'Doc/library/enum.rst')
+ if os.path.exists(lib_tests):
tests.addTests(doctest.DocFileSuite(
- '../../Doc/library/enum.rst',
+ lib_tests,
+ module_relative=False,
optionflags=doctest.ELLIPSIS|doctest.NORMALIZE_WHITESPACE,
))
- if os.path.exists('Doc/howto/enum.rst'):
+ howto_tests = os.path.join(REPO_ROOT, 'Doc/howto/enum.rst')
+ if os.path.exists(howto_tests):
tests.addTests(doctest.DocFileSuite(
- '../../Doc/howto/enum.rst',
+ howto_tests,
+ module_relative=False,
optionflags=doctest.ELLIPSIS|doctest.NORMALIZE_WHITESPACE,
))
return tests
allowed.add(name)
else:
allowed.discard(name)
- else:
+ elif name not in member._member_map_:
allowed.add(name)
return sorted(allowed)