Skip to content

Commit 2d949d9

Browse files
Factor out _uncertain_nodes_in_try_blocks_when_evaluating_except_blocks()
1 parent c0fbbb7 commit 2d949d9

File tree

1 file changed

+27
-13
lines changed

1 file changed

+27
-13
lines changed

pylint/checkers/variables.py

+27-13
Original file line numberDiff line numberDiff line change
@@ -678,20 +678,14 @@ def get_next_to_consume(self, node):
678678
# If this node is in an ExceptHandler,
679679
# filter out assignments in the try portion, assuming they may fail
680680
if found_nodes and isinstance(node_statement.parent, nodes.ExceptHandler):
681-
filtered_nodes = [
682-
n
683-
for n in found_nodes
684-
if not (
685-
isinstance(n.statement(future=True).parent, nodes.TryExcept)
686-
and n.statement(future=True) in n.statement(future=True).parent.body
687-
and node_statement.parent
688-
in n.statement(future=True).parent.handlers
681+
uncertain_nodes = (
682+
self._uncertain_nodes_in_try_blocks_when_evaluating_except_blocks(
683+
found_nodes, node_statement
689684
)
690-
]
691-
filtered_nodes_set = set(filtered_nodes)
692-
difference = [n for n in found_nodes if n not in filtered_nodes_set]
693-
self.consumed_uncertain[node.name] += difference
694-
found_nodes = filtered_nodes
685+
)
686+
self.consumed_uncertain[node.name] += uncertain_nodes
687+
uncertain_nodes_set = set(uncertain_nodes)
688+
found_nodes = [n for n in found_nodes if n not in uncertain_nodes_set]
695689

696690
return found_nodes
697691

@@ -740,6 +734,26 @@ def _uncertain_nodes_in_except_blocks(found_nodes, node, node_statement):
740734
uncertain_nodes.append(other_node)
741735
return uncertain_nodes
742736

737+
@staticmethod
738+
def _uncertain_nodes_in_try_blocks_when_evaluating_except_blocks(
739+
found_nodes, node_statement
740+
):
741+
uncertain_nodes = []
742+
for other_node in found_nodes:
743+
other_node_statement = other_node.statement(future=True)
744+
other_node_try_ancestor = utils.get_node_first_ancestor_of_type(
745+
other_node_statement, nodes.TryExcept
746+
)
747+
if other_node_try_ancestor is None:
748+
continue
749+
if other_node_statement not in other_node_try_ancestor.body:
750+
continue
751+
if node_statement.parent not in other_node_try_ancestor.handlers:
752+
continue
753+
# Passed all tests for uncertain execution
754+
uncertain_nodes.append(other_node)
755+
return uncertain_nodes
756+
743757

744758
# pylint: disable=too-many-public-methods
745759
class VariablesChecker(BaseChecker):

0 commit comments

Comments
 (0)