Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ScopeProvider sometimes returns nodes that don't appear in the CST #381

Closed
zsol opened this issue Sep 2, 2020 · 3 comments · Fixed by #483
Closed

ScopeProvider sometimes returns nodes that don't appear in the CST #381

zsol opened this issue Sep 2, 2020 · 3 comments · Fixed by #483
Labels
bug Something isn't working

Comments

@zsol
Copy link
Member

zsol commented Sep 2, 2020

Consider this example code:

def f() -> "None":
  pass

Scope analysis will return an Access for a node like Name(value="None"), but there's no such CST node in the code. This is a problem because users of LibCST would expect to query metadata (for example the position) of nodes returned by scope analysis.

Here's some code to demonstrate the problem:

import libcst as cst
m = cst.metadata.MetadataWrapper(cst.parse_module("def f() -> \"None\": pass"))
pos = m.resolve(cst.metadata.PositionProvider)
global_scope = list(filter(None, m.resolve(cst.metadata.ScopeProvider).values()))[0]
alien_node = list(global_scope.accesses['None'])[0].node
pos[alien_node]  # BOOM

This produces an exception like the following:

KeyError: Name(
    value='None',
    lpar=[],
    rpar=[],
)
@zsol
Copy link
Member Author

zsol commented Sep 2, 2020

I suspect the issue exists since #373, where we construct a new CST for each string annotation, and then nodes from this constructed CST are exposed by ScopeProvider. I think we should instead expose the original string annotation CST nodes as the referrer nodes.

@jimmylai
Copy link
Contributor

jimmylai commented Sep 2, 2020

I suspect the issue exists since #373, where we construct a new CST for each string annotation, and then nodes from this constructed CST are exposed by ScopeProvider. I think we should instead expose the original string annotation CST nodes as the referrer nodes.

Nice catch! The change does expose new CSTNode which not existed in the original node.

The suggestion makes sense for simple type annotation with a type name.

For the case that the String is an expression with multiple parts, is exposing the entire string annotation still useful?
E.g. a: "Optional[A, B]" = None

@zsol
Copy link
Member Author

zsol commented Sep 2, 2020

I think it's still better than nothing. Not to mention it would be great to keep the Access.node attribute as not Optional

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants