]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-40084: Enum - dir() includes member attributes (GH-19219)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Mon, 14 Dec 2020 22:43:43 +0000 (14:43 -0800)
committerGitHub <noreply@github.com>
Mon, 14 Dec 2020 22:43:43 +0000 (14:43 -0800)
(cherry picked from commit 68526fe258da8c01196fd7cf48e8e5f1280bf8fd)

Co-authored-by: Angelin BOOZ <9497359+lem2clide@users.noreply.github.com>
Lib/enum.py
Lib/test/test_enum.py
Lib/test/test_httplib.py
Misc/ACKS
Misc/NEWS.d/next/Library/2020-03-29-21-32-00.bpo-40084.MCYwcv.rst [new file with mode: 0644]

index b14da088f33bf353acc4a5b663c00b5a991074fb..35210c993325db326c129346daead2fdfd1159e2 100644 (file)
@@ -699,7 +699,7 @@ class Enum(metaclass=EnumMeta):
                 for cls in self.__class__.mro()
                 for m in cls.__dict__
                 if m[0] != '_' and m not in self._member_map_
-                ]
+                ] + [m for m in self.__dict__ if m[0] != '_']
         return (['__class__', '__doc__', '__module__'] + added_behavior)
 
     def __format__(self, format_spec):
index ccc2cbe1ec14b50bb0c93171c95df8fbb2344971..dca668a9046f36191857a21250d5a5a43abc5774 100644 (file)
@@ -215,6 +215,18 @@ class TestEnum(unittest.TestCase):
                 set(['__class__', '__doc__', '__module__', 'name', 'value', 'invisible']),
                 )
 
+    def test_dir_on_sub_with_behavior_including_instance_dict_on_super(self):
+        # see issue40084
+        class SuperEnum(IntEnum):
+            def __new__(cls, value, description=""):
+                obj = int.__new__(cls, value)
+                obj._value_ = value
+                obj.description = description
+                return obj
+        class SubEnum(SuperEnum):
+            sample = 5
+        self.assertTrue({'description'} <= set(dir(SubEnum.sample)))
+
     def test_enum_in_enum_out(self):
         Season = self.Season
         self.assertIs(Season(Season.WINTER), Season.WINTER)
index ed125893d6d07292e8244d11e7baae2d19170bce..28943f02564fb0e66b3df4d55b656ab9fdcbde2c 100644 (file)
@@ -1,5 +1,5 @@
 import errno
-from http import client
+from http import client, HTTPStatus
 import io
 import itertools
 import os
@@ -516,6 +516,10 @@ class TransferEncodingTest(TestCase):
 
 
 class BasicTest(TestCase):
+    def test_dir_with_added_behavior_on_status(self):
+        # see issue40084
+        self.assertTrue({'description', 'name', 'phrase', 'value'} <= set(dir(HTTPStatus(404))))
+
     def test_status_lines(self):
         # Test HTTP status lines
 
index 12a5ac1410a7700252cd32c2c91b9f6908234465..ebf12c9a9f1dcfcdabae981dd88ef2513bd12a70 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -192,6 +192,7 @@ Gawain Bolton
 Carl Friedrich Bolz-Tereick
 Forest Bond
 Gregory Bond
+Angelin Booz
 Médéric Boquien
 Matias Bordese
 Jonas Borgström
diff --git a/Misc/NEWS.d/next/Library/2020-03-29-21-32-00.bpo-40084.MCYwcv.rst b/Misc/NEWS.d/next/Library/2020-03-29-21-32-00.bpo-40084.MCYwcv.rst
new file mode 100644 (file)
index 0000000..65ff4ce
--- /dev/null
@@ -0,0 +1 @@
+Fix ``Enum.__dir__``: dir(Enum.member) now includes attributes as well as methods.