Skip to content

Commit 03d064b

Browse files
committed
bpo-40084: dir() includes entries from instance dict
1 parent 0003c2d commit 03d064b

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
@@ -614,7 +614,7 @@ def __dir__(self):
614614
for cls in self.__class__.mro()
615615
for m in cls.__dict__
616616
if m[0] != '_' and m not in self._member_map_
617-
]
617+
] + [m for m in self.__dict__ if m[0] != '_']
618618
return (['__class__', '__doc__', '__module__'] + added_behavior)
619619

620620
def __format__(self, format_spec):

Lib/test/test_enum.py

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

218+
def test_dir_on_sub_with_behavior_including_instance_dict_on_super(self):
219+
# see issue40084
220+
class SuperEnum(IntEnum):
221+
def __new__(cls, value, description=""):
222+
obj = int.__new__(cls, value)
223+
obj._value_ = value
224+
obj.description = description
225+
return obj
226+
class SubEnum(SuperEnum):
227+
sample = 5
228+
self.assertTrue({'description'} <= set(dir(SubEnum.sample)))
229+
218230
def test_enum_in_enum_out(self):
219231
Season = self.Season
220232
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
@@ -493,6 +493,10 @@ def _parse_chunked(self, data):
493493

494494

495495
class BasicTest(TestCase):
496+
def test_dir_with_added_behavior_on_status(self):
497+
# see issue40084
498+
self.assertTrue({'description', 'name', 'phrase', 'value'} <= set(dir(HTTPStatus(404))))
499+
496500
def test_status_lines(self):
497501
# Test HTTP status lines
498502

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ Gawain Bolton
188188
Carl Friedrich Bolz-Tereick
189189
Forest Bond
190190
Gregory Bond
191+
Angelin Booz
191192
Médéric Boquien
192193
Matias Bordese
193194
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)