Skip to content

Commit

Permalink
Added checking for raises in 'for' branches
Browse files Browse the repository at this point in the history
Also added relevant tests
  • Loading branch information
JessicaBell00 committed Sep 2, 2024
1 parent 3d03db1 commit 2ca6b85
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2741,19 +2741,19 @@ def check_for_raise(self, node):
if isinstance(i, astroid.Raise):
self.check_for_logging(node)
# Check for any nested 'If' branches
if isinstance(i, astroid.If):
if isinstance(i, astroid.If) or isinstance(i, astroid.For):
self.check_for_raise(i.body)
# Check any 'elif' and 'else' branches
elif_statements = i.orelse
while len(elif_statements) == 1:
if isinstance(elif_statements[0], astroid.If):
for x in elif_statements:
# Check any 'elif', 'else' or 'for' branches
orelse_statements = i.orelse
while len(orelse_statements) == 1:
if isinstance(orelse_statements[0], astroid.If):
for x in orelse_statements:
self.check_for_raise(x.body)
elif_statements = x.orelse
orelse_statements = x.orelse
else:
break
# Check 'else' body for raise
self.check_for_raise(elif_statements)
self.check_for_raise(orelse_statements)

def check_for_logging(self, node):
""" Helper function - checks 'Expr' nodes to see if logging has occurred at 'warning' or 'error'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5108,3 +5108,55 @@ def test_extra_nested_branches_exception_logged(self):
)
):
self.checker.visit_try(try_node)

def test_for_and_nested_for_branches_exception_logged(self):
try_node, expression_node_a, expression_node_b = astroid.extract_node(
'''
try: #@
add = 1 + 2
except Exception as e:
y = 3
for x in y:
logging.error(f"F: {e}") #@
raise SystemError("Uh oh!") from e
if e.code == "Retryable":
for z in y:
logging.error(f"F: {e}") #@
raise SystemError("Uh oh!") from e
''')
with self.assertAddsMessages(
pylint.testutils.MessageTest(
msg_id="do-not-log-raised-errors",
line=7,
node=expression_node_a.parent,
col_offset=8,
end_line=7,
end_col_offset=32,
),
pylint.testutils.MessageTest(
msg_id="do-not-log-raised-errors",
line=11,
node=expression_node_b.parent,
col_offset=12,
end_line=11,
end_col_offset=36,
)
):
self.checker.visit_try(try_node)

def test_for_and_nested_for_branches_exception_not_logged(self):
try_node, expression_node_a, expression_node_b = astroid.extract_node(
'''
try: #@
add = 1 + 2
except Exception as e:
y = 3
for x in y:
logging.error(f"F: {e}") #@
if e.code == "Retryable":
for z in y:
logging.error(f"F: {e}") #@
''')
with self.assertNoMessages():
self.checker.visit_try(try_node)

0 comments on commit 2ca6b85

Please sign in to comment.