Skip to content

feat: add parse_kv_pairs function to sessions utils#121

Open
DeanChensj wants to merge 1 commit intogoogle:mainfrom
DeanChensj:test-gemini-review-pr
Open

feat: add parse_kv_pairs function to sessions utils#121
DeanChensj wants to merge 1 commit intogoogle:mainfrom
DeanChensj:test-gemini-review-pr

Conversation

@DeanChensj
Copy link
Copy Markdown
Collaborator

No description provided.

@DeanChensj
Copy link
Copy Markdown
Collaborator Author

@gemini-cli /review

@github-actions
Copy link
Copy Markdown

github-actions Bot commented May 5, 2026

🤖 Hi @DeanChensj, I've received your request, and I'm working on it now! You can track my progress in the logs for more details.

@github-actions
Copy link
Copy Markdown

github-actions Bot commented May 5, 2026

🤖 I'm sorry @DeanChensj, but I was unable to process your request. Please see the logs for more details.

@DeanChensj
Copy link
Copy Markdown
Collaborator Author

@gemini-cli /review

@github-actions
Copy link
Copy Markdown

github-actions Bot commented May 5, 2026

🤖 Hi @DeanChensj, I've received your request, and I'm working on it now! You can track my progress in the logs for more details.

@github-actions
Copy link
Copy Markdown

github-actions Bot commented May 5, 2026

🤖 I'm sorry @DeanChensj, but I was unable to process your request. Please see the logs for more details.

@DeanChensj
Copy link
Copy Markdown
Collaborator Author

@gemini-cli /review

@github-actions
Copy link
Copy Markdown

github-actions Bot commented May 5, 2026

🤖 Hi @DeanChensj, I've received your request, and I'm working on it now! You can track my progress in the logs for more details.

Copy link
Copy Markdown

@github-actions github-actions Bot left a comment

Choose a reason for hiding this comment

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

I've added some comments regarding robustness and missing tests. The function is a good addition, but it needs to be more resilient to malformed input and edge cases like values containing colons. Additionally, please include unit tests.

return str(obj)


def parse_kv_pairs(data: str) -> dict:
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Please add unit tests for this new utility function to ensure it handles various input formats and edge cases correctly.

def parse_kv_pairs(data: str) -> dict:
"""Parses comma-separated key-value pairs into a dictionary."""
result = {}
parts = data.split(",")
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

If data has trailing commas or double commas, split(",") will produce empty strings, which will then cause part.split(":") to fail. You might want to filter out empty parts: parts = [p for p in data.split(",") if p.strip()].

result = {}
parts = data.split(",")
for part in parts:
key, value = part.split(":")
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Consider using part.split(":", 1) to handle cases where the value might contain a colon (e.g., a URL). Currently, this will raise a ValueError: too many values to unpack in such cases.

Copy link
Copy Markdown

@github-actions github-actions Bot left a comment

Choose a reason for hiding this comment

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

Thank you for the contribution! I've reviewed the code and found a few areas for improvement:

  1. Error Handling: The current implementation of parse_kv_pairs is fragile. It will raise a ValueError if the input string contains trailing commas (resulting in empty parts), missing colons, or multiple colons in a single pair. I recommend adding validation or using a more robust splitting strategy.
  2. Missing Tests: No unit tests were provided for this new utility function. Please add tests covering both happy paths and edge cases (empty strings, malformed pairs, etc.).
  3. Typing: Precision in typing (e.g., dict[str, str]) would be beneficial.

Please address these comments.

def parse_kv_pairs(data: str) -> dict:
"""Parses comma-separated key-value pairs into a dictionary."""
result = {}
parts = data.split(",")
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This will fail if any part doesn't contain exactly one colon (e.g., empty parts from trailing commas, or malformed input like "key1"). Consider adding a check or using part.partition(':') or part.split(':', 1) and validating the result.

if isinstance(obj, Decimal):
return float(obj)
return str(obj)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

It's a good practice to include unit tests for new utility functions to ensure they handle various edge cases (like empty strings, trailing commas, or spaces).

if isinstance(obj, Decimal):
return float(obj)
return str(obj)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

For better type safety, you might want to use dict[str, str] as the return type.

Copy link
Copy Markdown

@github-actions github-actions Bot left a comment

Choose a reason for hiding this comment

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

