diff options
author | Jacob Walls <jacobtylerwalls@gmail.com> | 2022-12-12 09:02:07 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-12-12 09:02:07 -0500 |
commit | ca5b4aff168ff65fc9b9896265ecf0e58404427f (patch) | |
tree | 518daadb72c8be19c1951de2b5bb39f97c7cfc23 | |
parent | b94fdb2944e5b94c33dc67eb385abbdf1f2e7b3f (diff) | |
download | pylint-git-ca5b4aff168ff65fc9b9896265ecf0e58404427f.tar.gz |
Fix unreleased false positives with `used-before-assignment` (#7921)
Cases with NamedExpr and Starred relating to
definitions under "always false" conditions added
in https://github.com/PyCQA/pylint/pull/6677
-rw-r--r-- | pylint/checkers/variables.py | 17 | ||||
-rw-r--r-- | tests/functional/u/undefined/undefined_variable_py38.py | 4 | ||||
-rw-r--r-- | tests/functional/u/used/used_before_assignment.py | 10 |
3 files changed, 20 insertions, 11 deletions
diff --git a/pylint/checkers/variables.py b/pylint/checkers/variables.py index 602dd5414..29911c471 100644 --- a/pylint/checkers/variables.py +++ b/pylint/checkers/variables.py @@ -900,21 +900,16 @@ scope_type : {self._atomic.scope_type} if isinstance(node, nodes.Assign): for target in node.targets: for elt in utils.get_all_elements(target): + if isinstance(elt, nodes.Starred): + elt = elt.value if isinstance(elt, nodes.AssignName) and elt.name == name: return True if isinstance(node, nodes.If): - # Check for assignments inside the test - if isinstance(node.test, nodes.NamedExpr) and node.test.target.name == name: + if any( + child_named_expr.target.name == name + for child_named_expr in node.nodes_of_class(nodes.NamedExpr) + ): return True - if isinstance(node.test, nodes.Call): - for arg_or_kwarg in node.test.args + [ - kw.value for kw in node.test.keywords - ]: - if ( - isinstance(arg_or_kwarg, nodes.NamedExpr) - and arg_or_kwarg.target.name == name - ): - return True return False @staticmethod diff --git a/tests/functional/u/undefined/undefined_variable_py38.py b/tests/functional/u/undefined/undefined_variable_py38.py index 8afb5eaf9..2612e535f 100644 --- a/tests/functional/u/undefined/undefined_variable_py38.py +++ b/tests/functional/u/undefined/undefined_variable_py38.py @@ -186,3 +186,7 @@ if (defined := False): NEVER_DEFINED = 1 print(defined) print(NEVER_DEFINED) # [used-before-assignment] + +if (still_defined := False) == 1: + NEVER_DEFINED_EITHER = 1 +print(still_defined) diff --git a/tests/functional/u/used/used_before_assignment.py b/tests/functional/u/used/used_before_assignment.py index f8ed651b5..d36b2fd8d 100644 --- a/tests/functional/u/used/used_before_assignment.py +++ b/tests/functional/u/used/used_before_assignment.py @@ -106,3 +106,13 @@ for num in [0, 1]: if VAR11: VAR12 = False print(VAR12) + +def turn_on2(**kwargs): + """https://github.com/PyCQA/pylint/issues/7873""" + if "brightness" in kwargs: + brightness = kwargs["brightness"] + var, *args = (1, "set_dimmer_state", brightness) + else: + var, *args = (1, "restore_dimmer_state") + + print(var, *args) |