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

libcst.metadata.Scope.get_qualified_names_for doesn't handle relative imports #460

Closed
lpetre opened this issue Feb 26, 2021 · 0 comments · Fixed by #465
Closed

libcst.metadata.Scope.get_qualified_names_for doesn't handle relative imports #460

lpetre opened this issue Feb 26, 2021 · 0 comments · Fixed by #465

Comments

@lpetre
Copy link
Contributor

lpetre commented Feb 26, 2021

This also applies to QualifiedNameProvider. As far as I can tell, these types don't have the necessary context (ie full module name) to resolve relative imports.

libcst.helpers.get_absolute_module_for_import could resolve the proper name if we had access to a full module name for the scope.

Working:

>>> wrapper = cst.MetadataWrapper(cst.parse_module("from lib import a\na()"))
>>> scopes = wrapper.resolve(cst.metadata.ScopeProvider)
>>> scope_of_module = scopes[wrapper.module]
>>> a_call = wrapper.module.body[1].body[0].value
>>> scope_of_module.get_qualified_names_for(a_call)
{QualifiedName(name='lib.a', source=<QualifiedNameSource.IMPORT: 1>)}

Broken:

>>> wrapper = cst.MetadataWrapper(cst.parse_module("from . import a\na()"))
>>> scopes = wrapper.resolve(cst.metadata.ScopeProvider)
>>> scope_of_module = scopes[wrapper.module]
>>> a_call = wrapper.module.body[1].body[0].value
>>> scope_of_module.get_qualified_names_for(a_call)
{QualifiedName(name='a', source=<QualifiedNameSource.IMPORT: 1>)}

I believe this TODO is necessary to implement (somehow): https://github.com/Instagram/LibCST/blob/master/libcst/metadata/scope_provider.py#L298

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant