Skip to content

Commit fbf3145

Browse files
[Enum] Remove automatic docstring generation (GH-94188)
(cherry picked from commit 28a2ccf) Co-authored-by: Sam Ezeh <sam.z.ezeh@gmail.com>
1 parent 65ed8b4 commit fbf3145

File tree

2 files changed

+4
-267
lines changed

2 files changed

+4
-267
lines changed

Lib/enum.py

-105
Original file line numberDiff line numberDiff line change
@@ -536,111 +536,6 @@ def __new__(metacls, cls, bases, classdict, *, boundary=None, _simple=False, **k
536536
# update classdict with any changes made by __init_subclass__
537537
classdict.update(enum_class.__dict__)
538538
#
539-
# create a default docstring if one has not been provided
540-
if enum_class.__doc__ is None:
541-
if not member_names or not list(enum_class):
542-
enum_class.__doc__ = classdict['__doc__'] = _dedent("""\
543-
Create a collection of name/value pairs.
544-
545-
Example enumeration:
546-
547-
>>> class Color(Enum):
548-
... RED = 1
549-
... BLUE = 2
550-
... GREEN = 3
551-
552-
Access them by:
553-
554-
- attribute access::
555-
556-
>>> Color.RED
557-
<Color.RED: 1>
558-
559-
- value lookup:
560-
561-
>>> Color(1)
562-
<Color.RED: 1>
563-
564-
- name lookup:
565-
566-
>>> Color['RED']
567-
<Color.RED: 1>
568-
569-
Enumerations can be iterated over, and know how many members they have:
570-
571-
>>> len(Color)
572-
3
573-
574-
>>> list(Color)
575-
[<Color.RED: 1>, <Color.BLUE: 2>, <Color.GREEN: 3>]
576-
577-
Methods can be added to enumerations, and members can have their own
578-
attributes -- see the documentation for details.
579-
""")
580-
else:
581-
member = list(enum_class)[0]
582-
enum_length = len(enum_class)
583-
cls_name = enum_class.__name__
584-
if enum_length == 1:
585-
list_line = 'list(%s)' % cls_name
586-
list_repr = '[<%s.%s: %r>]' % (cls_name, member.name, member.value)
587-
elif enum_length == 2:
588-
member2 = list(enum_class)[1]
589-
list_line = 'list(%s)' % cls_name
590-
list_repr = '[<%s.%s: %r>, <%s.%s: %r>]' % (
591-
cls_name, member.name, member.value,
592-
cls_name, member2.name, member2.value,
593-
)
594-
else:
595-
member2 = list(enum_class)[1]
596-
member3 = list(enum_class)[2]
597-
list_line = 'list(%s)%s' % (cls_name, ('','[:3]')[enum_length > 3])
598-
list_repr = '[<%s.%s: %r>, <%s.%s: %r>, <%s.%s: %r>]' % (
599-
cls_name, member.name, member.value,
600-
cls_name, member2.name, member2.value,
601-
cls_name, member3.name, member3.value,
602-
)
603-
enum_class.__doc__ = classdict['__doc__'] = _dedent("""\
604-
A collection of name/value pairs.
605-
606-
Access them by:
607-
608-
- attribute access::
609-
610-
>>> %s.%s
611-
<%s.%s: %r>
612-
613-
- value lookup:
614-
615-
>>> %s(%r)
616-
<%s.%s: %r>
617-
618-
- name lookup:
619-
620-
>>> %s[%r]
621-
<%s.%s: %r>
622-
623-
Enumerations can be iterated over, and know how many members they have:
624-
625-
>>> len(%s)
626-
%r
627-
628-
>>> %s
629-
%s
630-
631-
Methods can be added to enumerations, and members can have their own
632-
attributes -- see the documentation for details.
633-
"""
634-
% (cls_name, member.name,
635-
cls_name, member.name, member.value,
636-
cls_name, member.value,
637-
cls_name, member.name, member.value,
638-
cls_name, member.name,
639-
cls_name, member.name, member.value,
640-
cls_name, enum_length,
641-
list_line, list_repr,
642-
))
643-
#
644539
# double check that repr and friends are not the mixin's or various
645540
# things break (such as pickle)
646541
# however, if the method is defined in the Enum itself, don't replace

Lib/test/test_enum.py

+4-162
Original file line numberDiff line numberDiff line change
@@ -4073,36 +4073,6 @@ class TestEnumTypeSubclassing(unittest.TestCase):
40734073
class Color(enum.Enum)
40744074
| Color(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)
40754075
|\x20\x20
4076-
| A collection of name/value pairs.
4077-
|\x20\x20
4078-
| Access them by:
4079-
|\x20\x20
4080-
| - attribute access::
4081-
|\x20\x20
4082-
| >>> Color.CYAN
4083-
| <Color.CYAN: 1>
4084-
|\x20\x20
4085-
| - value lookup:
4086-
|\x20\x20
4087-
| >>> Color(1)
4088-
| <Color.CYAN: 1>
4089-
|\x20\x20
4090-
| - name lookup:
4091-
|\x20\x20
4092-
| >>> Color['CYAN']
4093-
| <Color.CYAN: 1>
4094-
|\x20\x20
4095-
| Enumerations can be iterated over, and know how many members they have:
4096-
|\x20\x20
4097-
| >>> len(Color)
4098-
| 3
4099-
|\x20\x20
4100-
| >>> list(Color)
4101-
| [<Color.CYAN: 1>, <Color.MAGENTA: 2>, <Color.YELLOW: 3>]
4102-
|\x20\x20
4103-
| Methods can be added to enumerations, and members can have their own
4104-
| attributes -- see the documentation for details.
4105-
|\x20\x20
41064076
| Method resolution order:
41074077
| Color
41084078
| enum.Enum
@@ -4348,157 +4318,29 @@ def test__all__(self):
43484318
def test_doc_1(self):
43494319
class Single(Enum):
43504320
ONE = 1
4351-
self.assertEqual(
4352-
Single.__doc__,
4353-
dedent("""\
4354-
A collection of name/value pairs.
4355-
4356-
Access them by:
4357-
4358-
- attribute access::
4359-
4360-
>>> Single.ONE
4361-
<Single.ONE: 1>
4362-
4363-
- value lookup:
4364-
4365-
>>> Single(1)
4366-
<Single.ONE: 1>
4367-
4368-
- name lookup:
4369-
4370-
>>> Single['ONE']
4371-
<Single.ONE: 1>
4372-
4373-
Enumerations can be iterated over, and know how many members they have:
4374-
4375-
>>> len(Single)
4376-
1
4377-
4378-
>>> list(Single)
4379-
[<Single.ONE: 1>]
4380-
4381-
Methods can be added to enumerations, and members can have their own
4382-
attributes -- see the documentation for details.
4383-
"""))
4321+
self.assertEqual(Single.__doc__, None)
43844322

43854323
def test_doc_2(self):
43864324
class Double(Enum):
43874325
ONE = 1
43884326
TWO = 2
4389-
self.assertEqual(
4390-
Double.__doc__,
4391-
dedent("""\
4392-
A collection of name/value pairs.
4393-
4394-
Access them by:
4395-
4396-
- attribute access::
4397-
4398-
>>> Double.ONE
4399-
<Double.ONE: 1>
4400-
4401-
- value lookup:
4402-
4403-
>>> Double(1)
4404-
<Double.ONE: 1>
4405-
4406-
- name lookup:
4407-
4408-
>>> Double['ONE']
4409-
<Double.ONE: 1>
4410-
4411-
Enumerations can be iterated over, and know how many members they have:
4412-
4413-
>>> len(Double)
4414-
2
4415-
4416-
>>> list(Double)
4417-
[<Double.ONE: 1>, <Double.TWO: 2>]
4418-
4419-
Methods can be added to enumerations, and members can have their own
4420-
attributes -- see the documentation for details.
4421-
"""))
4327+
self.assertEqual(Double.__doc__, None)
44224328

44234329

44244330
def test_doc_1(self):
44254331
class Triple(Enum):
44264332
ONE = 1
44274333
TWO = 2
44284334
THREE = 3
4429-
self.assertEqual(
4430-
Triple.__doc__,
4431-
dedent("""\
4432-
A collection of name/value pairs.
4433-
4434-
Access them by:
4435-
4436-
- attribute access::
4437-
4438-
>>> Triple.ONE
4439-
<Triple.ONE: 1>
4440-
4441-
- value lookup:
4442-
4443-
>>> Triple(1)
4444-
<Triple.ONE: 1>
4445-
4446-
- name lookup:
4447-
4448-
>>> Triple['ONE']
4449-
<Triple.ONE: 1>
4450-
4451-
Enumerations can be iterated over, and know how many members they have:
4452-
4453-
>>> len(Triple)
4454-
3
4455-
4456-
>>> list(Triple)
4457-
[<Triple.ONE: 1>, <Triple.TWO: 2>, <Triple.THREE: 3>]
4458-
4459-
Methods can be added to enumerations, and members can have their own
4460-
attributes -- see the documentation for details.
4461-
"""))
4335+
self.assertEqual(Triple.__doc__, None)
44624336

44634337
def test_doc_1(self):
44644338
class Quadruple(Enum):
44654339
ONE = 1
44664340
TWO = 2
44674341
THREE = 3
44684342
FOUR = 4
4469-
self.assertEqual(
4470-
Quadruple.__doc__,
4471-
dedent("""\
4472-
A collection of name/value pairs.
4473-
4474-
Access them by:
4475-
4476-
- attribute access::
4477-
4478-
>>> Quadruple.ONE
4479-
<Quadruple.ONE: 1>
4480-
4481-
- value lookup:
4482-
4483-
>>> Quadruple(1)
4484-
<Quadruple.ONE: 1>
4485-
4486-
- name lookup:
4487-
4488-
>>> Quadruple['ONE']
4489-
<Quadruple.ONE: 1>
4490-
4491-
Enumerations can be iterated over, and know how many members they have:
4492-
4493-
>>> len(Quadruple)
4494-
4
4495-
4496-
>>> list(Quadruple)[:3]
4497-
[<Quadruple.ONE: 1>, <Quadruple.TWO: 2>, <Quadruple.THREE: 3>]
4498-
4499-
Methods can be added to enumerations, and members can have their own
4500-
attributes -- see the documentation for details.
4501-
"""))
4343+
self.assertEqual(Quadruple.__doc__, None)
45024344

45034345

45044346
# These are unordered here on purpose to ensure that declaration order

0 commit comments

Comments
 (0)