SETUP: Python 3.8.2; Some Enum Class with an overloaded __contains__() function.
from enum import Enum
class Something(Enum):
A = 1
def __contains__(self, Other):
return Other in self.__class__.__members__.keys()
TEST 1: Using values from the enum itself.
print("A:", Something.A in Something)
works fine (here result = A: True).
TEST 2: Using non-enum values.
print("1:", 1 in Something)
fails with an exception, namely
TypeError: unsupported operand type(s) for 'in': 'int' and 'EnumMeta'
QUESTION:
How is it possible to achieve an in functionality (membership operator) where the left operand can be anything? That is, it should be possible to write something like
if anything in Something:
...
without having to check the type of anything.