Skip to content

Commit ec00397

Browse files
miss-islingtonsobolevnethanfurman
authored
[3.12] gh-111181: Fix enum doctests (GH-111180) (GH-111518)
gh-111181: Fix enum doctests (GH-111180) (cherry picked from commit c4dc5a6) Co-authored-by: Nikita Sobolev <[email protected]> Co-authored-by: Ethan Furman <[email protected]>
1 parent 4f619e8 commit ec00397

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
@@ -1217,14 +1217,13 @@ def __str__(self):
12171217

12181218
def __dir__(self):
12191219
"""
1220-
Returns all members and all public methods
1220+
Returns public methods and other interesting attributes.
12211221
"""
1222-
if self.__class__._member_type_ is object:
1223-
interesting = set(['__class__', '__doc__', '__eq__', '__hash__', '__module__', 'name', 'value'])
1224-
else:
1222+
interesting = set()
1223+
if self.__class__._member_type_ is not object:
12251224
interesting = set(object.__dir__(self))
12261225
for name in getattr(self, '__dict__', []):
1227-
if name[0] != '_':
1226+
if name[0] != '_' and name not in self._member_map_:
12281227
interesting.add(name)
12291228
for cls in self.__class__.mro():
12301229
for name, obj in cls.__dict__.items():
@@ -1237,7 +1236,7 @@ def __dir__(self):
12371236
else:
12381237
# in case it was added by `dir(self)`
12391238
interesting.discard(name)
1240-
else:
1239+
elif name not in self._member_map_:
12411240
interesting.add(name)
12421241
names = sorted(
12431242
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
@@ -5127,7 +5132,7 @@ def member_dir(member):
51275132
allowed.add(name)
51285133
else:
51295134
allowed.discard(name)
5130-
else:
5135+
elif name not in member._member_map_:
51315136
allowed.add(name)
51325137
return sorted(allowed)
51335138

0 commit comments

Comments
 (0)