diff --git a/chalice/analyzer.py b/chalice/analyzer.py index c270aac69..2b41f5e7b 100644 --- a/chalice/analyzer.py +++ b/chalice/analyzer.py @@ -565,7 +565,8 @@ def visit_GeneratorExp(self, node): def _handle_comprehension(self, node, comprehension_type): # type: (Union[ast.ListComp, ast.GeneratorExp], str) -> None - child_scope = self._get_matching_sub_namespace(comprehension_type) + child_scope = self._get_matching_sub_namespace( + comprehension_type, node.lineno) if child_scope is None: # If there's no child scope (listcomps in py2) then we can # just analyze the node.elt node in the current scope instead @@ -577,11 +578,11 @@ def _handle_comprehension(self, node, comprehension_type): ParsedCode(node.elt, child_table), self._binder) child_infer.bind_types() - def _get_matching_sub_namespace(self, name): - # type: (str) -> symtable.SymbolTable + def _get_matching_sub_namespace(self, name, lineno): + # type: (str, int) -> symtable.SymbolTable namespaces = [ t for t in self._symbol_table.get_sub_namespaces() - if t.get_name() == name] + if t.get_name() == name and t.get_lineno() == lineno] if not namespaces: return # We're making a simplification and using the genexpr subnamespace. diff --git a/tests/unit/test_analyzer.py b/tests/unit/test_analyzer.py index 881a33ca6..c00ae0d93 100644 --- a/tests/unit/test_analyzer.py +++ b/tests/unit/test_analyzer.py @@ -457,6 +457,17 @@ def test_can_handle_list_expr_with_api_calls(): """) == {'dynamodb': set(['list_tables'])} +def test_can_handle_multiple_listcomps(): + assert aws_calls("""\ + bar_key = 'bar' + baz_key = 'baz' + items = [{'foo': 'sun', 'bar': 'moon', 'baz': 'stars'}] + foos = [t['foo'] for i in items] + bars = [t[bar_key] for t in items] + bazs = [t[baz_key] for t in items] + """) == {} + + # def test_can_handle_dict_comp(): # assert aws_calls("""\ # import boto3