Skip to content

Improve SelectField enum coerce#926

Open
azmeuk wants to merge 3 commits into
pallets-eco:mainfrom
azmeuk:select-enum
Open

Improve SelectField enum coerce#926
azmeuk wants to merge 3 commits into
pallets-eco:mainfrom
azmeuk:select-enum

Conversation

@azmeuk

@azmeuk azmeuk commented Jun 1, 2026

Copy link
Copy Markdown
Member

Implementing suggestions from #922. No more implicit magic for coerce= with Enum values, SelectChoice.coerce_by_name is 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...

@azmeuk azmeuk changed the title Drop SelectField magic Enum coerce wrapping, add SelectChoice.c… Improve SelectField enum coerce Jun 1, 2026
@azmeuk azmeuk requested a review from Copilot June 1, 2026 15:01

This comment was marked as off-topic.

@Daverball

Copy link
Copy Markdown

Looks good to me. I also can't think of a better name for the helper method, apart from maybe coerce_enum_by_name, to really drive home that this method is for enums, but the name also doesn't seem that bad to me in the first place, the only slightly awkward thing about it, is that SelectChoice may not be the most natural place to look for such a method, maybe it makes more sense to define it on SelectField itself?

@miketheman miketheman left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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),
  ),

Comment thread docs/fields.rst Outdated
azmeuk and others added 2 commits June 4, 2026 15:53
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
@azmeuk

azmeuk commented Jun 9, 2026

Copy link
Copy Markdown
Member Author

After giving some more thoughts I went to two independent enum_choices and enum_coerce methods with a by attribute, so either the enum label or the enum value can be used as form value.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

4 participants