Skip to content

Commit 4a61169

Browse files
[3.11] gh-111181: Fix enum doctests (GH-111180) (GH-111617)
gh-111181: Fix enum doctests (GH-111180) Co-authored-by: Ethan Furman <[email protected]> (cherry picked from commit c4dc5a6) Co-authored-by: Nikita Sobolev <[email protected]>
1 parent a106f61 commit 4a61169

File tree

3 files changed

+21
-15
lines changed

3 files changed

+21
-15
lines changed

Doc/howto/enum.rst

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,8 @@ It is possible to modify how enum members are pickled/unpickled by defining
494494
:meth:`__reduce_ex__` in the enumeration class. The default method is by-value,
495495
but enums with complicated values may want to use by-name::
496496

497-
>>> class MyEnum(Enum):
497+
>>> import enum
498+
>>> class MyEnum(enum.Enum):
498499
... __reduce_ex__ = enum.pickle_by_enum_name
499500

500501
.. note::
@@ -736,7 +737,7 @@ be combined with them (but may lose :class:`IntFlag` membership::
736737
>>> Perm.X | 4
737738
<Perm.R|X: 5>
738739

739-
>>> Perm.X | 8
740+
>>> Perm.X + 8
740741
9
741742

742743
.. note::
@@ -1398,8 +1399,9 @@ alias::
13981399
... GRENE = 2
13991400
...
14001401
Traceback (most recent call last):
1401-
...
1402+
...
14021403
ValueError: aliases not allowed in DuplicateFreeEnum: 'GRENE' --> 'GREEN'
1404+
Error calling __set_name__ on '_proto_member' instance 'GRENE' in 'Color'
14031405

14041406
.. note::
14051407

Lib/enum.py

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

11981198
def __dir__(self):
11991199
"""
1200-
Returns all members and all public methods
1200+
Returns public methods and other interesting attributes.
12011201
"""
1202-
if self.__class__._member_type_ is object:
1203-
interesting = set(['__class__', '__doc__', '__eq__', '__hash__', '__module__', 'name', 'value'])
1204-
else:
1202+
interesting = set()
1203+
if self.__class__._member_type_ is not object:
12051204
interesting = set(object.__dir__(self))
12061205
for name in getattr(self, '__dict__', []):
1207-
if name[0] != '_':
1206+
if name[0] != '_' and name not in self._member_map_:
12081207
interesting.add(name)
12091208
for cls in self.__class__.mro():
12101209
for name, obj in cls.__dict__.items():
@@ -1217,7 +1216,7 @@ def __dir__(self):
12171216
else:
12181217
# in case it was added by `dir(self)`
12191218
interesting.discard(name)
1220-
else:
1219+
elif name not in self._member_map_:
12211220
interesting.add(name)
12221221
names = sorted(
12231222
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,7 +18,7 @@
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 textwrap import dedent
2424
from datetime import timedelta
@@ -27,14 +27,19 @@
2727

2828
def load_tests(loader, tests, ignore):
2929
tests.addTests(doctest.DocTestSuite(enum))
30-
if os.path.exists('Doc/library/enum.rst'):
30+
31+
lib_tests = os.path.join(REPO_ROOT, 'Doc/library/enum.rst')
32+
if os.path.exists(lib_tests):
3133
tests.addTests(doctest.DocFileSuite(
32-
'../../Doc/library/enum.rst',
34+
lib_tests,
35+
module_relative=False,
3336
optionflags=doctest.ELLIPSIS|doctest.NORMALIZE_WHITESPACE,
3437
))
35-
if os.path.exists('Doc/howto/enum.rst'):
38+
howto_tests = os.path.join(REPO_ROOT, 'Doc/howto/enum.rst')
39+
if os.path.exists(howto_tests):
3640
tests.addTests(doctest.DocFileSuite(
37-
'../../Doc/howto/enum.rst',
41+
howto_tests,
42+
module_relative=False,
3843
optionflags=doctest.ELLIPSIS|doctest.NORMALIZE_WHITESPACE,
3944
))
4045
return tests
@@ -4874,7 +4879,7 @@ def member_dir(member):
48744879
allowed.add(name)
48754880
else:
48764881
allowed.discard(name)
4877-
else:
4882+
elif name not in member._member_map_:
48784883
allowed.add(name)
48794884
return sorted(allowed)
48804885

0 commit comments

Comments
 (0)