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

Able to subclass enum with members by using multiple inheritance #85689

Closed
talsuk5 mannequin opened this issue Aug 10, 2020 · 12 comments
Closed

Able to subclass enum with members by using multiple inheritance #85689

talsuk5 mannequin opened this issue Aug 10, 2020 · 12 comments
Assignees
Labels
3.8 only security fixes 3.9 only security fixes 3.10 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@talsuk5
Copy link
Mannequin

talsuk5 mannequin commented Aug 10, 2020

BPO 41517
Nosy @warsaw, @ericvsmith, @stevendaprano, @ethanfurman, @miss-islington, @websurfer5
PRs
  • bpo-41517: do not allow Enums to be extended #22271
  • [3.9] bpo-41517: do not allow Enums to be extended (GH-22271) #22278
  • [3.8] bpo-41517: do not allow Enums to be extended (GH-22271) #22279
  • 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 2020-09-16.14:43:14.465>
    created_at = <Date 2020-08-10.17:47:58.005>
    labels = ['3.8', 'type-bug', 'library', '3.9', '3.10']
    title = 'Able to subclass enum with members by using multiple inheritance'
    updated_at = <Date 2020-09-16.14:43:14.465>
    user = 'https://bugs.python.org/talsuk5'

    bugs.python.org fields:

    activity = <Date 2020-09-16.14:43:14.465>
    actor = 'ethan.furman'
    assignee = 'ethan.furman'
    closed = True
    closed_date = <Date 2020-09-16.14:43:14.465>
    closer = 'ethan.furman'
    components = ['Library (Lib)']
    creation = <Date 2020-08-10.17:47:58.005>
    creator = 'talsuk5'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 41517
    keywords = ['patch']
    message_count = 12.0
    messages = ['375133', '375135', '375139', '375141', '375143', '375145', '375147', '375157', '376921', '376994', '376997', '376998']
    nosy_count = 8.0
    nosy_names = ['barry', 'eric.smith', 'steven.daprano', 'eli.bendersky', 'ethan.furman', 'miss-islington', 'Jeffrey.Kintscher', 'talsuk5']
    pr_nums = ['22271', '22278', '22279']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue41517'
    versions = ['Python 3.8', 'Python 3.9', 'Python 3.10']

    @talsuk5
    Copy link
    Mannequin Author

    talsuk5 mannequin commented Aug 10, 2020

    when inheriting an implemented enum, we get a runtime error
    But when creating a multiple inheritance like the following, it works… so something feels broken in the enum mechanism

    ------------------------------------

    from enum import IntEnum, Enum
    
    class A(IntEnum):
       a = 1
    
    class B(A, Enum):
       b= 1
    
    print(B.b)

    @talsuk5 talsuk5 mannequin added 3.7 (EOL) end of life stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Aug 10, 2020
    @ericvsmith
    Copy link
    Member

    Could you provide code which demonstrates the problem?

    @talsuk5
    Copy link
    Mannequin Author

    talsuk5 mannequin commented Aug 10, 2020

    Eric V. Smith it's in the first comment

    @ericvsmith
    Copy link
    Member

    With Python 3.7.4, which is all I have handy, that code does not give a runtime error. It prints "B.b".

    Your text says code "when creating a multiple inheritance like the following, it works". It sounds like you know the code sample works. So I don't understand what code gives a runtime error. What type of error are you seeing?

    @talsuk5
    Copy link
    Mannequin Author

    talsuk5 mannequin commented Aug 10, 2020

    sorry, I'll explain. by "when inheriting an implemented enum, we get a runtime error" I refer to this https://docs.python.org/3/library/enum.html#restricted-enum-subclassing

    You shouldn't be able to subclass an enum, yet I just showed a loophole. my example code shows you can subclass an enum by using a trick, multiple inheritance.

    I hope it's more clear now :)

    btw it also works with regular Enum inheritance (I just used IntEnum for simplicity)

    @ethanfurman
    Copy link
    Member

    The problem is that class B should raise an error as class A already has members.

    @talsuk5
    Copy link
    Mannequin Author

    talsuk5 mannequin commented Aug 10, 2020

    Ethan Furman bullseye! thanks for clarifying

    @stevendaprano
    Copy link
    Member

    The documentation says:

    "Allowing subclassing of enums that define members would lead to a violation of some important invariants of types and instances."

    but it isn't clear what those invariants are, or why it is more of a problem for enums than any other subclassing situation. Could the docs be updated to explain why it is prohibited?

    @stevendaprano stevendaprano changed the title Enum multiple inheritance loophole Able to subclass enum with members by using multiple inheritance Aug 10, 2020
    @stevendaprano stevendaprano changed the title Enum multiple inheritance loophole Able to subclass enum with members by using multiple inheritance Aug 10, 2020
    @ethanfurman
    Copy link
    Member

    To answer the invariant question, see this post by Guido:

    https://mail.python.org/pipermail/python-dev/2013-April/125716.html

    @ethanfurman ethanfurman added 3.8 only security fixes 3.9 only security fixes 3.10 only security fixes and removed 3.7 (EOL) end of life labels Sep 16, 2020
    @ethanfurman
    Copy link
    Member

    New changeset 3064dbf by Ethan Furman in branch 'master':
    bpo-41517: do not allow Enums to be extended (bpo-22271)
    3064dbf

    @miss-islington
    Copy link
    Contributor

    New changeset 3f40121 by Miss Islington (bot) in branch '3.8':
    bpo-41517: do not allow Enums to be extended (GH-22271)
    3f40121

    @miss-islington
    Copy link
    Contributor

    New changeset 48f9925 by Miss Islington (bot) in branch '3.9':
    bpo-41517: do not allow Enums to be extended (GH-22271)
    48f9925

    @ethanfurman ethanfurman self-assigned this Sep 16, 2020
    @ethanfurman ethanfurman self-assigned this Sep 16, 2020
    @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.8 only security fixes 3.9 only security fixes 3.10 only 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

    4 participants