Skip to content

Prevent used-before-assignment for names only defined under except if the else returns #6791

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

Merged
merged 2 commits into from
Jun 3, 2022
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
5 changes: 5 additions & 0 deletions doc/whatsnew/2/2.14/full.rst
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ Release date: 2022-06-01

Refs #6462

* Fixed a false positive regression in 2.13 for ``used-before-assignment`` where it is safe to rely
on a name defined only in an ``except`` block because the ``else`` block returned.

Closes #6790

* Removed the ``assign-to-new-keyword`` message as there are no new keywords in the supported Python
versions any longer.

Expand Down
15 changes: 13 additions & 2 deletions pylint/checkers/variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -680,12 +680,16 @@ def _uncertain_nodes_in_except_blocks(
if closest_except_handler.parent_of(node):
continue
closest_try_except: nodes.TryExcept = closest_except_handler.parent
# If the try or else blocks return, assume the except blocks execute.
try_block_returns = any(
isinstance(try_statement, nodes.Return)
for try_statement in closest_try_except.body
)
# If the try block returns, assume the except blocks execute.
if try_block_returns:
else_block_returns = any(
isinstance(else_statement, nodes.Return)
for else_statement in closest_try_except.orelse
)
if try_block_returns or else_block_returns:
# Exception: if this node is in the final block of the other_node_statement,
# it will execute before returning. Assume the except statements are uncertain.
if (
Expand All @@ -694,6 +698,13 @@ def _uncertain_nodes_in_except_blocks(
and closest_try_except.parent.parent_of(node_statement)
):
uncertain_nodes.append(other_node)
# Or the node_statement is in the else block of the relevant TryExcept
elif (
isinstance(node_statement.parent, nodes.TryExcept)
and node_statement in node_statement.parent.orelse
and closest_try_except.parent.parent_of(node_statement)
):
uncertain_nodes.append(other_node)
# Assume the except blocks execute, so long as each handler
# defines the name, raises, or returns.
elif all(
Expand Down
61 changes: 61 additions & 0 deletions tests/functional/u/used/used_before_assignment_else_return.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
"""If the else block returns, it is generally safe to rely on assignments in the except."""


def valid():
"""https://github.com/PyCQA/pylint/issues/6790"""
try:
pass
except ValueError:
error = True
else:
return

print(error)


def invalid():
"""The finally will execute before the else returns."""
try:
pass
except ValueError:
error = None
else:
return
finally:
print(error) # [used-before-assignment]


def invalid_2():
"""The else does not return in every branch."""
try:
pass
except ValueError:
error = None
else:
if range(0):
return
finally:
print(error) # [used-before-assignment]


def invalid_3():
"""Not every except defines the name."""
try:
pass
except ValueError:
error = None
except KeyError:
pass
finally:
print(error) # [used-before-assignment]


def invalid_4():
"""Should not rely on the name in the else even if it returns."""
try:
pass
except ValueError:
error = True
else:
print(error) # [used-before-assignment]
return
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
used-before-assignment:25:14:25:19:invalid:Using variable 'error' before assignment:CONTROL_FLOW
used-before-assignment:38:14:38:19:invalid_2:Using variable 'error' before assignment:CONTROL_FLOW
used-before-assignment:50:14:50:19:invalid_3:Using variable 'error' before assignment:CONTROL_FLOW
used-before-assignment:60:14:60:19:invalid_4:Using variable 'error' before assignment:CONTROL_FLOW