From 5d6d05061b7fc3d5bf470339287f4123970a522a Mon Sep 17 00:00:00 2001 From: Lilian Sanselme Date: Mon, 19 Aug 2024 17:42:13 +0000 Subject: [PATCH] fix: allow f-string to trigger TRY003 --- src/tests/analyzers_call_test.py | 7 ++++--- src/tests/samples/violations/call_raise_long_str.py | 2 ++ src/tryceratops/analyzers/call.py | 4 ++++ 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/tests/analyzers_call_test.py b/src/tests/analyzers_call_test.py index 9650ad7..59f31af 100644 --- a/src/tests/analyzers_call_test.py +++ b/src/tests/analyzers_call_test.py @@ -49,10 +49,11 @@ def test_raise_long_args(): violations = analyzer.check(tree, "filename") - assert len(violations) == 1 - violation, *_ = violations + assert len(violations) == 2 + first, second = violations - assert_args(17, 8, violation) + assert_args(17, 8, first) + assert_args(23, 8, second) def test_check_continue(): diff --git a/src/tests/samples/violations/call_raise_long_str.py b/src/tests/samples/violations/call_raise_long_str.py index 5ac6059..33f9186 100644 --- a/src/tests/samples/violations/call_raise_long_str.py +++ b/src/tests/samples/violations/call_raise_long_str.py @@ -19,6 +19,8 @@ def func(): raise CustomException("Short") # This is acceptable elif a == 3: raise CustomException("its_code_not_message") # This is acceptable + elif a == 4: + raise CustomException(f"A long message in the f-string number {a}") def ignore(): diff --git a/src/tryceratops/analyzers/call.py b/src/tryceratops/analyzers/call.py index 9500b18..4c9bdc8 100644 --- a/src/tryceratops/analyzers/call.py +++ b/src/tryceratops/analyzers/call.py @@ -42,6 +42,10 @@ def _check_raise_callable(self, node: ast.Raise, exc: ast.Call, func: ast.Name) if is_constant_str and WHITESPACE in first_arg.value: # type: ignore self._mark_violation(node) + is_constant_fstr = isinstance(first_arg, ast.JoinedStr) + if is_constant_fstr: + self._mark_violation(node) + class CallAvoidCheckingToContinueAnalyzer(BaseAnalyzer): EXPERIMENTAL = True