Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

3.11 creates doctests for Enum subclasses brittle against globs and overridden __repr__ #93976

Closed
jacobtylerwalls opened this issue Jun 18, 2022 · 0 comments · Fixed by #94188
Assignees
Labels
3.11 only security fixes 3.12 bugs and security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@jacobtylerwalls
Copy link
Contributor

Bug report

In 3.11, subclassing Enum without providing a docstring causes tests to be added to a module's doctest suite.

  • This lengthens test suites
  • This creates failures if Enum is not provided in the globs argument to doctest test runners
  • This creates failures if __repr__ is overridden
import re
from enum import Enum

class MyEnum(Enum):
    def __repr__(self):
        """Suppress the trailing colon and integer"""
        return re.sub(r': \d+', '', super().__repr__())


class FinalEnum(MyEnum):
    MEMBER = 1


if __name__ == "__main__":
    import doctest
    doctest.testmod(globs={'MyEnum': MyEnum, 'FinalEnum': FinalEnum})

3.10

$ python3.10 e.py -v
4 items had no tests:
    __main__
    __main__.FinalEnum
    __main__.MyEnum
    __main__.MyEnum.__repr__
0 tests in 4 items.
0 passed and 0 failed.
Test passed.

3.11

$ python3.11 e.py
**********************************************************************
File "/Users/.../e.py", line ?, in __main__.FinalEnum
Failed example:
    FinalEnum.MEMBER
Expected:
    <FinalEnum.MEMBER: 1>
Got:
    <FinalEnum.MEMBER>
**********************************************************************
File "/Users/.../e.py", line ?, in __main__.FinalEnum
Failed example:
    FinalEnum(1)
Expected:
    <FinalEnum.MEMBER: 1>
Got:
    <FinalEnum.MEMBER>
**********************************************************************
File "/Users/.../e.py", line ?, in __main__.FinalEnum
Failed example:
    FinalEnum['MEMBER']
Expected:
    <FinalEnum.MEMBER: 1>
Got:
    <FinalEnum.MEMBER>
**********************************************************************
File "/Users/.../e.py", line ?, in __main__.FinalEnum
Failed example:
    list(FinalEnum)
Expected:
    [<FinalEnum.MEMBER: 1>]
Got:
    [<FinalEnum.MEMBER>]
**********************************************************************
File "/Users/.../e.py", line 10, in __main__.MyEnum
Failed example:
    class Color(Enum):
        RED = 1
        BLUE = 2
        GREEN = 3
Exception raised:
    Traceback (most recent call last):
      File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/doctest.py", line 1350, in __run
        exec(compile(example.source, filename, "single",
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "<doctest __main__.MyEnum[0]>", line 1, in <module>
        class Color(Enum):
                    ^^^^
    NameError: name 'Enum' is not defined
**********************************************************************
File "/Users/.../e.py", line 19, in __main__.MyEnum
Failed example:
    Color.RED
Exception raised:
    Traceback (most recent call last):
      File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/doctest.py", line 1350, in __run
        exec(compile(example.source, filename, "single",
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "<doctest __main__.MyEnum[1]>", line 1, in <module>
        Color.RED
        ^^^^^
    NameError: name 'Color' is not defined
**********************************************************************
File "/Users/.../e.py", line 24, in __main__.MyEnum
Failed example:
    Color(1)
Exception raised:
    Traceback (most recent call last):
      File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/doctest.py", line 1350, in __run
        exec(compile(example.source, filename, "single",
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "<doctest __main__.MyEnum[2]>", line 1, in <module>
        Color(1)
        ^^^^^
    NameError: name 'Color' is not defined
**********************************************************************
File "/Users/.../e.py", line 29, in __main__.MyEnum
Failed example:
    Color['RED']
Exception raised:
    Traceback (most recent call last):
      File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/doctest.py", line 1350, in __run
        exec(compile(example.source, filename, "single",
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "<doctest __main__.MyEnum[3]>", line 1, in <module>
        Color['RED']
        ^^^^^
    NameError: name 'Color' is not defined
**********************************************************************
File "/Users/.../e.py", line 34, in __main__.MyEnum
Failed example:
    len(Color)
Exception raised:
    Traceback (most recent call last):
      File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/doctest.py", line 1350, in __run
        exec(compile(example.source, filename, "single",
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "<doctest __main__.MyEnum[4]>", line 1, in <module>
        len(Color)
            ^^^^^
    NameError: name 'Color' is not defined
**********************************************************************
File "/Users/.../e.py", line 37, in __main__.MyEnum
Failed example:
    list(Color)
Exception raised:
    Traceback (most recent call last):
      File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/doctest.py", line 1350, in __run
        exec(compile(example.source, filename, "single",
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "<doctest __main__.MyEnum[5]>", line 1, in <module>
        list(Color)
             ^^^^^
    NameError: name 'Color' is not defined
**********************************************************************
2 items had failures:
   4 of   5 in __main__.FinalEnum
   6 of   6 in __main__.MyEnum
***Test Failed*** 10 failures.

Your environment

Python 3.11.0b3

cc/ @mscuthbert (FYI: a potential workaround is to add docstrings)

@jacobtylerwalls jacobtylerwalls added the type-bug An unexpected behavior, bug, or error label Jun 18, 2022
@ethanfurman ethanfurman self-assigned this Jun 18, 2022
@AlexWaygood AlexWaygood added stdlib Python modules in the Lib dir 3.11 only security fixes 3.12 bugs and security fixes labels Jun 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.11 only security fixes 3.12 bugs and security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants