Skip to content

Commit f08c33a

Browse files
[Backport maintenance/4.0.x] Properly detect self.fail() as a terminating node in unittest.TestCase (#10771)
Properly detect 'self.fail()' as a terminating node in 'unittest.TestCase' (#10770) (cherry picked from commit c45fef7) Co-authored-by: Zachary Wilkins-Olson <[email protected]>
1 parent e16f942 commit f08c33a

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix possibly-used-before-assignment false positive when using self.fail() in tests.
2+
3+
Closes #10743

pylint/checkers/utils.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,13 @@
238238
SINGLETON_VALUES = {True, False, None}
239239

240240
TERMINATING_FUNCS_QNAMES = frozenset(
241-
{"_sitebuiltins.Quitter", "sys.exit", "posix._exit", "nt._exit"}
241+
{
242+
"_sitebuiltins.Quitter",
243+
"sys.exit",
244+
"posix._exit",
245+
"nt._exit",
246+
"unittest.case.TestCase.fail",
247+
}
242248
)
243249

244250

tests/checkers/unittest_utils.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -622,3 +622,22 @@ def test_is_reassigned_with_node_no_lineno() -> None:
622622
assert utils.is_reassigned_before_current(first_assign_name, "x") is False
623623
finally:
624624
first_assign_name.lineno = original_lineno
625+
626+
627+
def test_is_terminating_func_unittest_fail() -> None:
628+
node = astroid.extract_node(
629+
"""
630+
from unittest import TestCase
631+
import os
632+
633+
class TestX(TestCase):
634+
def test_foo(self):
635+
if 'FOO' in os.environ:
636+
x = 1
637+
else:
638+
self.fail() #@
639+
print(x)
640+
"""
641+
)
642+
result = utils.is_terminating_func(node)
643+
assert result is True

0 commit comments

Comments
 (0)