Skip to content

Commit

Permalink
fix: remove deprecation warnings for tuple and dict usage on SelectFi…
Browse files Browse the repository at this point in the history
…eld choice
  • Loading branch information
azmeuk committed Sep 30, 2023
1 parent 633d164 commit f9337a5
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 27 deletions.
15 changes: 0 additions & 15 deletions src/wtforms/fields/choices.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import warnings
from dataclasses import dataclass
from typing import Optional

Expand Down Expand Up @@ -49,12 +48,6 @@ def from_input(cls, input, optgroup=None):
return Choice(value=input, optgroup=optgroup)

if isinstance(input, tuple):
warnings.warn(
"Passing SelectField choices as tuples is deprecated and will be "
"removed in wtforms 3.3. Please use Choice instead.",
DeprecationWarning,
stacklevel=2,
)
return Choice(*input, optgroup=optgroup)


Expand Down Expand Up @@ -107,14 +100,6 @@ def choices_from_input(self, choices):
return None

if isinstance(choices, dict):
warnings.warn(
"Passing SelectField choices in a dict deprecated and will be removed "
"in wtforms 3.3. Please pass a list of Choice objects with a "
"custom optgroup attribute instead.",
DeprecationWarning,
stacklevel=2,
)

return [
Choice.from_input(input, optgroup)
for optgroup, inputs in choices.items()
Expand Down
18 changes: 6 additions & 12 deletions tests/fields/test_select.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,20 +290,16 @@ def test_optgroup_option_render_kw():
]


def test_tuple_choices_deprecation():
def test_tuple_choices():
F = make_form(a=SelectField(choices=[("a", "Foo")]))
with pytest.warns(DeprecationWarning):
form = F(a="a")

form = F(a="a")
assert '<option selected value="a">Foo</option>' in form.a()
assert list(form.a.iter_choices()) == [Choice("a", "Foo", None, None, True)]


def test_dict_choices_deprecation_with_choice_object():
def test_dict_choices_with_choice_object():
F = make_form(a=SelectField(choices={"hello": [Choice("a", "Foo")]}))
with pytest.warns(DeprecationWarning):
form = F(a="a")

form = F(a="a")
assert (
'<optgroup label="hello">'
'<option selected value="a">Foo</option>'
Expand All @@ -312,11 +308,9 @@ def test_dict_choices_deprecation_with_choice_object():
assert list(form.a.iter_choices()) == [Choice("a", "Foo", None, "hello", True)]


def test_dict_choices_deprecation_with_tuple():
def test_dict_choices_with_tuple():
F = make_form(a=SelectField(choices={"hello": [("a", "Foo")]}))
with pytest.warns(DeprecationWarning):
form = F(a="a")

form = F(a="a")
assert (
'<optgroup label="hello">'
'<option selected value="a">Foo</option>'
Expand Down

0 comments on commit f9337a5

Please sign in to comment.