Skip to content

enum.Enum.__dir__ docs do not mention enum members in return value #104235

Closed

Description

@chgnrdv

3.12 documentation for enum.Enum.__dir__ says that this method returns "['__class__', '__doc__', '__module__', 'name', 'value'] and any public methods defined on self.__class__", but this is not true, e.g. when using the code provided:

>>> from enum import Enum
>>> from datetime import date
>>> class Weekday(Enum):
...     MONDAY = 1
...     TUESDAY = 2
...     WEDNESDAY = 3
...     THURSDAY = 4
...     FRIDAY = 5
...     SATURDAY = 6
...     SUNDAY = 7
...     @classmethod
...     def today(cls):
...         print('today is %s' % cls(date.today().isoweekday()).name)
... 
>>> dir(Weekday.SATURDAY)
['FRIDAY', 'MONDAY', 'SATURDAY', 'SUNDAY', 'THURSDAY', 'TUESDAY', 'WEDNESDAY', '__class__', '__doc__', '__eq__', '__hash__', '__module__', 'name', 'today', 'value']

Also it seems that test_enum has bug in it, because it doesn't detect the error above if invoked through libregrtest (as ./python -m test test_enum), but direct run of test_enum or doctest on enum documentation fails:

$ ./python -m test.test_enum
...........................................................................................................................................s.s......s........................................................................................................................................................................................................................................................................................................................s...........................................s...............................................................................................F
======================================================================
FAIL: /home/chgnrdv/cpython/Lib/test/../../Doc/library/enum.rst
Doctest: enum.rst
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/chgnrdv/cpython/Lib/doctest.py", line 2228, in runTest
    raise self.failureException(self.format_failure(new.getvalue()))
AssertionError: Failed doctest test for enum.rst
  File "/home/chgnrdv/cpython/Lib/test/../../Doc/library/enum.rst", line 0

----------------------------------------------------------------------
File "/home/chgnrdv/cpython/Lib/test/../../Doc/library/enum.rst", line 297, in enum.rst
Failed example:
    dir(Weekday.SATURDAY)
Expected:
    ['__class__', '__doc__', '__eq__', '__hash__', '__module__', 'name', 'today', 'value']
Got:
    ['FRIDAY', 'MONDAY', 'SATURDAY', 'SUNDAY', 'THURSDAY', 'TUESDAY', 'WEDNESDAY', '__class__', '__doc__', '__eq__', '__hash__', '__module__', 'name', 'today', 'value']


----------------------------------------------------------------------
Ran 602 tests in 5.113s

FAILED (failures=1, skipped=5)
./python -m doctest Doc/library/enum.rst 
**********************************************************************
File "Doc/library/enum.rst", line 297, in enum.rst
Failed example:
    dir(Weekday.SATURDAY)
Expected:
    ['__class__', '__doc__', '__eq__', '__hash__', '__module__', 'name', 'today', 'value']
Got:
    ['FRIDAY', 'MONDAY', 'SATURDAY', 'SUNDAY', 'THURSDAY', 'TUESDAY', 'WEDNESDAY', '__class__', '__doc__', '__eq__', '__hash__', '__module__', 'name', 'today', 'value']
**********************************************************************
1 items had failures:
   1 of  88 in enum.rst
***Test Failed*** 1 failures.

I'm working on fixes for both errors.

Linked PRs

Activity

  1. added
    type-bugAn unexpected behavior, bug, or error
    on May 6, 2023
  2. added
    stdlibStandard Library Python modules in the Lib/ directory
    on May 6, 2023
  3. sunmy2019 commented on May 6, 2023

    @sunmy2019
    Member

    This behavior change is probably due to the new Enum implementation in 3.11. Also confirmed on 3.11.

    No need to backport to 3.10.

  4. added
    3.11only security fixes
    3.12only security fixes
    on May 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

3.11only security fixes3.12only security fixesstdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions