classification
Title: Able to subclass enum with members by using multiple inheritance
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.10, Python 3.9, Python 3.8
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: ethan.furman Nosy List: Jeffrey.Kintscher, barry, eli.bendersky, eric.smith, ethan.furman, miss-islington, steven.daprano, talsuk5
Priority: normal Keywords: patch

Created on 2020-08-10 17:47 by talsuk5, last changed 2020-09-16 14:43 by ethan.furman. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 22271 merged ethan.furman, 2020-09-16 05:47
PR 22278 merged miss-islington, 2020-09-16 14:12
PR 22279 merged miss-islington, 2020-09-16 14:12
Messages (12)
msg375133 - (view) Author: Tal Suhareanu (talsuk5) Date: 2020-08-10 17:47
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)
------------------------------------
msg375135 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2020-08-10 18:55
Could you provide code which demonstrates the problem?
msg375139 - (view) Author: Tal Suhareanu (talsuk5) Date: 2020-08-10 19:01
Eric V. Smith it's in the first comment
msg375141 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2020-08-10 19:06
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?
msg375143 - (view) Author: Tal Suhareanu (talsuk5) Date: 2020-08-10 19:24
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)
msg375145 - (view) Author: Ethan Furman (ethan.furman) * (Python committer) Date: 2020-08-10 19:48
The problem is that class B should raise an error as class A already has members.
msg375147 - (view) Author: Tal Suhareanu (talsuk5) Date: 2020-08-10 19:54
Ethan Furman bullseye! thanks for clarifying
msg375157 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2020-08-10 23:42
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?
msg376921 - (view) Author: Ethan Furman (ethan.furman) * (Python committer) Date: 2020-09-15 00:36
To answer the invariant question, see this post by Guido:

  https://mail.python.org/pipermail/python-dev/2013-April/125716.html
msg376994 - (view) Author: Ethan Furman (ethan.furman) * (Python committer) Date: 2020-09-16 14:12
New changeset 3064dbf5df1021e85b507366a7ea448c8895efe7 by Ethan Furman in branch 'master':
bpo-41517: do not allow Enums to be extended (#22271)
https://github.com/python/cpython/commit/3064dbf5df1021e85b507366a7ea448c8895efe7
msg376997 - (view) Author: miss-islington (miss-islington) Date: 2020-09-16 14:31
New changeset 3f4012117bf80aa7c005f8fa6fb8e1f8b1aef5d5 by Miss Islington (bot) in branch '3.8':
bpo-41517: do not allow Enums to be extended (GH-22271)
https://github.com/python/cpython/commit/3f4012117bf80aa7c005f8fa6fb8e1f8b1aef5d5
msg376998 - (view) Author: miss-islington (miss-islington) Date: 2020-09-16 14:35
New changeset 48f99250ff319e36b15b555128cd62e408d8165f by Miss Islington (bot) in branch '3.9':
bpo-41517: do not allow Enums to be extended (GH-22271)
https://github.com/python/cpython/commit/48f99250ff319e36b15b555128cd62e408d8165f
History
Date User Action Args
2020-09-16 14:43:14ethan.furmansetstatus: open -> closed
assignee: ethan.furman
resolution: fixed
stage: patch review -> resolved
2020-09-16 14:35:22miss-islingtonsetmessages: + msg376998
2020-09-16 14:31:11miss-islingtonsetmessages: + msg376997
2020-09-16 14:12:25miss-islingtonsetpull_requests: + pull_request21332
2020-09-16 14:12:14miss-islingtonsetnosy: + miss-islington
pull_requests: + pull_request21331
2020-09-16 14:12:04ethan.furmansetmessages: + msg376994
2020-09-16 05:55:57ethan.furmansetversions: + Python 3.8, Python 3.9, Python 3.10, - Python 3.7
2020-09-16 05:47:05ethan.furmansetkeywords: + patch
stage: patch review
pull_requests: + pull_request21326
2020-09-15 00:36:02ethan.furmansetmessages: + msg376921
2020-08-10 23:42:29steven.dapranosetnosy: + steven.daprano

messages: + msg375157
title: Enum multiple inheritance loophole -> Able to subclass enum with members by using multiple inheritance
2020-08-10 22:46:25Jeffrey.Kintschersetnosy: + Jeffrey.Kintscher
2020-08-10 19:54:30talsuk5setmessages: + msg375147
2020-08-10 19:48:59ethan.furmansetmessages: + msg375145
2020-08-10 19:24:02talsuk5setmessages: + msg375143
2020-08-10 19:06:00eric.smithsetmessages: + msg375141
2020-08-10 19:01:16talsuk5setmessages: + msg375139
2020-08-10 18:55:17eric.smithsetnosy: + eric.smith
messages: + msg375135
2020-08-10 18:09:56xtreaksetnosy: + barry, eli.bendersky, ethan.furman
2020-08-10 17:47:58talsuk5create