Skip to content

Shortcut syntax for choices with same label & data#526

Merged
ftm merged 2 commits intopallets-eco:masterfrom
dominickj-tdi:patch-1
Dec 17, 2019
Merged

Shortcut syntax for choices with same label & data#526
ftm merged 2 commits intopallets-eco:masterfrom
dominickj-tdi:patch-1

Conversation

@dominickj-tdi
Copy link
Copy Markdown
Contributor

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
Copy Markdown
Contributor

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 New feature, or existing feature improvement 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
Copy Markdown
Contributor

ftm commented Dec 17, 2019

Thanks for sorting that, I'll merge it now

@ftm ftm merged commit 597fa77 into pallets-eco:master Dec 17, 2019
azmeuk added a commit to azmeuk/wtforms that referenced this pull request Apr 20, 2020
@spookey
Copy link
Copy Markdown

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
Copy Markdown

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 pull request Apr 23, 2020
AdrianVollmer added a commit to AdrianVollmer/Crack-O-Matic that referenced this pull request 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 pallets-eco/wtforms#526.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature, or existing feature improvement

Development

Successfully merging this pull request may close these issues.

3 participants