Skip to content

Commit

Permalink
Handle non-import attribute accesses gracefully
Browse files Browse the repository at this point in the history
  • Loading branch information
zsol authored and jimmylai committed Apr 28, 2020
1 parent 6f6223d commit 93a389b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
6 changes: 4 additions & 2 deletions libcst/metadata/scope_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -613,7 +613,8 @@ def _gen_dotted_names(
else:
value = node.value
if not isinstance(value, (cst.Attribute, cst.Name)):
raise ValueError(f"Unexpected name value in import: {value}")
# this is not an import
return
name_values = iter(_gen_dotted_names(value))
(next_name, next_node) = next(name_values)
yield (f"{next_name}.{node.attr.value}", node)
Expand Down Expand Up @@ -830,7 +831,8 @@ def infer_accesses(self) -> None:
if name in access.scope:
access.node = node
break
assert name is not None
if name is None:
continue
else:
name = ensure_type(access.node, cst.Name).value
scope_name_accesses[(access.scope, name)].add(access)
Expand Down
7 changes: 7 additions & 0 deletions libcst/metadata/tests/test_scope_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -1106,6 +1106,13 @@ def test_contains_is_read_only(self) -> None:
list(scope.parent._accesses.items()), before_parent_accesses
)

def test_attribute_of_function_call(self) -> None:
get_scope_metadata_provider("foo().bar")

def test_self(self) -> None:
with open(__file__) as f:
get_scope_metadata_provider(f.read())

def test_get_qualified_names_for_is_read_only(self) -> None:
m, scopes = get_scope_metadata_provider(
"""
Expand Down

0 comments on commit 93a389b

Please sign in to comment.