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

Improve error message when attempting to extend an enum with __call__ #90400

Closed
sobolevn opened this issue Jan 3, 2022 · 2 comments
Closed
Assignees
Labels
3.10 only security fixes 3.11 bug and security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@sobolevn
Copy link
Member

sobolevn commented Jan 3, 2022

BPO 46242
Nosy @ethanfurman, @pablogsal, @sobolevn
PRs
  • bpo-46242: better error message for extending Enum with members #30357
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = 'https://github.com/ethanfurman'
    closed_at = <Date 2022-01-14.23:58:17.014>
    created_at = <Date 2022-01-03.13:23:31.323>
    labels = ['type-bug', 'library', '3.10', '3.11']
    title = 'Improve error message when attempting to extend an enum with `__call__`'
    updated_at = <Date 2022-01-14.23:58:17.014>
    user = 'https://github.com/sobolevn'

    bugs.python.org fields:

    activity = <Date 2022-01-14.23:58:17.014>
    actor = 'AlexWaygood'
    assignee = 'ethan.furman'
    closed = True
    closed_date = <Date 2022-01-14.23:58:17.014>
    closer = 'AlexWaygood'
    components = ['Library (Lib)']
    creation = <Date 2022-01-03.13:23:31.323>
    creator = 'sobolevn'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 46242
    keywords = ['patch']
    message_count = 2.0
    messages = ['409583', '410603']
    nosy_count = 3.0
    nosy_names = ['ethan.furman', 'pablogsal', 'sobolevn']
    pr_nums = ['30357']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue46242'
    versions = ['Python 3.10', 'Python 3.11']

    @sobolevn
    Copy link
    Member Author

    sobolevn commented Jan 3, 2022

    Right now when creating a new Enum, we check not to extend Enum with existing _member_names_:

    Python 3.11.0a3+ (heads/main:8d7644fa64, Dec 30 2021, 13:00:40) [Clang 11.0.0 (clang-1100.0.33.16)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import enum
    >>> class A(enum.Enum):
    ...   a = 1
    ... 
    >>> class B(A): pass
    ... 
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/Users/sobolev/Desktop/cpython/Lib/enum.py", line 398, in __prepare__
        metacls._check_for_existing_members(cls, bases)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "/Users/sobolev/Desktop/cpython/Lib/enum.py", line 850, in _check_for_existing_members
        raise TypeError(
        ^^^^^^^^^^^^^^^^
    TypeError: B: cannot extend enumeration 'A'

    But when we try to use A() call to do the same, where what happens:

    Python 3.11.0a3+ (heads/main:8d7644fa64, Dec 30 2021, 13:00:40) [Clang 11.0.0 (clang-1100.0.33.16)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import enum
    >>> class A(enum.Enum):
    ...   a = 1
    ... 
    >>> B = A('B', 'b')
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/Users/sobolev/Desktop/cpython/Lib/enum.py", line 606, in __call__
        return cls._create_(
               ^^^^^^^^^^^^^
      File "/Users/sobolev/Desktop/cpython/Lib/enum.py", line 770, in _create_
        _, first_enum = cls._get_mixins_(class_name, bases)
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "/Users/sobolev/Desktop/cpython/Lib/enum.py", line 899, in _get_mixins_
        raise TypeError('Cannot extend enumerations')
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    TypeError: Cannot extend enumerations
    

    I propose to use the first error message in this case as well. Moreover, this behavior is not covered with tests:

    » ag 'Cannot extend enumerations'
    Lib/enum.py
    899:            raise TypeError('Cannot extend enumerations')
    

    I will add tests for this edge case.

    @sobolevn sobolevn added 3.10 only security fixes 3.11 bug and security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Jan 3, 2022
    @AlexWaygood AlexWaygood changed the title Improve error message when creating an enum with __call__ Improve error message when attempting to extend an enum with __call__ Jan 3, 2022
    @ethanfurman
    Copy link
    Member

    New changeset e674e48 by Nikita Sobolev in branch 'main':
    bpo-46242: [Enum] better error message for extending Enum with members (GH-30357)
    e674e48

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.10 only security fixes 3.11 bug and security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants