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: Can create enum of ranges, cannot create range enum. Range should be subclassable... or EnumMeta.__new__ should be smarter.
Type: enhancement Stage: resolved
Components: Documentation Versions: Python 3.11
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: ethan.furman Nosy List: ethan.furman, sobolevn, techdragon
Priority: normal Keywords: patch

Created on 2021-09-30 13:57 by techdragon, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 30521 merged sobolevn, 2022-01-10 23:29
Messages (2)
msg402960 - (view) Author: Sam Bishop (techdragon) Date: 2021-09-30 13:57
Range types are perfectly valid as values in an enum, like so.

class EnumOfRanges(Enum):
    ZERO = range(0, 0)
    RANGE_A = range(1, 11)
    RANGE_B = range(11, 26)


However unlike the other base types , 'int', 'str', 'float', etc. You cannot create a "range enum" 

class RangeEnum(range, Enum):
    ZERO = range(0, 0)
    RANGE_A = range(1, 11)
    RANGE_B = range(11, 26)

produces `TypeError: type 'range' is not an acceptable base type` when you try and import `RangeEnum`.

The current documentation for `enum` implicitly says this should work by not mentioning anything special here https://docs.python.org/3/library/enum.html#others

It also allows the use of range objects as value types, another implicit suggestion that we should be able to restrict an enum class to just range values like we can for other builtin class types.

Also to keep this a concise issue:
- Yes I'm aware not all of the base classes can be subclassed.
- Yes I know I that there are good reasons bool should not be subclassable.

So I'd like to suggest one of three things should be done to improve the situation:

A: Solve https://bugs.python.org/issue17279 and by documenting the special base class objects that cannot be subclassed and reference this in the documentation for Enums.

B: Make a decision as to which base class objects we should be able to subclass, and then improve their C implementations to allow subclassing. (It's also probably worth documenting the final list of special objects and solving https://bugs.python.org/issue17279 should this approach be selected.) 

C: The __new__ method on EnumMeta should be made smarter so that it either emits a more useful warning (I had to head to the CPython source code to work out what the error `TypeError: type 'range' is not an acceptable base type` meant) or somehow being more smart about how it handles the special classes which can't cannot be subclassed allowing them to be used anyway.  which again sort of involves solving https://bugs.python.org/issue17279, and in the case that its just made magically smarter, I'll admit could confuse some people as to why "Enum" is special and can subclass these but their own code can't just do `class MyRange(range):` 

Regardless of the outcome, it would be good to fill in this pitfall one way or the other for the sake of future developers, I'm a reasonably experienced Python developer and it caught me by surprise I'm likely not the first and probably wont be the last if the behaviour remains as it currently is.
msg410266 - (view) Author: Ethan Furman (ethan.furman) * (Python committer) Date: 2022-01-10 23:42
New changeset 6223cbf86ad7d5e6d12f9747e5a9cf1d8c72bdc8 by Nikita Sobolev in branch 'main':
bpo-45331: [Enum] add rule to docs that mixin type must be subclassable (GH-30521)
https://github.com/python/cpython/commit/6223cbf86ad7d5e6d12f9747e5a9cf1d8c72bdc8
History
Date User Action Args
2022-04-11 14:59:50adminsetgithub: 89494
2022-01-12 22:11:13AlexWaygoodsetstatus: open -> closed
resolution: fixed
components: + Documentation, - Interpreter Core
stage: patch review -> resolved
2022-01-10 23:42:58ethan.furmansetmessages: + msg410266
2022-01-10 23:29:16sobolevnsetkeywords: + patch
nosy: + sobolevn

pull_requests: + pull_request28722
stage: patch review
2021-09-30 17:46:19ethan.furmansetassignee: ethan.furman

nosy: + ethan.furman
versions: + Python 3.11, - Python 3.9
2021-09-30 13:57:43techdragoncreate