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

Add support for dotted imports in get_qualified_names #290

Merged
merged 3 commits into from
Apr 28, 2020

Conversation

zsol
Copy link
Member

@zsol zsol commented Apr 27, 2020

Summary

get_qualified_names now returns the most appropriate qualified name for both import a.b.c style imports as well as later references to them.

Test Plan

Added unit test.

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Apr 27, 2020
@zsol zsol changed the title qname-for-dotted-imports Add support for dotted imports in get_qualified_names Apr 27, 2020
@zsol zsol force-pushed the qname-for-dotted-imports branch from 83920a0 to 1c8b3d4 Compare April 27, 2020 09:11
# 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)))
Copy link
Contributor

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.

Suggested change
".".join(parts[: i + 1]) for i in reversed(range(len(parts)))
".".join(parts[: i]) for i in range(len(parts), 0, -1)

Copy link
Member Author

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.

Comment on lines 436 to 440
for i in reversed(range(len(parts))):
prefix = ".".join(parts[: i + 1])
if prefix in self:
assignments = self[prefix]
break
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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

Comment on lines 249 to 253
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):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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):

@jimmylai
Copy link
Contributor

Is this PR related to #286?

@zsol
Copy link
Member Author

zsol commented Apr 27, 2020

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):
Copy link
Contributor

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.

Copy link
Contributor

@jimmylai jimmylai left a 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.

@jimmylai jimmylai merged commit 6f6223d into Instagram:master Apr 28, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants