-
Notifications
You must be signed in to change notification settings - Fork 196
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
Add support for dotted imports in get_qualified_names #290
Conversation
83920a0
to
1c8b3d4
Compare
libcst/metadata/scope_provider.py
Outdated
# for these we want to find the longest prefix that matches full_name | ||
parts = real_name.split(".") | ||
real_names = [ | ||
".".join(parts[: i + 1]) for i in reversed(range(len(parts))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is simpler and probably also easier to read.
".".join(parts[: i + 1]) for i in reversed(range(len(parts))) | |
".".join(parts[: i]) for i in range(len(parts), 0, -1) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that's actually what I started with but the numbers felt a bit arbitrary. Happy to change it back though.
libcst/metadata/scope_provider.py
Outdated
for i in reversed(range(len(parts))): | ||
prefix = ".".join(parts[: i + 1]) | ||
if prefix in self: | ||
assignments = self[prefix] | ||
break |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for i in reversed(range(len(parts))): | |
prefix = ".".join(parts[: i + 1]) | |
if prefix in self: | |
assignments = self[prefix] | |
break | |
for i in range(len(parts), 0, -1): | |
prefix = ".".join(parts[: i]) | |
if prefix in self: | |
assignments = self[prefix] | |
break |
libcst/metadata/scope_provider.py
Outdated
if name and name.asname: | ||
name_asname = name.asname | ||
if name_asname: | ||
as_name = cst.ensure_type(name_asname.name, cst.Name).value | ||
if full_name.startswith(as_name): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if name and name.asname: | |
name_asname = name.asname | |
if name_asname: | |
as_name = cst.ensure_type(name_asname.name, cst.Name).value | |
if full_name.startswith(as_name): | |
as_name = name.evaluated_alias | |
if as_name full_name.startswith(as_name): |
Is this PR related to #286? |
It's not, I just noticed that one. I'll look at it tomorrow. |
if real_name: | ||
if name and name.asname: | ||
as_name = name.evaluated_alias | ||
if full_name.startswith(as_name): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pyre complains as_name can be None
but startswith
doesn't take None
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Other than the Pyre error, LGTM.
Summary
get_qualified_names
now returns the most appropriate qualified name for bothimport a.b.c
style imports as well as later references to them.Test Plan
Added unit test.