Skip to content

Commit

Permalink
Prevent used-before-assignment for variables defined in assignment …
Browse files Browse the repository at this point in the history
…expressions (#7847)
  • Loading branch information
jacobtylerwalls committed Nov 25, 2022
1 parent a6a5446 commit ffdedfe
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 8 deletions.
8 changes: 4 additions & 4 deletions pylint/checkers/variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -738,14 +738,14 @@ def _exhaustively_define_name_raise_or_return(
if any(node.nodes_of_class(nodes.Break)):
return True

# If there is no else, then there is no collectively exhaustive set of paths
if not node.orelse:
return False

# Is there an assignment in this node itself, e.g. in named expression?
if NamesConsumer._defines_name_raises_or_returns(name, node):
return True

# If there is no else, then there is no collectively exhaustive set of paths
if not node.orelse:
return False

return NamesConsumer._branch_handles_name(
name, node.body
) and NamesConsumer._branch_handles_name(name, node.orelse)
Expand Down
7 changes: 4 additions & 3 deletions tests/functional/u/undefined/undefined_variable_py38.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ def expression_in_ternary_operator_inside_container_wrong_position():
z = z + 1


if (never_defined := False):
pass
print(never_defined) # [used-before-assignment]
if (defined := False):
NEVER_DEFINED = 1
print(defined)
print(NEVER_DEFINED) # [used-before-assignment]
2 changes: 1 addition & 1 deletion tests/functional/u/undefined/undefined_variable_py38.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ used-before-assignment:140:10:140:16:type_annotation_used_improperly_after_compr
used-before-assignment:147:10:147:16:type_annotation_used_improperly_after_comprehension_2:Using variable 'my_int' before assignment:HIGH
used-before-assignment:177:12:177:16:expression_in_ternary_operator_inside_container_wrong_position:Using variable 'val3' before assignment:HIGH
used-before-assignment:181:9:181:10::Using variable 'z' before assignment:HIGH
used-before-assignment:187:6:187:19::Using variable 'never_defined' before assignment:CONTROL_FLOW
used-before-assignment:188:6:188:19::Using variable 'NEVER_DEFINED' before assignment:CONTROL_FLOW

0 comments on commit ffdedfe

Please sign in to comment.