trafaret_forked icon indicating copy to clipboard operation
trafaret_forked copied to clipboard

Fill t.Enum variants from Python standard library enum.Enum instances

Open playpauseandstop opened this issue 8 years ago • 1 comments

Hi.

What about adding syntax sugar for filling t.Enum variants from Python's enum.Enum? Right now it is doable, but not very reusbale,

>>> import enum
>>> import trafaret as t
>>> class PyEnum(Enum):
...    first = 'first-value'
...    second = 'second-value'
>>> trafaret = t.Enum(*(item.value for item in PyEnum.__members__.values()))
>>> trafaret.check('first-value')
'first-value'
>>> t.extract_error(trafaret, 'first')
"value doesn't match any variant"

From my POV, it'd be more user-friendly if t.Enum accepts Python enum.Enum as argument and automatically extract its values.

What do you think?

playpauseandstop avatar Apr 24 '17 14:04 playpauseandstop

I think this is ok. Want to do PR?

Deepwalker avatar Jun 06 '17 18:06 Deepwalker