From a55a126350880ef378cb7e8d6576e400d7f6205d Mon Sep 17 00:00:00 2001 From: Florent Xicluna Date: Sun, 30 Mar 2014 04:36:09 +0200 Subject: Refactoring --- pyflakes/checker.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/pyflakes/checker.py b/pyflakes/checker.py index 993a4fc..f58bc70 100644 --- a/pyflakes/checker.py +++ b/pyflakes/checker.py @@ -355,7 +355,7 @@ class Checker(object): messg = messages.UnusedImport self.report(messg, value.source, value.name) for node in value.redefined: - if self.hasParent(node, ast.For): + if isinstance(self.getParent(node), ast.For): messg = messages.ImportShadowedByLoopVar elif used: continue @@ -370,14 +370,12 @@ class Checker(object): self.messages.append(messageClass(self.filename, *args, **kwargs)) def getParent(self, node): + # Lookup the first parent which is not Tuple, List or Starred while True: node = node.parent if not hasattr(node, 'elts') and not hasattr(node, 'ctx'): return node - def hasParent(self, node, kind): - return isinstance(self.getParent(node), kind) - def getCommonAncestor(self, lnode, rnode, stop=None): if not stop: stop = self.root @@ -436,13 +434,15 @@ class Checker(object): if existing and not self.differentForks(node, existing.source): - if isinstance(existing, Importation) and self.hasParent(value.source, ast.For): + parent_stmt = self.getParent(value.source) + if isinstance(existing, Importation) and isinstance(parent_stmt, ast.For): self.report(messages.ImportShadowedByLoopVar, node, value.name, existing.source) elif scope is self.scope: - if (self.hasParent(value.source, ast.comprehension) and - not self.hasParent(existing.source, (ast.For, ast.comprehension))): + if (isinstance(parent_stmt, ast.comprehension) and + not isinstance(self.getParent(existing.source), + (ast.For, ast.comprehension))): self.report(messages.RedefinedInListComp, node, value.name, existing.source) elif not existing.used and value.redefines(existing): -- cgit v1.2.1