@@ -678,20 +678,14 @@ def get_next_to_consume(self, node):
678
678
# If this node is in an ExceptHandler,
679
679
# filter out assignments in the try portion, assuming they may fail
680
680
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
689
684
)
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 ]
695
689
696
690
return found_nodes
697
691
@@ -740,6 +734,26 @@ def _uncertain_nodes_in_except_blocks(found_nodes, node, node_statement):
740
734
uncertain_nodes .append (other_node )
741
735
return uncertain_nodes
742
736
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
+
743
757
744
758
# pylint: disable=too-many-public-methods
745
759
class VariablesChecker (BaseChecker ):
0 commit comments