Есть ли способ использовать functools.singledispatch
со встроенной библиотекой enum Python?
Например:
from enum import Enum
from functools import singledispatch
class ContentType(Enum):
XML = 'application/xml'
JSON = 'application/json'
ct = ContentType
XML = ct.XML
JSON = ct.JSON
@singledispatch
def prepare_data(arg):
raise NotImplementedError()
@prepare_data.register
def _(arg: XML):
print('xml type')
@prepare_data.register
def _(arg: JSON):
print('json type')
prepare_data(JSON)
Это выдает ошибку TypeError:
TypeError: Invalid annotation for 'arg'. <ContentType: 'XML'> is not a class.
Полагаю, я мог бы создать подкласс Enum
, чтобы у каждого члена был свой тип, но это много работы.
Есть ли лучший способ?