Skip to content

Commit

Permalink
don't convert str to tuple in TupleParameter (spotify#3275)
Browse files Browse the repository at this point in the history
* fix: don't convert str to tuple in TupleParameter

* fix: don't compare message in other library.

jsonschema validation error message was changed. I think it is very flaky test.
  • Loading branch information
kitagry authored Jan 23, 2024
1 parent 367edc2 commit 25d179b
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
7 changes: 6 additions & 1 deletion luigi/parameter.py
Original file line number Diff line number Diff line change
Expand Up @@ -1379,10 +1379,15 @@ def parse(self, x):
# ast.literal_eval(t_str) == t
try:
# loop required to parse tuple of tuples
return tuple(tuple(x) for x in json.loads(x, object_pairs_hook=FrozenOrderedDict))
return tuple(self._convert_iterable_to_tuple(x) for x in json.loads(x, object_pairs_hook=FrozenOrderedDict))
except (ValueError, TypeError):
return tuple(literal_eval(x)) # if this causes an error, let that error be raised.

def _convert_iterable_to_tuple(self, x):
if isinstance(x, str):
return x
return tuple(x)


class OptionalTupleParameter(OptionalParameterMixin, TupleParameter):
"""Class to parse optional tuple parameters."""
Expand Down
2 changes: 1 addition & 1 deletion test/list_parameter_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def test_schema(self):
a.normalize(["INVALID_ATTRIBUTE"])

# Check that empty list is not valid
with pytest.raises(ValidationError, match=r"\[\] is too short"):
with pytest.raises(ValidationError):
a.normalize([])

# Check that valid lists work
Expand Down
8 changes: 8 additions & 0 deletions test/parameter_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,14 @@ class Foo(luigi.Task):
self.assertEqual(hash(Foo(args=(('foo', 'bar'), ('doge', 'wow'))).args),
hash(p.normalize(p.parse('(("foo", "bar"), ("doge", "wow"))'))))

def test_tuple_string_with_json(self):
class Foo(luigi.Task):
args = luigi.parameter.TupleParameter()

p = luigi.parameter.TupleParameter()
self.assertEqual(hash(Foo(args=('foo', 'bar')).args),
hash(p.normalize(p.parse('["foo", "bar"]'))))

def test_task(self):
class Bar(luigi.Task):
pass
Expand Down

0 comments on commit 25d179b

Please sign in to comment.