Skip to content

Commit 6d4dbf0

Browse files
dlstadtherTarrasch
authored andcommitted
Normalize ListParameter to be Immutable (spotify#1759)
* Add normalize to ListParameter * Add test for ListParameter normalization
1 parent 98860f5 commit 6d4dbf0

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

luigi/parameter.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -853,6 +853,15 @@ def run(self):
853853
854854
$ luigi --module my_tasks MyTask --grades '[100,70]'
855855
"""
856+
def normalize(self, x):
857+
"""
858+
Ensure that list parameter is converted to a tuple so it can be hashed.
859+
860+
:param str x: the value to parse.
861+
:return: the normalized (hashable/immutable) value.
862+
"""
863+
return tuple(x)
864+
856865
def parse(self, x):
857866
"""
858867
Parse an individual value from the input.

test/parameter_test.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,13 @@ class Foo(luigi.Task):
347347
p = luigi.parameter.DictParameter()
348348
self.assertEqual(hash(Foo(args=dict(foo=1, bar="hello")).args), hash(p.parse('{"foo":1,"bar":"hello"}')))
349349

350+
def test_list(self):
351+
class Foo(luigi.Task):
352+
args = luigi.parameter.ListParameter()
353+
354+
p = luigi.parameter.ListParameter()
355+
self.assertEqual(hash(Foo(args=[1, "hello"]).args), hash(p.normalize(p.parse('[1,"hello"]'))))
356+
350357
def test_tuple(self):
351358
class Foo(luigi.Task):
352359
args = luigi.parameter.TupleParameter()

0 commit comments

Comments
 (0)