The parse_kv_pairs function is a useful addition, but it needs to be more robust to handle edge cases like empty strings, malformed input (missing colons), and values that might contain colons. Additionally, please include unit tests to verify the functionality and prevent regressions.

result = {}
parts = data.split(",")
for part in parts:
key, value = part.split(":")
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This line is prone to ValueError. It will fail if a part doesn't contain a colon (e.g., "key") or contains multiple colons (e.g., "key:value:extra"). I recommend using part.split(":", 1) and checking if the result has exactly 2 elements, or using part.partition(":").

def parse_kv_pairs(data: str) -> dict:
"""Parses comma-separated key-value pairs into a dictionary."""
result = {}
parts = data.split(",")
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This will raise a ValueError if the input data is an empty string, because "".split(",") returns [''], and '' does not contain a colon. Consider adding a check for if not data: return {}.

return str(obj)


def parse_kv_pairs(data: str) -> dict:
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Please add unit tests for this new function in tests/unittests/sessions/test_utils.py (or a similar appropriate test file) to ensure it handles various input formats and edge cases correctly.

@DeanChensj
Copy link
Copy Markdown
Collaborator Author

@gemini-cli /review

@github-actions
Copy link
Copy Markdown

github-actions Bot commented May 5, 2026

🤖 Hi @DeanChensj, I've received your request, and I'm working on it now! You can track my progress in the logs for more details.

Copy link
Copy Markdown

@github-actions github-actions Bot left a comment

Choose a reason for hiding this comment

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

Thanks for the contribution! The parse_kv_pairs function is a useful addition, but it needs some robustness improvements to handle edge cases and malformed input. Specifically:

  1. Error Handling: The current implementation will crash if a part doesn't contain a colon or contains multiple colons.
  2. Empty Input: An empty string will also cause a crash.
  3. Type Hinting: More specific type hints would be beneficial.

Please see the line-specific comments for suggestions.

def parse_kv_pairs(data: str) -> dict:
"""Parses comma-separated key-value pairs into a dictionary."""
result = {}
parts = data.split(",")
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

If data is an empty string, data.split(",") will return ['']. This will cause an error on line 45 because an empty string doesn't contain a colon. Consider adding a check for empty input at the beginning of the function.

result = {}
parts = data.split(",")
for part in parts:
key, value = part.split(":")
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This line will raise a ValueError in two cases:

  1. If a part doesn't contain a colon (e.g., "key").
  2. If a part contains more than one colon (e.g., "url:https://google.com").

Consider using part.split(":", 1) to handle multiple colons and adding validation to handle missing colons gracefully.

return str(obj)


def parse_kv_pairs(data: str) -> dict:
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Consider using a more specific type hint like Dict[str, str] (imported from typing) to improve clarity on the return type.

@github-actions
Copy link
Copy Markdown

github-actions Bot commented May 5, 2026

🤖 Hi @DeanChensj, I've received your request, and I'm working on it now! You can track my progress in the logs for more details.

Copy link
Copy Markdown

@github-actions github-actions Bot left a comment

Choose a reason for hiding this comment

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

I have reviewed the changes. I found a few potential issues with the parse_kv_pairs function that should be addressed to make it more robust.

Copy link
Copy Markdown

@github-actions github-actions Bot left a comment

Choose a reason for hiding this comment

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

The parse_kv_pairs function needs some improvements to handle edge cases like empty input strings and malformed key-value pairs. I've added some specific comments in the diff. Please also consider adding unit tests for this function.

parts = data.split(",")
for part in parts:
key, value = part.split(":")
result[key.strip()] = value.strip()
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

If data is an empty string, data.split(",") results in ['']. This will lead to an error in the loop. Consider handling empty input at the beginning.

"""Parses comma-separated key-value pairs into a dictionary."""
result = {}
parts = data.split(",")
for part in parts:
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

If data is an empty string, data.split(",") results in ['']. This will lead to an error in the loop at the next line. Consider handling empty input at the beginning.

parts = data.split(",")
for part in parts:
key, value = part.split(":")
result[key.strip()] = value.strip()
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This will fail if part doesn't have a colon or has more than one. Using part.split(":", 1) would be safer if you only want the first colon to be the separator. You should also check if the split actually produced two parts.

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.

1 participant