diff options
author | cpopa <devnull@localhost> | 2013-10-03 18:29:21 +0300 |
---|---|---|
committer | cpopa <devnull@localhost> | 2013-10-03 18:29:21 +0300 |
commit | 5950351274dc611ab4849f8c4135cd00fb2322e9 (patch) | |
tree | 383ce5428e8b75fae27a7b1c4ba3bd556d24e3e7 | |
parent | 7d46a3cd8b4563fd0d93b6ea14fe9a3ec90c647d (diff) | |
download | pylint-non_unpackable.tar.gz |
Refactoring to allow the catching of InferenceErrors.non_unpackable
-rw-r--r-- | checkers/variables.py | 83 |
1 files changed, 44 insertions, 39 deletions
diff --git a/checkers/variables.py b/checkers/variables.py index bc56ec6..0d35884 100644 --- a/checkers/variables.py +++ b/checkers/variables.py @@ -557,52 +557,57 @@ builtins. Remember that you should avoid to define new builtins when possible.' if not isinstance(node.targets[0], (astroid.Tuple, astroid.List)): return - try: - infered_nodes = node.value.infered() - except astroid.InferenceError: - return - targets = node.targets[0].itered() if any(not isinstance(target_node, astroid.AssName) for target_node in targets): return - for infered in infered_nodes: - if isinstance(infered, (astroid.Tuple, astroid.List)): - values = infered.itered() - if len(targets) != len(values): - if node.root().name == infered.root().name: - location = infered.lineno or 'unknown' - else: - location = '%s (%s)' % (infered.lineno or 'unknown', - infered.root().name) - - self.add_message('unbalanced-tuple-unpacking', - node=node, - args=(location, - len(targets), - len(values))) - else: - if infered is astroid.YES: + try: + for infered in node.value.infer(): + self._check_unpacking(infered, node, targets) + except astroid.InferenceError: + return + + def _check_unpacking(self, infered, node, targets): + """ Check for unbalanced tuple unpacking + and unpacking non sequences. + """ + if isinstance(infered, (astroid.Tuple, astroid.List)): + values = infered.itered() + if len(targets) != len(values): + if node.root().name == infered.root().name: + location = infered.lineno or 'unknown' + else: + location = '%s (%s)' % (infered.lineno or 'unknown', + infered.root().name) + + self.add_message('unbalanced-tuple-unpacking', + node=node, + args=(location, + len(targets), + len(values))) + else: + if infered is astroid.YES: + return + + for meth in ('__iter__', '__getitem__'): + try: + infered.getattr(meth) + except astroid.NotFoundError: continue + else: + break + else: + if node.root().name == infered.root().name: + location = infered.lineno or 'unknown' + else: + location = '%s (%s)' % (infered.lineno or 'unknown', + infered.root().name) + + self.add_message('unpacking-non-sequence', + node=node, + args=(location, )) - for meth in ('__iter__', '__getitem__'): - try: - infered.getattr(meth) - except astroid.NotFoundError: - continue - else: - break - else: - if node.root().name == infered.root().name: - location = infered.lineno or 'unknown' - else: - location = '%s (%s)' % (infered.lineno or 'unknown', - infered.root().name) - - self.add_message('unpacking-non-sequence', - node=node, - args=(location, )) def _check_module_attrs(self, node, module, module_names): """check that module_names (list of string) are accessible through the |