Skip to content

Commit

Permalink
[3.11] gh-111181: Fix enum doctests (GH-111180) (GH-111617)
Browse files Browse the repository at this point in the history
gh-111181: Fix enum doctests (GH-111180)

Co-authored-by: Ethan Furman <ethan@stoneleaf.us>
(cherry picked from commit c4dc5a6)

Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
  • Loading branch information
ethanfurman and sobolevn authored Nov 3, 2023
1 parent a106f61 commit 4a61169
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 15 deletions.
8 changes: 5 additions & 3 deletions Doc/howto/enum.rst
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,8 @@ It is possible to modify how enum members are pickled/unpickled by defining
: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::
Expand Down Expand Up @@ -736,7 +737,7 @@ be combined with them (but may lose :class:`IntFlag` membership::
>>> Perm.X | 4
<Perm.R|X: 5>

>>> Perm.X | 8
>>> Perm.X + 8
9

.. note::
Expand Down Expand Up @@ -1398,8 +1399,9 @@ alias::
... 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::

Expand Down
11 changes: 5 additions & 6 deletions Lib/enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -1197,14 +1197,13 @@ def __str__(self):

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():
Expand All @@ -1217,7 +1216,7 @@ def __dir__(self):
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__'])
Expand Down
17 changes: 11 additions & 6 deletions Lib/test/test_enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
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 textwrap import dedent
from datetime import timedelta
Expand All @@ -27,14 +27,19 @@

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
Expand Down Expand Up @@ -4874,7 +4879,7 @@ def member_dir(member):
allowed.add(name)
else:
allowed.discard(name)
else:
elif name not in member._member_map_:
allowed.add(name)
return sorted(allowed)

Expand Down

0 comments on commit 4a61169

Please sign in to comment.