Skip to content

Commit

Permalink
Normalize ListParameter to be Immutable (#1759)
Browse files Browse the repository at this point in the history
* Add normalize to ListParameter

* Add test for ListParameter normalization
  • Loading branch information
dlstadther authored and Tarrasch committed Jul 13, 2016
1 parent 98860f5 commit 6d4dbf0
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
9 changes: 9 additions & 0 deletions luigi/parameter.py
Original file line number Diff line number Diff line change
Expand Up @@ -853,6 +853,15 @@ def run(self):
$ luigi --module my_tasks MyTask --grades '[100,70]'
"""
def normalize(self, x):
"""
Ensure that list parameter is converted to a tuple so it can be hashed.
:param str x: the value to parse.
:return: the normalized (hashable/immutable) value.
"""
return tuple(x)

def parse(self, x):
"""
Parse an individual value from the input.
Expand Down
7 changes: 7 additions & 0 deletions test/parameter_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,13 @@ class Foo(luigi.Task):
p = luigi.parameter.DictParameter()
self.assertEqual(hash(Foo(args=dict(foo=1, bar="hello")).args), hash(p.parse('{"foo":1,"bar":"hello"}')))

def test_list(self):
class Foo(luigi.Task):
args = luigi.parameter.ListParameter()

p = luigi.parameter.ListParameter()
self.assertEqual(hash(Foo(args=[1, "hello"]).args), hash(p.normalize(p.parse('[1,"hello"]'))))

def test_tuple(self):
class Foo(luigi.Task):
args = luigi.parameter.TupleParameter()
Expand Down

0 comments on commit 6d4dbf0

Please sign in to comment.