gh-111181: Fix enum doctests (GH-111180)

Co-authored-by: Ethan Furman <ethan@stoneleaf.us>
This commit is contained in:
Nikita Sobolev 2023-10-30 22:56:29 +03:00 committed by GitHub
parent cd6e0a04a1
commit c4dc5a6ae8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 15 deletions

View File

@ -483,6 +483,7 @@ Dataclass support
When inheriting from a :class:`~dataclasses.dataclass`, When inheriting from a :class:`~dataclasses.dataclass`,
the :meth:`~Enum.__repr__` omits the inherited class' name. For example:: the :meth:`~Enum.__repr__` omits the inherited class' name. For example::
>>> from dataclasses import dataclass, field
>>> @dataclass >>> @dataclass
... class CreatureDataMixin: ... class CreatureDataMixin:
... size: str ... size: str
@ -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, :meth:`__reduce_ex__` in the enumeration class. The default method is by-value,
but enums with complicated values may want to use by-name:: 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 ... __reduce_ex__ = enum.pickle_by_enum_name
.. note:: .. note::
@ -770,7 +772,7 @@ be combined with them (but may lose :class:`IntFlag` membership::
>>> Perm.X | 4 >>> Perm.X | 4
<Perm.R|X: 5> <Perm.R|X: 5>
>>> Perm.X | 8 >>> Perm.X + 8
9 9
.. note:: .. note::
@ -1437,6 +1439,7 @@ alias::
Traceback (most recent call last): Traceback (most recent call last):
... ...
ValueError: aliases not allowed in DuplicateFreeEnum: 'GRENE' --> 'GREEN' ValueError: aliases not allowed in DuplicateFreeEnum: 'GRENE' --> 'GREEN'
Error calling __set_name__ on '_proto_member' instance 'GRENE' in 'Color'
.. note:: .. note::

View File

@ -1189,14 +1189,13 @@ class Enum(metaclass=EnumType):
def __dir__(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()
interesting = set(['__class__', '__doc__', '__eq__', '__hash__', '__module__', 'name', 'value']) if self.__class__._member_type_ is not object:
else:
interesting = set(object.__dir__(self)) interesting = set(object.__dir__(self))
for name in getattr(self, '__dict__', []): for name in getattr(self, '__dict__', []):
if name[0] != '_': if name[0] != '_' and name not in self._member_map_:
interesting.add(name) interesting.add(name)
for cls in self.__class__.mro(): for cls in self.__class__.mro():
for name, obj in cls.__dict__.items(): for name, obj in cls.__dict__.items():
@ -1209,7 +1208,7 @@ class Enum(metaclass=EnumType):
else: else:
# in case it was added by `dir(self)` # in case it was added by `dir(self)`
interesting.discard(name) interesting.discard(name)
else: elif name not in self._member_map_:
interesting.add(name) interesting.add(name)
names = sorted( names = sorted(
set(['__class__', '__doc__', '__eq__', '__hash__', '__module__']) set(['__class__', '__doc__', '__eq__', '__hash__', '__module__'])

View File

@ -18,7 +18,7 @@ from enum import member, nonmember, _iter_bits_lsb
from io import StringIO from io import StringIO
from pickle import dumps, loads, PicklingError, HIGHEST_PROTOCOL from pickle import dumps, loads, PicklingError, HIGHEST_PROTOCOL
from test import support 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 test.support import threading_helper
from datetime import timedelta from datetime import timedelta
@ -26,14 +26,19 @@ python_version = sys.version_info[:2]
def load_tests(loader, tests, ignore): def load_tests(loader, tests, ignore):
tests.addTests(doctest.DocTestSuite(enum)) 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( tests.addTests(doctest.DocFileSuite(
'../../Doc/library/enum.rst', lib_tests,
module_relative=False,
optionflags=doctest.ELLIPSIS|doctest.NORMALIZE_WHITESPACE, 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( tests.addTests(doctest.DocFileSuite(
'../../Doc/howto/enum.rst', howto_tests,
module_relative=False,
optionflags=doctest.ELLIPSIS|doctest.NORMALIZE_WHITESPACE, optionflags=doctest.ELLIPSIS|doctest.NORMALIZE_WHITESPACE,
)) ))
return tests return tests
@ -5166,7 +5171,7 @@ def member_dir(member):
allowed.add(name) allowed.add(name)
else: else:
allowed.discard(name) allowed.discard(name)
else: elif name not in member._member_map_:
allowed.add(name) allowed.add(name)
return sorted(allowed) return sorted(allowed)