This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Add recipe for "valueless" Enums to docs
Type: enhancement Stage: resolved
Components: Documentation Versions: Python 3.6
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: ethan.furman Nosy List: John Hagen, abarry, barry, docs@python, eli.bendersky, ethan.furman, rhettinger
Priority: normal Keywords: patch

Created on 2016-08-27 13:42 by John Hagen, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
enum_valueless_1.patch abarry, 2016-08-28 23:41 review
issue27877.johnthagen.02.patch John Hagen, 2016-08-29 11:28 review
Messages (8)
msg273780 - (view) Author: John Hagen (John Hagen) * Date: 2016-08-27 13:42
Many programming languages allow the developer to define a enum without specifying values when there would be no significance to those values.  Adding some kind of support in the stdlib itself was rejected due to the high degree of magic that would be needed: https://bugs.python.org/issue26988

I propose that a simple example be added to the enum docs that show a developer how to use a normal Python Enum to create this most common and basic enum (without values).

import enum

class Color(enum.Enum):
    red = object()
    green = object()
    blue = object()

object() returns a new unique value while conveying that that value should not be expected to be used in any meaningful way (unlike an integer value, which could be used to encode the Enum in some way).

There is no extra magic going on here, no new code that needs to be added to the stdlib, and no bare identifiers that could confuse linters.  For example, PyCharm already fully understands the above example and statically types it.

This example also allows the developer to omit the extra @enum.unique() boilerplate that is normally needed, since he or she cannot accidentally use the same integer value twice.
msg273818 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2016-08-28 08:47
After a little more thought, I think this would be a worthwhile recipe and would help communicate the notion that the value has no particular meaning.
msg273835 - (view) Author: John Hagen (John Hagen) * Date: 2016-08-28 18:38
Raymond, thanks for your consideration and input.

I'll work on a small patch unless I hear from Ethan that he'd rather do it.  I'm happy to defer to his expertise.

I did try out None as a value just to be sure that that didn't work as it would not be a bad alternative.  But since the values are "equal", Enum tries to alias them.

import enum

@enum.unique
class Color(enum.Enum):
    red = None
    green = None
    blue = None

>>> ValueError: duplicate values found in <enum 'Color'>: green -> red, blue -> red
msg273842 - (view) Author: John Hagen (John Hagen) * Date: 2016-08-28 21:03
Patch attached.
msg273847 - (view) Author: Anilyka Barry (abarry) * (Python triager) Date: 2016-08-28 23:41
The patch doesn't apply. I manually copy-pasted the lines in the source and generated a new one.

I would probably rephrase "these values hold no meaning and should not be used" into "the values are not important" or something along those lines.
msg273854 - (view) Author: John Hagen (John Hagen) * Date: 2016-08-29 11:28
Emanuel, I like your rewording.  Uploaded a new patch incorporating it.
msg274818 - (view) Author: Ethan Furman (ethan.furman) * (Python committer) Date: 2016-09-07 15:19
New changeset 871bdb06c1cf by Ethan Furman in branch 'default':
add recipes for pseudo-valueless enums
https://hg.python.org/cpython/rev/871bdb06c1cf
msg274821 - (view) Author: Ethan Furman (ethan.furman) * (Python committer) Date: 2016-09-07 15:23
Thanks for the patch, John and Emanuel!
History
Date User Action Args
2022-04-11 14:58:35adminsetgithub: 72064
2016-09-08 13:15:05berker.peksagsetstatus: open -> closed
resolution: fixed
stage: resolved
2016-09-07 15:23:34ethan.furmansetmessages: + msg274821
2016-09-07 15:19:42ethan.furmansetmessages: + msg274818
2016-08-29 15:27:30rhettingersetassignee: docs@python -> ethan.furman
2016-08-29 11:28:10John Hagensetfiles: + issue27877.johnthagen.02.patch

messages: + msg273854
2016-08-29 11:25:56John Hagensetfiles: - issue27877.johnthagen.01.patch
2016-08-28 23:41:50abarrysetfiles: + enum_valueless_1.patch
nosy: + abarry
messages: + msg273847

2016-08-28 21:03:40John Hagensetfiles: + issue27877.johnthagen.01.patch
keywords: + patch
messages: + msg273842
2016-08-28 18:38:23John Hagensetmessages: + msg273835
2016-08-28 08:47:30rhettingersetmessages: + msg273818
2016-08-28 08:46:18rhettingersetmessages: - msg273815
2016-08-28 08:14:05rhettingersetnosy: + rhettinger
messages: + msg273815
2016-08-27 13:42:09John Hagencreate