Skip to content

Commit c6a9699

Browse files
[possibly-used-before-assignment] Fix FP for sys.exit() (#9634)
1 parent 67e59ac commit c6a9699

File tree

4 files changed

+32
-15
lines changed

4 files changed

+32
-15
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Exclude if/else branches containing terminating functions (e.g. `sys.exit()`)
2+
from `possibly-used-before-assignment` checks.
3+
4+
Closes #9627

pylint/checkers/variables.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -940,6 +940,12 @@ def _uncertain_nodes_in_except_blocks(
940940
def _defines_name_raises_or_returns(name: str, node: nodes.NodeNG) -> bool:
941941
if isinstance(node, (nodes.Raise, nodes.Assert, nodes.Return, nodes.Continue)):
942942
return True
943+
if (
944+
isinstance(node, nodes.Expr)
945+
and isinstance(node.value, nodes.Call)
946+
and utils.is_terminating_func(node.value)
947+
):
948+
return True
943949
if (
944950
isinstance(node, nodes.AnnAssign)
945951
and node.value

tests/functional/u/used/used_before_assignment.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Miscellaneous used-before-assignment cases"""
22
# pylint: disable=consider-using-f-string, missing-function-docstring
33
import datetime
4+
import sys
45

56
MSG = "hello %s" % MSG # [used-before-assignment]
67

@@ -118,6 +119,12 @@ def redefine_time_import_with_global():
118119
VAR12 = False
119120
print(VAR12) # [possibly-used-before-assignment]
120121

122+
if input("This tests terminating functions: "):
123+
sys.exit()
124+
else:
125+
VAR13 = 1
126+
print(VAR13)
127+
121128
def turn_on2(**kwargs):
122129
"""https://github.com/pylint-dev/pylint/issues/7873"""
123130
if "brightness" in kwargs:
Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
used-before-assignment:5:19:5:22::Using variable 'MSG' before assignment:HIGH
2-
used-before-assignment:7:20:7:24::Using variable 'MSG2' before assignment:HIGH
3-
used-before-assignment:10:4:10:9:outer:Using variable 'inner' before assignment:HIGH
4-
used-before-assignment:19:20:19:40:ClassWithProperty:Using variable 'redefine_time_import' before assignment:HIGH
5-
used-before-assignment:23:0:23:9::Using variable 'calculate' before assignment:HIGH
6-
used-before-assignment:31:10:31:14:redefine_time_import:Using variable 'time' before assignment:HIGH
7-
used-before-assignment:45:3:45:7::Using variable 'VAR2' before assignment:INFERENCE
8-
possibly-used-before-assignment:63:3:63:7::Possibly using variable 'VAR4' before assignment:INFERENCE
9-
possibly-used-before-assignment:73:3:73:7::Possibly using variable 'VAR5' before assignment:INFERENCE
10-
used-before-assignment:78:3:78:7::Using variable 'VAR6' before assignment:INFERENCE
11-
used-before-assignment:113:6:113:11::Using variable 'VAR10' before assignment:INFERENCE
12-
possibly-used-before-assignment:119:6:119:11::Possibly using variable 'VAR12' before assignment:CONTROL_FLOW
13-
used-before-assignment:144:10:144:14::Using variable 'SALE' before assignment:INFERENCE
14-
used-before-assignment:176:10:176:18::Using variable 'ALL_DONE' before assignment:INFERENCE
15-
used-before-assignment:187:6:187:24::Using variable 'NOT_ALWAYS_DEFINED' before assignment:INFERENCE
1+
used-before-assignment:6:19:6:22::Using variable 'MSG' before assignment:HIGH
2+
used-before-assignment:8:20:8:24::Using variable 'MSG2' before assignment:HIGH
3+
used-before-assignment:11:4:11:9:outer:Using variable 'inner' before assignment:HIGH
4+
used-before-assignment:20:20:20:40:ClassWithProperty:Using variable 'redefine_time_import' before assignment:HIGH
5+
used-before-assignment:24:0:24:9::Using variable 'calculate' before assignment:HIGH
6+
used-before-assignment:32:10:32:14:redefine_time_import:Using variable 'time' before assignment:HIGH
7+
used-before-assignment:46:3:46:7::Using variable 'VAR2' before assignment:INFERENCE
8+
possibly-used-before-assignment:64:3:64:7::Possibly using variable 'VAR4' before assignment:INFERENCE
9+
possibly-used-before-assignment:74:3:74:7::Possibly using variable 'VAR5' before assignment:INFERENCE
10+
used-before-assignment:79:3:79:7::Using variable 'VAR6' before assignment:INFERENCE
11+
used-before-assignment:114:6:114:11::Using variable 'VAR10' before assignment:INFERENCE
12+
possibly-used-before-assignment:120:6:120:11::Possibly using variable 'VAR12' before assignment:CONTROL_FLOW
13+
used-before-assignment:151:10:151:14::Using variable 'SALE' before assignment:INFERENCE
14+
used-before-assignment:183:10:183:18::Using variable 'ALL_DONE' before assignment:INFERENCE
15+
used-before-assignment:194:6:194:24::Using variable 'NOT_ALWAYS_DEFINED' before assignment:INFERENCE

0 commit comments

Comments
 (0)