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

Implementing Fallback Mechanisms for YAML Parsing #444

Merged
merged 2 commits into from
Nov 12, 2023
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
15 changes: 15 additions & 0 deletions pr_agent/algo/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,21 @@ def load_yaml(review_text: str) -> dict:

def try_fix_yaml(review_text: str) -> dict:
review_text_lines = review_text.split('\n')

# first fallback - try to convert 'relevant line: ...' to relevant line: |-\n ...'
review_text_lines_copy = review_text_lines.copy()
for i in range(0, len(review_text_lines_copy)):
if 'relevant line:' in review_text_lines_copy[i] and not '|-' in review_text_lines_copy[i]:
review_text_lines_copy[i] = review_text_lines_copy[i].replace('relevant line: ',
'relevant line: |-\n ')
try:
data = yaml.load('\n'.join(review_text_lines_copy), Loader=yaml.SafeLoader)
get_logger().info(f"Successfully parsed AI prediction after adding |-\n to relevant line")
return data
except:
get_logger().debug(f"Failed to parse AI prediction after adding |-\n to relevant line")

# second fallback - try to remove last lines
data = {}
for i in range(1, len(review_text_lines)):
review_text_lines_tmp = '\n'.join(review_text_lines[:-i])
Expand Down
31 changes: 31 additions & 0 deletions tests/unittest/try_fix_yaml.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

# Generated by CodiumAI
from pr_agent.algo.utils import try_fix_yaml


import pytest

class TestTryFixYaml:

# The function successfully parses a valid YAML string.
def test_valid_yaml(self):
review_text = "key: value\n"
expected_output = {"key": "value"}
assert try_fix_yaml(review_text) == expected_output

# The function adds '|-' to 'relevant line:' if it is not already present and successfully parses the YAML string.
def test_add_relevant_line(self):
review_text = "relevant line: value: 3\n"
expected_output = {"relevant line": "value: 3"}
assert try_fix_yaml(review_text) == expected_output

# The function removes the last line(s) of the YAML string and successfully parses the YAML string.
def test_remove_last_line(self):
review_text = "key: value\nextra invalid line\n"
expected_output = {"key": "value"}
assert try_fix_yaml(review_text) == expected_output

# The YAML string is empty.
def test_empty_yaml_fixed(self):
review_text = ""
assert try_fix_yaml(review_text) is None
Loading