Skip to content

Commit

Permalink
Fixed issue where empty preconditions were preventing CFGs from being…
Browse files Browse the repository at this point in the history
… generated (#1104)
  • Loading branch information
david-yz-liu authored Oct 30, 2024
1 parent 4ee37a9 commit a307a7e
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ and adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
- Fixed issue within `Snapshot.py` where the `memory_viz_version` parameter was not respected
- Fixed issue where parallel assignment statements and assignment to multiple targets were not checked by `redundant_assignment_checker`
- Fixed issue where annotated assignment statements were not checked by `redundant_assignment_checker`
- Fixed issue where empty preconditions were preventing CFGs from being generated

### 🔧 Internal changes

Expand Down
2 changes: 1 addition & 1 deletion python_ta/cfg/visitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -491,5 +491,5 @@ def _is_python_precondition(precondition: str) -> bool:
try:
_ = extract_node(precondition)
return True
except AstroidSyntaxError:
except (AstroidSyntaxError, ValueError): # ValueError raised when precondition is blank
return False
31 changes: 31 additions & 0 deletions tests/test_cfg/test_functions_preconditions.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,3 +169,34 @@ def multiply(a: int, b: int) -> int:
expected_conditions = ["y != 0 and x > 0 and x + y < 100", "b > 0 and a > 0 and a + b != 100"]
assert found_conditions == expected_conditions
assert len(found_conditions) == expected_num_conditions


def test_condition_blank_precondition() -> None:
"""Test that the condition node in a function CFG is created properly if there is a blank precondition."""
src = """def divide(x: int, y: int) -> int:
\"\"\"Return x // y.
Precondition:
\"\"\"
return x // y
"""
expected_num_conditions = 0
found_conditions = _extract_edge_conditions(build_cfg(src))
assert len(found_conditions) == expected_num_conditions


def test_condition_blank_precondition_mixed() -> None:
"""Test that the condition node in a function CFG is created properly if there is a blank precondition
mixed in with non-blank preconditions.
"""
src = """def divide(x: int, y: int) -> int:
\"\"\"Return x // y.
Preconditions:
-
- y != 0
\"\"\"
return x // y
"""
expected_num_conditions = 1
found_conditions = _extract_edge_conditions(build_cfg(src))
assert all(condition == "y != 0" for condition in found_conditions)
assert len(found_conditions) == expected_num_conditions

0 comments on commit a307a7e

Please sign in to comment.