Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SelectField will throw ValueError when using list of strings #598

merged 3 commits into from Jun 14, 2020


Copy link

@tresni tresni commented Apr 26, 2020

This may assist with #464 (at least the behavior described by @ftm in that ticket.)

In the validation phase, we use iter_choices now instead of trying to iterate on choices directly. This will mean that regardless of list of tuples or the shortcut list of strings, we always have 3 values when we iterate the list. We use the match result from iter_choices instead of doing the comparison again to save the operation.

If you are using the shortcut list of strings and getting 'too many values to unpack (expected 2)', this is the fix.

I'm doing the following to work around this right now:

class TimeZoneForm(FlaskForm):
    timezone = SelectField('Timezone', choices=list(zip(listTimezones(), listTimezones())))

list(zip( is required to prevent choices from being an iterator. listTimezones() is a singleton function that returns a cached list of strings. Without the list(zip( or this patch, this will will always fail to validate.

tresni added 3 commits Apr 26, 2020
This give a consistent output of 3 values where using just choices leads to ValueError when choices is a list of strings. By using match from iter_choices we also keep from doing the check twice so there should be no costs here except for the zip() when using lists of strings.
@azmeuk azmeuk merged commit 5db7dfb into wtforms:master Jun 14, 2020
6 checks passed
@davidism davidism added this to the 3.0.0 milestone Jun 16, 2020
azmeuk added a commit to azmeuk/wtforms that referenced this issue Jul 29, 2020
@azmeuk azmeuk mentioned this pull request Jul 29, 2020
azmeuk added a commit that referenced this issue Jul 29, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet

Successfully merging this pull request may close these issues.

None yet

3 participants