Skip to content
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
4 changes: 4 additions & 0 deletions changelog_entry.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
- bump: patch
changes:
changed:
- Fixed tracer parsing function. It was not properly handling variables whose names are substrings of other variables.
4 changes: 3 additions & 1 deletion policyengine_api/services/tracer_analysis_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,10 @@ def _parse_tracer_output(self, tracer_output, target_variable):
# Create a regex pattern to match the exact variable name
# This will match the variable name followed by optional whitespace,
# then optional angle brackets with any content, then optional whitespace
pattern = rf"^(\s*)({re.escape(target_variable)})\s*(?:<[^>]*>)?\s*"

pattern = (
rf"^(\s*)({re.escape(target_variable)})(?!\w)\s*(?:<[^>]*>)?\s*"
)
for line in tracer_output:
# Count leading spaces to determine indentation level
indent = len(line) - len(line.strip())
Expand Down
4 changes: 4 additions & 0 deletions tests/fixtures/services/tracer_analysis_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,8 @@

spliced_valid_tracer_output_leaf_variable = valid_tracer_output[8:]

spliced_valid_tracer_output_for_variable_that_is_substring_of_another = (
valid_tracer_output[7:8]
)

empty_tracer = []
18 changes: 18 additions & 0 deletions tests/unit/services/test_tracer_analysis_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,21 @@ def test_tracer_output_for_leaf_variable():
# Then: It should return only leaf variable since it has no children
expected_output = spliced_valid_tracer_output_leaf_variable
assert result == expected_output


def test_tracer_output_for_variable_that_is_substring_of_another():
Copy link
Collaborator

Choose a reason for hiding this comment

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

accolade: I really appreciate how quickly you were able to get oriented on our code base. Great to have a test for this issue!

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Thank you so much @anth-volk !

# Given: A tracer output with variables where one is a substring of another
# and the target variable that's a substring of another variable
target_variable = "snap_net_income"

# When: Extracting the segment for this variable
result = test_service._parse_tracer_output(
valid_tracer_output, target_variable
)

# Then: It should return only the exact match for "snap_net_income", not "snap_net_income_fpg_ratio"

expected_output = (
spliced_valid_tracer_output_for_variable_that_is_substring_of_another
)
assert result == expected_output
Loading