-
Notifications
You must be signed in to change notification settings - Fork 395
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
Shortcut syntax for choices with same label & data #526
Conversation
This would allow for a convenient shortcut syntax for entering choices. If label and data are both the same, you can do ['One', 'Two', 'Three'] instead of [('One', 'One'), ('Two', 'Two'), ('Three', 'Three')]
Your PR is failing the style check part of the CI. WTForms uses the Black code style. If this is still something you're interested in having included into WTForms please fix the code style. |
Looks like it just deleted a little bit of whitespace
Thanks for sorting that, I'll merge it now |
I am afraid there is a bug in it.. If the result is empty, the choices are effectively set to So the following lines produce some
|
def iter_choices(self):
if isinstance(self.choices[0], (list, tuple)):
choices = self.choices
else:
choices = zip(self.choices, self.choices)
for value, label in choices:
yield (value, label, self.coerce(value) == self.data) It is possible to restore the original functionality by introducing some def iter_choices(self):
if self.choices:
if isinstance(self.choices[0], (list, tuple)):
choices = self.choices
else:
choices = zip(self.choices, self.choices)
for value, label in choices:
yield (value, label, self.coerce(value) == self.data) I am not sure if it is too dirty for a fix, please also note #572 😄 |
…move it when merging this branch back. wtforms/wtforms#526 (comment)
The behavior of the SelectField changed in 2.3.0. We can't use the shortcut syntax with v2.2.0. And python3-wtforms on Debian 10 Bullseye is < 2.3.0. See wtforms/wtforms#526.
This would allow for a convenient shortcut syntax for entering choices. If label and data are both the same, you can do
['One', 'Two', 'Three']
instead of[('One', 'One'), ('Two', 'Two'), ('Three', 'Three')]