@@ -677,7 +677,7 @@ def get_next_to_consume(self, node):
677
677
678
678
# If this node is in an ExceptHandler,
679
679
# filter out assignments in the try portion, assuming they may fail
680
- if found_nodes and isinstance ( node_statement . parent , nodes . ExceptHandler ) :
680
+ if found_nodes :
681
681
uncertain_nodes = (
682
682
self ._uncertain_nodes_in_try_blocks_when_evaluating_except_blocks (
683
683
found_nodes , node_statement
@@ -738,17 +738,28 @@ def _uncertain_nodes_in_except_blocks(found_nodes, node, node_statement):
738
738
def _uncertain_nodes_in_try_blocks_when_evaluating_except_blocks (
739
739
found_nodes , node_statement
740
740
):
741
+ closest_except_handler = utils .get_node_first_ancestor_of_type (
742
+ node_statement , nodes .ExceptHandler
743
+ )
744
+ if closest_except_handler is None :
745
+ return []
746
+ closest_try_ancestor = utils .get_node_first_ancestor_of_type (
747
+ node_statement , nodes .TryExcept
748
+ )
741
749
uncertain_nodes = []
742
750
for other_node in found_nodes :
743
751
other_node_statement = other_node .statement (future = True )
752
+ if other_node_statement is closest_except_handler :
753
+ continue
744
754
other_node_try_ancestor = utils .get_node_first_ancestor_of_type (
745
755
other_node_statement , nodes .TryExcept
746
756
)
747
- if other_node_try_ancestor is None :
748
- continue
749
- if other_node_statement not in other_node_try_ancestor .body :
757
+ if other_node_try_ancestor is not closest_try_ancestor :
750
758
continue
751
- if node_statement .parent not in other_node_try_ancestor .handlers :
759
+ other_node_except_handler = utils .get_node_first_ancestor_of_type (
760
+ other_node_statement , nodes .ExceptHandler
761
+ )
762
+ if other_node_except_handler is closest_except_handler :
752
763
continue
753
764
# Passed all tests for uncertain execution
754
765
uncertain_nodes .append (other_node )
0 commit comments