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

enum.Enum is False-y #70935

Closed
pitrou opened this issue Apr 13, 2016 · 10 comments
Closed

enum.Enum is False-y #70935

pitrou opened this issue Apr 13, 2016 · 10 comments
Assignees
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@pitrou
Copy link
Member

pitrou commented Apr 13, 2016

BPO 26748
Nosy @gvanrossum, @warsaw, @pitrou, @ethanfurman, @serhiy-storchaka

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 2016-04-14.21:00:43.068>
created_at = <Date 2016-04-13.17:22:01.365>
labels = ['type-bug', 'library']
title = 'enum.Enum is False-y'
updated_at = <Date 2016-04-14.21:00:43.066>
user = 'https://github.com/pitrou'

bugs.python.org fields:

activity = <Date 2016-04-14.21:00:43.066>
actor = 'ethan.furman'
assignee = 'ethan.furman'
closed = True
closed_date = <Date 2016-04-14.21:00:43.068>
closer = 'ethan.furman'
components = ['Library (Lib)']
creation = <Date 2016-04-13.17:22:01.365>
creator = 'pitrou'
dependencies = []
files = []
hgrepos = []
issue_num = 26748
keywords = []
message_count = 10.0
messages = ['263342', '263343', '263344', '263345', '263346', '263347', '263348', '263359', '263371', '263430']
nosy_count = 7.0
nosy_names = ['gvanrossum', 'barry', 'pitrou', 'eli.bendersky', 'ethan.furman', 'python-dev', 'serhiy.storchaka']
pr_nums = []
priority = 'normal'
resolution = 'fixed'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue26748'
versions = ['Python 3.4', 'Python 3.5']

@pitrou
Copy link
Member Author

pitrou commented Apr 13, 2016

>>> 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

@pitrou pitrou added stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Apr 13, 2016
@gvanrossum
Copy link
Member

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.

@gvanrossum
Copy link
Member

Oh wait. The *class* is False-y? That's definitely a bug, just fix it.

@pitrou
Copy link
Member Author

pitrou commented Apr 13, 2016

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).

@pitrou
Copy link
Member Author

pitrou commented Apr 13, 2016

Ok, we posted at the same time :-) Yes, the class is false-y.

@pitrou
Copy link
Member Author

pitrou commented Apr 13, 2016

Just reading the code now, the reason is that EnumMeta pretends to be a collection (it defines a __len__ and an __iter__).

@ethanfurman
Copy link
Member

EnumMeta /is/ a collection (at least in the same sense the dict class is a collection). ;)

Fix is on it's way...

@serhiy-storchaka
Copy link
Member

See also bpo-23008.

@python-dev
Copy link
Mannequin

python-dev mannequin commented Apr 14, 2016

New changeset 772805538caf by Ethan Furman in branch '3.4':
bpo-26748: Enum classes should evaluate as True
https://hg.python.org/cpython/rev/772805538caf

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

New changeset 2fc61f8ee2d2 by Ethan Furman in branch 'default':
bpo-26748: Enum classes should evaluate as True
https://hg.python.org/cpython/rev/2fc61f8ee2d2

@ethanfurman
Copy link
Member

Enum classes are now Truth-y in 3.4, 3.5, enum34, and aenum. :)

@ethanfurman ethanfurman self-assigned this Apr 14, 2016
@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
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