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

Shortcut syntax for choices with same label & data #526

Merged
merged 2 commits into from Dec 17, 2019

Conversation

dominickj-tdi
Copy link
Contributor

@dominickj-tdi dominickj-tdi commented Nov 21, 2019

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')]

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')]
@ftm
Copy link
Contributor

@ftm ftm commented Dec 17, 2019

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.

@ftm ftm added enhancement waiting labels Dec 17, 2019
Looks like it just deleted a little bit of whitespace
@ftm ftm removed the waiting label Dec 17, 2019
@ftm
Copy link
Contributor

@ftm ftm commented Dec 17, 2019

Thanks for sorting that, I'll merge it now

@ftm ftm merged commit 597fa77 into wtforms:master Dec 17, 2019
1 check passed
azmeuk added a commit to azmeuk/wtforms that referenced this issue Apr 20, 2020
@spookey
Copy link

@spookey spookey commented Apr 22, 2020

I am afraid there is a bug in it..
In my application I am filling the choices of some select field with elements coming from a database query.

If the result is empty, the choices are effectively set to [].

So the following lines produce some IndexError in this case - there is no index 0 in an empty list:

def iter_choices(self):
    if isinstance(self.choices[0], (list, tuple)):

@spookey
Copy link

@spookey spookey commented Apr 22, 2020

    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 if self.choices: check before:

    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 😄

spookey added a commit to spookey/observatory that referenced this issue Apr 23, 2020
AdrianVollmer added a commit to AdrianVollmer/Crack-O-Matic that referenced this issue Apr 10, 2021
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement
Development

Successfully merging this pull request may close these issues.

None yet

3 participants