Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Backport maintenance/3.2.x] [possibly-used-before-assignment] Fix FP for sys.exit() #9635

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions doc/whatsnew/fragments/9627.false_positive
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Exclude if/else branches containing terminating functions (e.g. `sys.exit()`)
from `possibly-used-before-assignment` checks.

Closes #9627
6 changes: 6 additions & 0 deletions pylint/checkers/variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -940,6 +940,12 @@ def _uncertain_nodes_in_except_blocks(
def _defines_name_raises_or_returns(name: str, node: nodes.NodeNG) -> bool:
if isinstance(node, (nodes.Raise, nodes.Assert, nodes.Return, nodes.Continue)):
return True
if (
isinstance(node, nodes.Expr)
and isinstance(node.value, nodes.Call)
and utils.is_terminating_func(node.value)
):
return True
if (
isinstance(node, nodes.AnnAssign)
and node.value
Expand Down
7 changes: 7 additions & 0 deletions tests/functional/u/used/used_before_assignment.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Miscellaneous used-before-assignment cases"""
# pylint: disable=consider-using-f-string, missing-function-docstring
import datetime
import sys

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

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

if input("This tests terminating functions: "):
sys.exit()
else:
VAR13 = 1
print(VAR13)

def turn_on2(**kwargs):
"""https://github.com/pylint-dev/pylint/issues/7873"""
if "brightness" in kwargs:
Expand Down
30 changes: 15 additions & 15 deletions tests/functional/u/used/used_before_assignment.txt
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
used-before-assignment:5:19:5:22::Using variable 'MSG' before assignment:HIGH
used-before-assignment:7:20:7:24::Using variable 'MSG2' before assignment:HIGH
used-before-assignment:10:4:10:9:outer:Using variable 'inner' before assignment:HIGH
used-before-assignment:19:20:19:40:ClassWithProperty:Using variable 'redefine_time_import' before assignment:HIGH
used-before-assignment:23:0:23:9::Using variable 'calculate' before assignment:HIGH
used-before-assignment:31:10:31:14:redefine_time_import:Using variable 'time' before assignment:HIGH
used-before-assignment:45:3:45:7::Using variable 'VAR2' before assignment:INFERENCE
possibly-used-before-assignment:63:3:63:7::Possibly using variable 'VAR4' before assignment:INFERENCE
possibly-used-before-assignment:73:3:73:7::Possibly using variable 'VAR5' before assignment:INFERENCE
used-before-assignment:78:3:78:7::Using variable 'VAR6' before assignment:INFERENCE
used-before-assignment:113:6:113:11::Using variable 'VAR10' before assignment:INFERENCE
possibly-used-before-assignment:119:6:119:11::Possibly using variable 'VAR12' before assignment:CONTROL_FLOW
used-before-assignment:144:10:144:14::Using variable 'SALE' before assignment:INFERENCE
used-before-assignment:176:10:176:18::Using variable 'ALL_DONE' before assignment:INFERENCE
used-before-assignment:187:6:187:24::Using variable 'NOT_ALWAYS_DEFINED' before assignment:INFERENCE
used-before-assignment:6:19:6:22::Using variable 'MSG' before assignment:HIGH
used-before-assignment:8:20:8:24::Using variable 'MSG2' before assignment:HIGH
used-before-assignment:11:4:11:9:outer:Using variable 'inner' before assignment:HIGH
used-before-assignment:20:20:20:40:ClassWithProperty:Using variable 'redefine_time_import' before assignment:HIGH
used-before-assignment:24:0:24:9::Using variable 'calculate' before assignment:HIGH
used-before-assignment:32:10:32:14:redefine_time_import:Using variable 'time' before assignment:HIGH
used-before-assignment:46:3:46:7::Using variable 'VAR2' before assignment:INFERENCE
possibly-used-before-assignment:64:3:64:7::Possibly using variable 'VAR4' before assignment:INFERENCE
possibly-used-before-assignment:74:3:74:7::Possibly using variable 'VAR5' before assignment:INFERENCE
used-before-assignment:79:3:79:7::Using variable 'VAR6' before assignment:INFERENCE
used-before-assignment:114:6:114:11::Using variable 'VAR10' before assignment:INFERENCE
possibly-used-before-assignment:120:6:120:11::Possibly using variable 'VAR12' before assignment:CONTROL_FLOW
used-before-assignment:151:10:151:14::Using variable 'SALE' before assignment:INFERENCE
used-before-assignment:183:10:183:18::Using variable 'ALL_DONE' before assignment:INFERENCE
used-before-assignment:194:6:194:24::Using variable 'NOT_ALWAYS_DEFINED' before assignment:INFERENCE
Loading