]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.11] gh-111181: Fix enum doctests (GH-111180) (GH-111617)
authorEthan Furman <ethan@stoneleaf.us>
Fri, 3 Nov 2023 23:51:56 +0000 (16:51 -0700)
committerGitHub <noreply@github.com>
Fri, 3 Nov 2023 23:51:56 +0000 (16:51 -0700)
gh-111181: Fix enum doctests (GH-111180)

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

Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
Doc/howto/enum.rst
Lib/enum.py
Lib/test/test_enum.py

index 58849429539cca71550db569cd05d28e0d0a7a9d..a714f6d9aa3c7e4b2648e8290929d59d5ecdec21 100644 (file)
@@ -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::
@@ -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::
@@ -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::
 
index 155cb13022fa1688073e7f8bc187684c362427d6..63833aaa717feb34ada89b7b382f3cc7ce41013c 100644 (file)
@@ -1197,14 +1197,13 @@ class Enum(metaclass=EnumType):
 
     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():
@@ -1217,7 +1216,7 @@ class Enum(metaclass=EnumType):
                     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__'])
index ed1c3a59ce479ce3ea9e11b0e4222b2f12a535f6..ab2b45853c2214291ea4854bb30dbc37a77f0468 100644 (file)
@@ -18,7 +18,7 @@ from enum import member, nonmember, _iter_bits_lsb
 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
@@ -27,14 +27,19 @@ 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
@@ -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)