Skip to content

gh-111181: Fix enum doctests #111180

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions Doc/howto/enum.rst
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,7 @@ Dataclass support
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
Expand Down Expand Up @@ -527,7 +528,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 @@ -770,7 +772,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 @@ -1435,8 +1437,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 @@ -1189,14 +1189,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 @@ -1209,7 +1208,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,22 +18,27 @@
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

python_version = sys.version_info[:2]

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 @@ -5166,7 +5171,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