10000 bpo-40084: Enum - dir() includes member attributes (GH-19219) · python/cpython@33cbb04 · GitHub
[go: up one dir, main page]

Skip to content

Commit 33cbb04

Browse files
bpo-40084: Enum - dir() includes member attributes (GH-19219)
(cherry picked from commit 68526fe) Co-authored-by: Angelin BOOZ <9497359+lem2clide@users.noreply.github.com>
1 parent dbb0006 commit 33cbb04

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
@@ -699,7 +699,7 @@ def __dir__(self):
699699
for cls in self.__class__.mro()
700700
for m in cls.__dict__
701701
if m[0] != '_' and m not in self._member_map_
702-
]
702+
] + [m for m in self.__dict__ if m[0] != '_']
703703
return (['__class__', '__doc__', '__module__'] + added_behavior)
704704

705705
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 8000 +
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
@@ -516,6 +516,10 @@ def _parse_chunked(self, data):
516516

517517

518518
class BasicTest(TestCase):
519+
def test_dir_with_added_behavior_on_status(self):
520+
# see issue40084
521+
self.assertTrue({'description', 'name', 'phrase', 'value'} <= set(dir(HTTPStatus(404))))
522+
519523
def test_status_lines(self):
520524
# Test HTTP status lines
521525

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ Gawain Bolton
192192
Carl Friedrich Bolz-Tereick
193193
Forest Bond
194194
Gregory Bond
195+
Angelin Booz
195196
Médéric Boquien
196197
Matias Bordese
197198
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)
0