Improve SelectField enum coerce#926
Conversation
|
Looks good to me. I also can't think of a better name for the helper method, apart from maybe |
miketheman
left a comment
There was a problem hiding this comment.
I haven't tried the code yet, but thinking about naming, how might this look?
class PaintForm(Form):
color = SelectField(
choices=SelectChoice.from_enum(Color),
coerce=SelectChoice.enum_name(Color),
)That allows for some future enum_value that handles the other pattern.
The UX still "feels weird" overall - since I'm not asking to coerce one type into another type (like the coerce_none example in the docs), rather I'm asking the SelectField choices options of (value, label, render_kw) to be populated with some part of an enum, so maybe the overall from_enum could do it for me, instead of only allowing the label input to affect what's displayed, but also the value?
class PaintForm(Form):
color = SelectField(
choices=SelectChoice.from_enum(
Color,
label=some_callable,
value=some_other_callable),
),Co-authored-by: Mike Fiedler <miketheman@gmail.com>
migrate SelectChoice.from_enum to enum_choices, and add a symmetric method enum_coerce for enum choices choices coercion
|
After giving some more thoughts I went to two independent |
Implementing suggestions from #922. No more implicit magic for
coerce=withEnumvalues,SelectChoice.coerce_by_nameis introduced to be used instead (coerce=SelectChoice.coerce_by_name). I am not delighted with the name but cannot find a better one./cc @miketheman @Daverball
edit: that damned Copilot review button...