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

Validate str type before tuple cast #3323

Merged
merged 3 commits into from
Nov 30, 2024
Merged

Conversation

ikyasam18
Copy link
Contributor

Description

Add string type validation for tuple parameter parsing to prevent unintended string splitting behavior.

Motivation and Context

Currently, when parsing a string input like '("abcd")' with TupleParameter(), it incorrectly splits the string into individual characters:

# Current behavior (incorrect)
>>> luigi.TupleParameter().parse('("abcd")')
('a', 'b', 'c', 'd')

# Expected behavior
>>> luigi.TupleParameter().parse('("abcd",)')
('abcd',)

This change adds validation to ensure proper tuple formatting and prevents incorrect string splitting. Now it raises a ValueError when detecting improperly formatted input:

>>> luigi.TupleParameter().parse('("abcd")')
ValueError: Parsed result cannot be a string

Have you tested this? If so, how?

I have tested this change by:

  1. Adding unit tests to verify the error handling for improperly formatted string inputs
  2. Confirming that properly formatted tuple strings like '("abcd",)' still parse correctly
  3. Verifying that a ValueError is raised for invalid inputs using the Python interpreter
>>> luigi.TupleParameter().parse('("abcd")')
Traceback (most recent call last):
  File "C:\Users\MasayukiKamoda\Desktop\DESK_work\develop\luigi\luigi\parameter.py", line 1385, in parse
    return tuple(self._convert_iterable_to_tuple(x) for x in json.loads(x, object_pairs_hook=FrozenOrderedDict))
                                                             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "E:\dev\anaconda3\Lib\json\__init__.py", line 359, in loads
    return cls(**kw).decode(s)
           ^^^^^^^^^^^^^^^^^^^
  File "E:\dev\anaconda3\Lib\json\decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "E:\dev\anaconda3\Lib\json\decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\MasayukiKamoda\Desktop\DESK_work\develop\luigi\luigi\parameter.py", line 1391, in parse
    raise ValueError("Parsed result cannot be a string")
ValueError: Parsed result cannot be a string

@ikyasam18 ikyasam18 requested review from dlstadther and a team as code owners November 26, 2024 08:14
@ikyasam18 ikyasam18 changed the title Draft: Validate str type before tuple cast Validate str type before tuple cast Nov 26, 2024
@dlstadther dlstadther merged commit 80549f6 into spotify:master Nov 30, 2024
48 checks passed
@ikyasam18
Copy link
Contributor Author

@dlstadther
Thank you for merging the PR. I hope it will be released soon.

@dlstadther
Copy link
Collaborator

@ikyasam18 , this was released as apart of 3.6.0

The release is also on PyPi.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants