[issue45331] Can create enum of ranges, cannot create range enum. Range should be subclassable... or EnumMeta.__new__ should be smarter.

Sam Bishop report at bugs.python.org
Thu Sep 30 09:57:43 EDT 2021


New submission from Sam Bishop <sam at psyx.co>:

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.

----------
components: Interpreter Core
messages: 402960
nosy: techdragon
priority: normal
severity: normal
status: open
title: Can create enum of ranges, cannot create range enum. Range should be subclassable... or EnumMeta.__new__ should be smarter.
type: enhancement
versions: Python 3.9

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue45331>
_______________________________________


More information about the Python-bugs-list mailing list