Skip to content

Commit

Permalink
ChoiceParameter checks if option is valid within .normalize (#2454)
Browse files Browse the repository at this point in the history
  • Loading branch information
jotinha authored and Tarrasch committed Jul 10, 2018
1 parent d994bca commit bdfcb6c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
7 changes: 5 additions & 2 deletions luigi/parameter.py
Original file line number Diff line number Diff line change
Expand Up @@ -1201,8 +1201,11 @@ def __init__(self, var_type=str, *args, **kwargs):

def parse(self, s):
var = self._var_type(s)
return self.normalize(var)

def normalize(self, var):
if var in self._choices:
return var
else:
raise ValueError("{s} is not a valid choice from {choices}".format(
s=s, choices=self._choices))
raise ValueError("{var} is not a valid choice from {choices}".format(
var=var, choices=self._choices))
5 changes: 5 additions & 0 deletions test/choice_parameter_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,8 @@ def test_serialize_parse(self):
a = luigi.ChoiceParameter(var_type=str, choices=["1", "2", "3"])
b = "3"
self.assertEqual(b, a.parse(a.serialize(b)))

def test_invalid_choice_task(self):
class Foo(luigi.Task):
args = luigi.ChoiceParameter(var_type=str, choices=["1", "2", "3"])
self.assertRaises(ValueError, lambda: Foo(args="4"))

0 comments on commit bdfcb6c

Please sign in to comment.