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

Fix #85, #2615: Emit used-before-assignment in final or except blocks where try statements could have failed #5384

Merged
Show file tree
Hide file tree
Changes from 4 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 ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ Release date: TBA

Closes #4761

* ``used-before-assignment`` now considers that assignments in a try block
may not have occurred when the except or finally blocks are executed.

Closes #85, #2615

* ``used-before-assignment`` now checks names in try blocks.

* Some files in ``pylint.testutils`` were deprecated. In the future imports should be done from the
Expand Down
5 changes: 5 additions & 0 deletions doc/whatsnew/2.13.rst
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ Other Changes

Closes #4761

* ``used-before-assignment`` now considers that assignments in a try block
may not have occurred when the except or finally blocks are executed.

Closes #85, #2615

* ``used-before-assignment`` now checks names in try blocks.

* Require Python ``3.6.2`` to run pylint.
Expand Down
52 changes: 45 additions & 7 deletions pylint/checkers/variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,8 @@ def _has_locals_call_after_node(stmt, scope):
"E0601": (
"Using variable %r before assignment",
"used-before-assignment",
"Used when a local variable is accessed before its assignment.",
"Used when a local variable is accessed before its assignment or within "
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's merge this in the current state and use #5506 to perfect this description 😄

"except or finally blocks without being defined before the try block.",
jacobtylerwalls marked this conversation as resolved.
Show resolved Hide resolved
),
"E0602": (
"Undefined variable %r",
Expand Down Expand Up @@ -580,12 +581,10 @@ def consumed(self):
def consumed_uncertain(self) -> DefaultDict[str, List[nodes.NodeNG]]:
"""
Retrieves nodes filtered out by get_next_to_consume() that may not
have executed, such as statements in except blocks. Checkers that
want to treat the statements as executed (e.g. for unused-variable)
may need to add them back.

TODO: A pending PR will extend this to nodes in try blocks when
evaluating their corresponding except and finally blocks.
have executed, such as statements in except blocks, or statements
in try blocks (when evaluating their corresponding except and finally
blocks). Checkers that want to treat the statements as executed
(e.g. for unused-variable) may need to add them back.
"""
return self._atomic.consumed_uncertain

Expand Down Expand Up @@ -617,6 +616,7 @@ def get_next_to_consume(self, node):
name = node.name
parent_node = node.parent
found_nodes = self.to_consume.get(name)
node_statement = node.statement(future=True)
if (
found_nodes
and isinstance(parent_node, nodes.Assign)
Expand Down Expand Up @@ -662,6 +662,44 @@ def get_next_to_consume(self, node):
self.consumed_uncertain[node.name] += difference
found_nodes = filtered_nodes

# If this node is in a Finally block of a Try/Finally,
# filter out assignments in the try portion, assuming they may fail
if (
found_nodes
and isinstance(node_statement.parent, nodes.TryFinally)
and node_statement in node_statement.parent.finalbody
):
filtered_nodes = [
n
for n in found_nodes
if not (
n.statement(future=True).parent is node_statement.parent
and n.statement(future=True) in n.statement(future=True).parent.body
)
]
filtered_nodes_set = set(filtered_nodes)
difference = [n for n in found_nodes if n not in filtered_nodes_set]
self.consumed_uncertain[node.name] += difference
found_nodes = filtered_nodes

# If this node is in an ExceptHandler,
# filter out assignments in the try portion, assuming they may fail
if found_nodes and isinstance(node_statement.parent, nodes.ExceptHandler):
filtered_nodes = [
n
for n in found_nodes
if not (
isinstance(n.statement(future=True).parent, nodes.TryExcept)
and n.statement(future=True) in n.statement(future=True).parent.body
and node_statement.parent
in n.statement(future=True).parent.handlers
)
]
filtered_nodes_set = set(filtered_nodes)
difference = [n for n in found_nodes if n not in filtered_nodes_set]
self.consumed_uncertain[node.name] += difference
found_nodes = filtered_nodes

return found_nodes


Expand Down
9 changes: 9 additions & 0 deletions tests/functional/u/use/used_before_assignment_issue2615.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
"""https://github.com/PyCQA/pylint/issues/2615"""
def main():
"""When evaluating except blocks, assume try statements fail."""
try:
res = 1 / 0
res = 42
except ZeroDivisionError:
print(res) # [used-before-assignment]
print(res)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
used-before-assignment:8:14:8:17:main:Using variable 'res' before assignment:UNDEFINED
9 changes: 9 additions & 0 deletions tests/functional/u/use/used_before_assignment_issue85.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
"""https://github.com/PyCQA/pylint/issues/85"""
def main():
"""When evaluating finally blocks, assume try statements fail."""
try:
res = 1 / 0
res = 42
finally:
print(res) # [used-before-assignment]
print(res)
1 change: 1 addition & 0 deletions tests/functional/u/use/used_before_assignment_issue85.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
used-before-assignment:8:14:8:17:main:Using variable 'res' before assignment:UNDEFINED