From a307a7e8a326c56900c8f6e35db3439bc6746879 Mon Sep 17 00:00:00 2001 From: David Liu Date: Wed, 30 Oct 2024 13:33:43 +0000 Subject: [PATCH] Fixed issue where empty preconditions were preventing CFGs from being generated (#1104) --- CHANGELOG.md | 1 + python_ta/cfg/visitor.py | 2 +- .../test_cfg/test_functions_preconditions.py | 31 +++++++++++++++++++ 3 files changed, 33 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b05576b93..24555dd0d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/python_ta/cfg/visitor.py b/python_ta/cfg/visitor.py index d9cba3536..2f8bc5dae 100644 --- a/python_ta/cfg/visitor.py +++ b/python_ta/cfg/visitor.py @@ -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 diff --git a/tests/test_cfg/test_functions_preconditions.py b/tests/test_cfg/test_functions_preconditions.py index 5a1382bb1..30c916e37 100644 --- a/tests/test_cfg/test_functions_preconditions.py +++ b/tests/test_cfg/test_functions_preconditions.py @@ -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