From 4c9abfb2aaf5e0649836a2a63c28279bc10eed72 Mon Sep 17 00:00:00 2001 From: "claude[bot]" <41898282+claude[bot]@users.noreply.github.com> Date: Wed, 18 Mar 2026 18:23:49 +0000 Subject: [PATCH] refactor: remove redundant try/finally; rely on conftest autouse fixture for language cleanup The conftest.py autouse fixture already resets _current_language before/after each test, making per-test try/finally cleanup unnecessary. Co-authored-by: Kevin Turcios --- tests/test_parse_line_profile_test_output.py | 82 ++++++++++---------- 1 file changed, 39 insertions(+), 43 deletions(-) diff --git a/tests/test_parse_line_profile_test_output.py b/tests/test_parse_line_profile_test_output.py index 439608c9f..cd1e4935a 100644 --- a/tests/test_parse_line_profile_test_output.py +++ b/tests/test_parse_line_profile_test_output.py @@ -4,58 +4,54 @@ from tempfile import TemporaryDirectory from codeflash.languages import set_current_language from codeflash.languages.base import Language -from codeflash.languages.current import reset_current_language from codeflash.languages.java.line_profiler import JavaLineProfiler def test_parse_line_profile_results_non_python_java_json(): set_current_language(Language.JAVA) - try: - with TemporaryDirectory() as tmpdir: - tmp_path = Path(tmpdir) - source_file = tmp_path / "Util.java" - source_file.write_text( - """public class Util { + with TemporaryDirectory() as tmpdir: + tmp_path = Path(tmpdir) + source_file = tmp_path / "Util.java" + source_file.write_text( + """public class Util { public static int f() { int x = 1; return x; } } """, - encoding="utf-8", - ) - profile_file = tmp_path / "line_profiler_output.json" - profile_data = { - f"{source_file.as_posix()}:3": { - "hits": 6, - "time": 1000, - "file": source_file.as_posix(), - "line": 3, - "content": "int x = 1;", - }, - f"{source_file.as_posix()}:4": { - "hits": 6, - "time": 2000, - "file": source_file.as_posix(), - "line": 4, - "content": "return x;", - }, - } - profile_file.write_text(json.dumps(profile_data), encoding="utf-8") - - results = JavaLineProfiler.parse_results(profile_file) - - assert results["unit"] == 1e-9 - assert results["str_out"] == ( - "# Timer unit: 1e-09 s\n" - "## Function: Util.java\n" - "## Total time: 3e-06 s\n" - "| Hits | Time | Per Hit | % Time | Line Contents |\n" - "|-------:|-------:|----------:|---------:|:----------------|\n" - "| 6 | 1000 | 166.7 | 33.3 | int x = 1; |\n" - "| 6 | 2000 | 333.3 | 66.7 | return x; |\n" + encoding="utf-8", ) - assert (source_file.as_posix(), 3, "Util.java") in results["timings"] - assert results["timings"][(source_file.as_posix(), 3, "Util.java")] == [(3, 6, 1000), (4, 6, 2000)] - finally: - reset_current_language() + profile_file = tmp_path / "line_profiler_output.json" + profile_data = { + f"{source_file.as_posix()}:3": { + "hits": 6, + "time": 1000, + "file": source_file.as_posix(), + "line": 3, + "content": "int x = 1;", + }, + f"{source_file.as_posix()}:4": { + "hits": 6, + "time": 2000, + "file": source_file.as_posix(), + "line": 4, + "content": "return x;", + }, + } + profile_file.write_text(json.dumps(profile_data), encoding="utf-8") + + results = JavaLineProfiler.parse_results(profile_file) + + assert results["unit"] == 1e-9 + assert results["str_out"] == ( + "# Timer unit: 1e-09 s\n" + "## Function: Util.java\n" + "## Total time: 3e-06 s\n" + "| Hits | Time | Per Hit | % Time | Line Contents |\n" + "|-------:|-------:|----------:|---------:|:----------------|\n" + "| 6 | 1000 | 166.7 | 33.3 | int x = 1; |\n" + "| 6 | 2000 | 333.3 | 66.7 | return x; |\n" + ) + assert (source_file.as_posix(), 3, "Util.java") in results["timings"] + assert results["timings"][(source_file.as_posix(), 3, "Util.java")] == [(3, 6, 1000), (4, 6, 2000)]