Skip to content

Commit

Permalink
fix: allow f-string to trigger TRY003
Browse files Browse the repository at this point in the history
  • Loading branch information
LSanselme authored and guilatrova committed Oct 30, 2024
1 parent 1cfb566 commit 5d6d050
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/tests/analyzers_call_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down
2 changes: 2 additions & 0 deletions src/tests/samples/violations/call_raise_long_str.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down
4 changes: 4 additions & 0 deletions src/tryceratops/analyzers/call.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 5d6d050

Please sign in to comment.