diff options
| author | Claudiu Popa <pcmanticore@gmail.com> | 2019-03-03 15:17:28 +0100 |
|---|---|---|
| committer | Claudiu Popa <pcmanticore@gmail.com> | 2019-03-03 15:17:28 +0100 |
| commit | 9d198cd71af9f25c46a9fade26daa45d5aee4cfb (patch) | |
| tree | 040bb215bda127d950b801ed10404564e6455af6 /astroid/node_classes.py | |
| parent | 1c5909f0e1dabf8a32bef4d4b9b45f2322d5185b (diff) | |
| download | astroid-git-9d198cd71af9f25c46a9fade26daa45d5aee4cfb.tar.gz | |
The last except handler wins when inferring variables bound in an except handler.
Close PyCQA/pylint#2777
Diffstat (limited to 'astroid/node_classes.py')
| -rw-r--r-- | astroid/node_classes.py | 16 |
1 files changed, 14 insertions, 2 deletions
diff --git a/astroid/node_classes.py b/astroid/node_classes.py index 0ab9be08..8782fb2b 100644 --- a/astroid/node_classes.py +++ b/astroid/node_classes.py @@ -1096,6 +1096,16 @@ class LookupMixIn: context = contextmod.InferenceContext() return bases._infer_stmts(stmts, context, frame) + def _get_filtered_node_statements(self, nodes): + statements = {node: node.statement() for node in nodes} + # Next we check if we have ExceptHandlers that are parent + # of the underlying variable, in which case the last one survives + if all(isinstance(stmt, ExceptHandler) for stmt in statements.values()): + statements = { + node: stmt for node, stmt in statements.items() if stmt.parent_of(self) + } + return statements + def _filter_stmts(self, stmts, frame, offset): """Filter the given list of statements to remove ignorable statements. @@ -1149,10 +1159,12 @@ class LookupMixIn: else: # disabling lineno filtering mylineno = 0 + _stmts = [] _stmt_parents = [] - for node in stmts: - stmt = node.statement() + statements = self._get_filtered_node_statements(stmts) + + for node, stmt in statements.items(): # line filtering is on and we have reached our location, break if stmt.fromlineno > mylineno > 0: break |
