Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

don't convert str to tuple in TupleParameter #3275

Merged
merged 2 commits into from
Jan 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
# 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))

Check warning on line 1382 in luigi/parameter.py

View check run for this annotation

Codecov / codecov/patch

luigi/parameter.py#L1382

Added line #L1382 was not covered by tests
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)

Check warning on line 1389 in luigi/parameter.py

View check run for this annotation

Codecov / codecov/patch

luigi/parameter.py#L1387-L1389

Added lines #L1387 - L1389 were not covered by tests


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):
Copy link
Contributor Author

@kitagry kitagry Jan 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CI failed because jsonschema validation error is changed.

I think the message should not be compared because it will be very flaky test. Do you think about this?

# before
with pytest.raises(ValidationError, match=r"\[\] is too short"):
    a.normalize([])

# after
with pytest.raises(ValidationError, '\[\] should be non-empty'):
    a.normalize([])

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
Loading