Skip to content

Commit c4dc5a6

Browse files
gh-111181: Fix enum doctests (GH-111180)
Co-authored-by: Ethan Furman <[email protected]>
1 parent cd6e0a0 commit c4dc5a6

File tree

3 files changed

+22
-15
lines changed

3 files changed

+22
-15
lines changed

Doc/howto/enum.rst

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,7 @@ Dataclass support
483483
When inheriting from a :class:`~dataclasses.dataclass`,
484484
the :meth:`~Enum.__repr__` omits the inherited class' name. For example::
485485

486+
>>> from dataclasses import dataclass, field
486487
>>> @dataclass
487488
... class CreatureDataMixin:
488489
... size: str
@@ -527,7 +528,8 @@ It is possible to modify how enum members are pickled/unpickled by defining
527528
:meth:`__reduce_ex__` in the enumeration class. The default method is by-value,
528529
but enums with complicated values may want to use by-name::
529530

530-
>>> class MyEnum(Enum):
531+
>>> import enum
532+
>>> class MyEnum(enum.Enum):
531533
... __reduce_ex__ = enum.pickle_by_enum_name
532534

533535
.. note::
@@ -770,7 +772,7 @@ be combined with them (but may lose :class:`IntFlag` membership::
770772
>>> Perm.X | 4
771773
<Perm.R|X: 5>
772774

773-
>>> Perm.X | 8
775+
>>> Perm.X + 8
774776
9
775777

776778
.. note::
@@ -1435,8 +1437,9 @@ alias::
14351437
... GRENE = 2
14361438
...
14371439
Traceback (most recent call last):
1438-
...
1440+
...
14391441
ValueError: aliases not allowed in DuplicateFreeEnum: 'GRENE' --> 'GREEN'
1442+
Error calling __set_name__ on '_proto_member' instance 'GRENE' in 'Color'
14401443

14411444
.. note::
14421445

Lib/enum.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1189,14 +1189,13 @@ def __str__(self):
11891189

11901190
def __dir__(self):
11911191
"""
1192-
Returns all members and all public methods
1192+
Returns public methods and other interesting attributes.
11931193
"""
1194-
if self.__class__._member_type_ is object:
1195-
interesting = set(['__class__', '__doc__', '__eq__', '__hash__', '__module__', 'name', 'value'])
1196-
else:
1194+
interesting = set()
1195+
if self.__class__._member_type_ is not object:
11971196
interesting = set(object.__dir__(self))
11981197
for name in getattr(self, '__dict__', []):
1199-
if name[0] != '_':
1198+
if name[0] != '_' and name not in self._member_map_:
12001199
interesting.add(name)
12011200
for cls in self.__class__.mro():
12021201
for name, obj in cls.__dict__.items():
@@ -1209,7 +1208,7 @@ def __dir__(self):
12091208
else:
12101209
# in case it was added by `dir(self)`
12111210
interesting.discard(name)
1212-
else:
1211+
elif name not in self._member_map_:
12131212
interesting.add(name)
12141213
names = sorted(
12151214
set(['__class__', '__doc__', '__eq__', '__hash__', '__module__'])

Lib/test/test_enum.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,27 @@
1818
from io import StringIO
1919
from pickle import dumps, loads, PicklingError, HIGHEST_PROTOCOL
2020
from test import support
21-
from test.support import ALWAYS_EQ
21+
from test.support import ALWAYS_EQ, REPO_ROOT
2222
from test.support import threading_helper
2323
from datetime import timedelta
2424

2525
python_version = sys.version_info[:2]
2626

2727
def load_tests(loader, tests, ignore):
2828
tests.addTests(doctest.DocTestSuite(enum))
29-
if os.path.exists('Doc/library/enum.rst'):
29+
30+
lib_tests = os.path.join(REPO_ROOT, 'Doc/library/enum.rst')
31+
if os.path.exists(lib_tests):
3032
tests.addTests(doctest.DocFileSuite(
31-
'../../Doc/library/enum.rst',
33+
lib_tests,
34+
module_relative=False,
3235
optionflags=doctest.ELLIPSIS|doctest.NORMALIZE_WHITESPACE,
3336
))
34-
if os.path.exists('Doc/howto/enum.rst'):
37+
howto_tests = os.path.join(REPO_ROOT, 'Doc/howto/enum.rst')
38+
if os.path.exists(howto_tests):
3539
tests.addTests(doctest.DocFileSuite(
36-
'../../Doc/howto/enum.rst',
40+
howto_tests,
41+
module_relative=False,
3742
optionflags=doctest.ELLIPSIS|doctest.NORMALIZE_WHITESPACE,
3843
))
3944
return tests
@@ -5166,7 +5171,7 @@ def member_dir(member):
51665171
allowed.add(name)
51675172
else:
51685173
allowed.discard(name)
5169-
else:
5174+
elif name not in member._member_map_:
51705175
allowed.add(name)
51715176
return sorted(allowed)
51725177

0 commit comments

Comments
 (0)