classification
Title: PEP0435 (enums) -- there is no standard on enum item letters case
Type: Stage: resolved
Components: Documentation Versions: Python 3.10, Python 3.9, Python 3.8
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: docs@python Nosy List: DahlitzFlorian, barry, docs@python, eli.bendersky, ethan.furman, miss-islington, remi.lapeyre, socketpair
Priority: normal Keywords: patch

Created on 2020-05-21 21:42 by socketpair, last changed 2020-09-14 20:51 by ethan.furman. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 22231 merged ethan.furman, 2020-09-13 21:06
PR 22247 merged miss-islington, 2020-09-14 20:34
PR 22248 merged miss-islington, 2020-09-14 20:34
Messages (8)
msg369544 - (view) Author: Марк Коренберг (socketpair) * Date: 2020-05-21 21:42
Example from PEP0435:

(https://www.python.org/dev/peps/pep-0435)

>>> from enum import Enum
>>> class Color(Enum):
...     red = 1
...     green = 2
...     blue = 3

Example from Python documentation:

(https://docs.python.org/3/library/enum.html)

>>> from enum import Enum
>>> class Color(Enum):
...     RED = 1
...     GREEN = 2
...     BLUE = 3
...

So, what are the rules for naming enum members?
CamelCase ? snake_case ? ALL_CAPS ?

Someone should explain how should we format sources. So various linters may check for that.
msg369545 - (view) Author: Марк Коренберг (socketpair) * Date: 2020-05-21 21:44
FYI: PEP8 does not mention enums.
msg369546 - (view) Author: Rémi Lapeyre (remi.lapeyre) * Date: 2020-05-21 21:50
There is no official way, you can find both in the Python tree: all caps at https://github.com/python/cpython/blob/master/Lib/ast.py#L615-L636 and snake case at https://github.com/python/cpython/blob/master/Lib/uuid.py#L76-L79.

I think it's common to use all caps for those that are used as flags (i.e. where each value is a power of 2 and that may be combined using bigs operations) but there is no one true way that you can enforced, it depends on the context. 

Also, note that pep8 covers the code of Python, it's not a rule that must be blindly applied to all Python code and that you may ignore it for various reasons: https://www.python.org/dev/peps/pep-0008/#a-foolish-consistency-is-the-hobgoblin-of-little-minds.
msg369565 - (view) Author: Florian Dahlitz (DahlitzFlorian) * Date: 2020-05-22 08:10
Should we add a note that there is no preferred way of naming enum members and provide a few examples or define some naming conventions?
msg376855 - (view) Author: Ethan Furman (ethan.furman) * (Python committer) Date: 2020-09-13 20:56
For Python code at least, Guido has proclaimed:

https://mail.python.org/pipermail/python-ideas/2016-September/042340.html

I recommend naming all enums UPPER_CASE. They're constants (within a
namespace) and that's the rule for constants. It's helpful for the
reader of the code to realize what they are when passed around -- they
have a similar status to literal constants, you know they stand for a
unique value and not for some computed quantity.
msg376901 - (view) Author: Ethan Furman (ethan.furman) * (Python committer) Date: 2020-09-14 20:32
New changeset 542e1df2b018ee7068dba8076f2d6e84efd6e144 by Ethan Furman in branch 'master':
bpo-40721: add note about enum member name case (GH-22231)
https://github.com/python/cpython/commit/542e1df2b018ee7068dba8076f2d6e84efd6e144
msg376902 - (view) Author: miss-islington (miss-islington) Date: 2020-09-14 20:42
New changeset 624cc10ee4aae513be9f2812f6f5621b6f32f17c by Miss Islington (bot) in branch '3.8':
bpo-40721: add note about enum member name case (GH-22231)
https://github.com/python/cpython/commit/624cc10ee4aae513be9f2812f6f5621b6f32f17c
msg376903 - (view) Author: miss-islington (miss-islington) Date: 2020-09-14 20:49
New changeset b502c7618d710d31517a7ee6a72890d0963e357a by Miss Islington (bot) in branch '3.9':
bpo-40721: add note about enum member name case (GH-22231)
https://github.com/python/cpython/commit/b502c7618d710d31517a7ee6a72890d0963e357a
History
Date User Action Args
2020-09-14 20:51:34ethan.furmansetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2020-09-14 20:49:33miss-islingtonsetmessages: + msg376903
2020-09-14 20:42:45miss-islingtonsetmessages: + msg376902
2020-09-14 20:34:17miss-islingtonsetpull_requests: + pull_request21304
2020-09-14 20:34:07miss-islingtonsetnosy: + miss-islington
pull_requests: + pull_request21303
2020-09-14 20:32:59ethan.furmansetmessages: + msg376901
2020-09-13 21:06:06ethan.furmansetkeywords: + patch
stage: patch review
pull_requests: + pull_request21286
2020-09-13 20:56:52ethan.furmansetmessages: + msg376855
2020-05-22 08:12:54xtreaksetnosy: + barry, eli.bendersky, ethan.furman
2020-05-22 08:10:06DahlitzFloriansetnosy: + DahlitzFlorian
messages: + msg369565
2020-05-21 21:50:03remi.lapeyresetnosy: + remi.lapeyre
messages: + msg369546
2020-05-21 21:44:35socketpairsetmessages: + msg369545
2020-05-21 21:42:56socketpaircreate