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

Skip to content

Commit f6d1520

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

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
@@ -705,7 +705,7 @@ def __dir__(self):
705705
for cls in self.__class__.mro()
706706
for m in cls.__dict__
707707
if m[0] != '_' and m not in self._member_map_
708-
]
708+
] + [m for m in self.__dict__ if m[0] != '_']
709709
return (['__class__', '__doc__', '__module__'] + added_behavior)
710710

711711
def __format__(self, format_spec):

Lib/test/test_enum.py

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

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

516516

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

Misc/ACKS

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