Skip to content

Commit 68526fe

Browse files
authored
bpo-40084: Enum - dir() includes member attributes (GH-19219)
1 parent 1b328ea commit 68526fe

File tree

5 files changed

+20
-2
lines changed

5 files changed

+20
-2
lines changed

Lib/enum.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -644,7 +644,7 @@ def __dir__(self):
644644
for cls in self.__class__.mro()
645645
for m in cls.__dict__
646646
if m[0] != '_' and m not in self._member_map_
647-
]
647+
] + [m for m in self.__dict__ if m[0] != '_']
648648
return (['__class__', '__doc__', '__module__'] + added_behavior)
649649

650650
def __format__(self, format_spec):

Lib/test/test_enum.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,18 @@ class SubEnum(SuperEnum):
216216
set(['__class__', '__doc__', '__module__', 'name', 'value', 'invisible']),
217217
)
218218

219+
def test_dir_on_sub_with_behavior_including_instance_dict_on_super(self):
220+
# see issue40084
221+
class SuperEnum(IntEnum):
222+
def __new__(cls, value, description=""):
223+
obj = int.__new__(cls, value)
224+
obj._value_ = value
225+
obj.description = description
226+
return obj
227+
class SubEnum(SuperEnum):
228+
sample = 5
229+
self.assertTrue({'description'} <= set(dir(SubEnum.sample)))
230+
219231
def test_enum_in_enum_out(self):
220232
Season = self.Season
221233
self.assertIs(Season(Season.WINTER), Season.WINTER)

Lib/test/test_httplib.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import errno
2-
from http import client
2+
from http import client, HTTPStatus
33
import io
44
import itertools
55
import os
@@ -519,6 +519,10 @@ def _parse_chunked(self, data):
519519

520520

521521
class BasicTest(TestCase):
522+
def test_dir_with_added_behavior_on_status(self):
523+
# see issue40084
524+
self.assertTrue({'description', 'name', 'phrase', 'value'} <= set(dir(HTTPStatus(404))))
525+
522526
def test_status_lines(self):
523527
# Test HTTP status lines
524528

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ Gawain Bolton
191191
Carl Friedrich Bolz-Tereick
192192
Forest Bond
193193
Gregory Bond
194+
Angelin Booz
194195
Médéric Boquien
195196
Matias Bordese
196197
Jonas Borgström
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix ``Enum.__dir__``: dir(Enum.member) now includes attributes as well as methods.

0 commit comments

Comments
 (0)