diff options
author | cpopa <devnull@localhost> | 2013-08-28 14:02:16 +0300 |
---|---|---|
committer | cpopa <devnull@localhost> | 2013-08-28 14:02:16 +0300 |
commit | 42a88a037493b65fa5760d771701cdca20daa382 (patch) | |
tree | 1560c91e68b7eca9b5073bb31d6861ed4d487eb5 /checkers/variables.py | |
parent | 44db4d0faf4c3a5393c7b9ce65fca15ec886cb73 (diff) | |
download | pylint-42a88a037493b65fa5760d771701cdca20daa382.tar.gz |
Make unpacking detection smarter by handling unpacking function calls.
Diffstat (limited to 'checkers/variables.py')
-rw-r--r-- | checkers/variables.py | 31 |
1 files changed, 20 insertions, 11 deletions
diff --git a/checkers/variables.py b/checkers/variables.py index 460192d..67bf8a1 100644 --- a/checkers/variables.py +++ b/checkers/variables.py @@ -18,6 +18,7 @@ import sys from copy import copy +from itertools import chain import astroid from astroid import are_exclusive, builtin_lookup, AstroidBuildingException @@ -554,9 +555,10 @@ 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 = node.value.infer().next() + inference = node.value.infer() + infered = inference.next() except astroid.InferenceError: return if isinstance(infered, (astroid.Tuple, astroid.List)): @@ -570,17 +572,24 @@ builtins. Remember that you should avoid to define new builtins when possible.' node=node, args=(len(targets), len(values))) else: - # Iterator or a Sequence, according to collections.abc - for meth in ('__iter__', '__getitem__'): - try: - infered.getattr(meth) - except astroid.NotFoundError: + # Search all the infered nodes for something which is unpackable + # This handles the case of multiple returns in a function + # where all returns should be unpackable + for infered_node in chain([infered], inference): + if infered_node is astroid.YES: continue + + for meth in ('__iter__', '__getitem__'): + try: + infered_node.getattr(meth) + except astroid.NotFoundError: + continue + else: + break else: - break - else: - self.add_message('unpacking-non-sequence', - node=node) + self.add_message('unpacking-non-sequence', + node=node) + break def _check_module_attrs(self, node, module, module_names): """check that module_names (list of string) are accessible through the |