EnumMetaclass should have a __instancecheck__ method

Bug #1172866 reported by Barry Warsaw
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
flufl.enum
Won't Fix
Undecided
Unassigned

Bug Description

There's been some discussion on python-dev that enum items pass an instance check against their enum class. E.g.

>>> X = Enum('X', 'a b c')
>>> Y = Enum('Y', 'z y x')
>>> class Z(Y):
... d = 4
... e = 5
...
>>> isinstance(Z.d, Y)
True
>>> isinstance(Z.d, Z)
True
>>> isinstance(Z.d, X)
False
>>> isinstance(Y.z, Y)
True
>>> isinstance(Y.z, Z)
False

To do this, we could add the following method to EnumMetaclass:

    def __instancecheck__(cls, instance):
        return instance.enum is cls or cls in instance.enum.__bases__

I haven't decided whether it's a good idea or not though, since the semantics seem weird to me.

Barry Warsaw (barry)
Changed in flufl.enum:
status: New → Won't Fix
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.