From a48d635633ab6aa1cdd3ebb8fa2bb000ab0a0e50 Mon Sep 17 00:00:00 2001 From: Jimmy Lai Date: Wed, 27 May 2020 16:46:48 -0700 Subject: [PATCH] [scope] remove iter call to be efficient --- libcst/metadata/scope_provider.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/libcst/metadata/scope_provider.py b/libcst/metadata/scope_provider.py index 56c1b252b..db25d5e2e 100644 --- a/libcst/metadata/scope_provider.py +++ b/libcst/metadata/scope_provider.py @@ -14,7 +14,6 @@ from typing import ( Collection, Dict, - Iterable, Iterator, List, Mapping, @@ -607,22 +606,23 @@ class ComprehensionScope(LocalScope): # each string has the corresponding CSTNode attached to it def _gen_dotted_names( node: Union[cst.Attribute, cst.Name] -) -> Iterable[Tuple[str, Union[cst.Attribute, cst.Name]]]: +) -> Iterator[Tuple[str, Union[cst.Attribute, cst.Name]]]: if isinstance(node, cst.Name): - yield (node.value, node) + yield node.value, node else: value = node.value if not isinstance(value, (cst.Attribute, cst.Name)): # this is not an import return - name_values = iter(_gen_dotted_names(value)) - next_pair = next(name_values, None) - if next_pair is None: + name_values = _gen_dotted_names(value) + try: + next_name, next_node = next(name_values) + except StopIteration: return - (next_name, next_node) = next_pair - yield (f"{next_name}.{node.attr.value}", node) - yield (next_name, next_node) - yield from name_values + else: + yield f"{next_name}.{node.attr.value}", node + yield next_name, next_node + yield from name_values class ScopeVisitor(cst.CSTVisitor):