classification
Title: enum.Enum is False-y
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.4, Python 3.5
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: ethan.furman Nosy List: barry, eli.bendersky, ethan.furman, gvanrossum, pitrou, python-dev, serhiy.storchaka
Priority: normal Keywords:

Created on 2016-04-13 17:22 by pitrou, last changed 2016-04-14 21:00 by ethan.furman. This issue is now closed.

Messages (10)
msg263342 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2016-04-13 17:22
>>> import enum
>>> bool(enum.Enum)
False
>>> bool(enum.IntEnum)
False

This behaviour is relatively unexpected for classes, and can lead to subtle bugs such as the following:

https://bitbucket.org/ambv/singledispatch/issues/8/inconsistent-hierarchy-with-enum
msg263343 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2016-04-13 17:47
I guess it's marked 2.7 because of the enum34 backport? There's no enum in the 2.7 stdlib.

I believe this was brought up before on one of the lists but I don't recall the outcome of the discussion, except that for IntEnum the behavior is correct.  I tend to agree that for plain Enum it's a problem, the question is whether we can fix it without breaking code that accidentally relies on this behavior.
msg263344 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2016-04-13 17:49
Oh wait. The *class* is False-y? That's definitely a bug, just fix it.
msg263345 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2016-04-13 17:50
Yes, I didn't find a separate bug tracker for the enum34 backport, which is why I included that version here.

> for IntEnum the behavior is correct

Do you remember the argument? I agree that IntEnum *instances* may be falsy, but IntEnum classes I don't see why.

That said, if an IntEnum *class* has to be false-y, then there's no real point in fixing just Enum (you can pass an IntEnum instance to singledispatch() too).
msg263346 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2016-04-13 17:51
Ok, we posted at the same time :-) Yes, the class is false-y.
msg263347 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2016-04-13 17:53
Just reading the code now, the reason is that EnumMeta pretends to be a collection (it defines a __len__ and an __iter__).
msg263348 - (view) Author: Ethan Furman (ethan.furman) * (Python committer) Date: 2016-04-13 18:06
EnumMeta /is/ a collection (at least in the same sense the dict class is a collection).  ;)

Fix is on it's way...
msg263359 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2016-04-13 20:04
See also issue23008.
msg263371 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2016-04-14 07:04
New changeset 772805538caf by Ethan Furman in branch '3.4':
Issue26748: Enum classes should evaluate as True
https://hg.python.org/cpython/rev/772805538caf

New changeset f840608f79da by Ethan Furman in branch '3.5':
Issue26748: Enum classes should evaluate as True
https://hg.python.org/cpython/rev/f840608f79da

New changeset 2fc61f8ee2d2 by Ethan Furman in branch 'default':
Issue26748: Enum classes should evaluate as True
https://hg.python.org/cpython/rev/2fc61f8ee2d2
msg263430 - (view) Author: Ethan Furman (ethan.furman) * (Python committer) Date: 2016-04-14 21:00
Enum classes are now Truth-y in 3.4, 3.5, enum34, and aenum.  :)
History
Date User Action Args
2016-04-14 21:00:43ethan.furmansetstatus: open -> closed
versions: - Python 2.7
messages: + msg263430

assignee: ethan.furman
resolution: fixed
stage: resolved
2016-04-14 07:04:14python-devsetnosy: + python-dev
messages: + msg263371
2016-04-13 20:04:46serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg263359
2016-04-13 18:06:42ethan.furmansetmessages: + msg263348
2016-04-13 17:53:55pitrousetmessages: + msg263347
2016-04-13 17:51:13pitrousetmessages: + msg263346
2016-04-13 17:50:51pitrousetmessages: + msg263345
2016-04-13 17:49:31gvanrossumsetmessages: + msg263344
2016-04-13 17:47:41gvanrossumsetmessages: + msg263343
2016-04-13 17:22:01pitroucreate