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

Fix StopIteration exception during scope analysis #298

Merged

Conversation

zsol
Copy link
Member

@zsol zsol commented May 21, 2020

Summary

During scope analysis all attribute accesses are collected for matching on
import names. The matching code (specifically _gen_dotted_names) was not
prepared for all types of expressions. In particular, complex expressions like
foo[0].bar.baz() caused a StopIteration exception when _gen_dotted_names
calls itself recursively. The nested call doesn't yield any values, and so
calling next() on it raises.

This commit fixes these types of errors.

Test Plan

Added test case

During scope analysis all attribute accesses are collected for matching on
import names. The matching code (specifically `_gen_dotted_names`) was not
prepared for all types of expressions. In particular, complex expressions like
`foo[0].bar.baz()` caused a `StopIteration` exception when `_gen_dotted_names`
calls itself recursively. The nested call doesn't yield any values, and so
calling `next()` on it raises.

This commit fixes these types of errors.
@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 May 21, 2020
@zsol zsol requested a review from jimmylai May 21, 2020 12:45
Comment on lines +619 to +621
next_pair = next(name_values, None)
if next_pair is None:
return
Copy link
Contributor

@jimmylai jimmylai May 21, 2020

Choose a reason for hiding this comment

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

Can we just add try ... except StopIteration? That can be more efficient and probably easier to understand.

Copy link
Member Author

Choose a reason for hiding this comment

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

Hmm, sure. Why is it more efficient?

Copy link
Contributor

Choose a reason for hiding this comment

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

That way we saved one extra next call.

Copy link
Member Author

Choose a reason for hiding this comment

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

Then I don't understand your suggestion :)
Apart from the way it's currently written, I can imagine this:

try:
    (next_name, next_node) = next(name_values)
    yield (f"...", ...)
    ...
except StopIteration:
    return

which is the same amount of next calls

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 comments LGTM.
Have you tried running this change on large codebase to test more edge cases?

@zsol
Copy link
Member Author

zsol commented May 21, 2020

I haven't yet.

next_pair = next(name_values, None)
if next_pair is None:
return
(next_name, next_node) = next_pair
Copy link
Contributor

Choose a reason for hiding this comment

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

We just need try catch on this line. Then we can save the next in next_pair = next(name_values, None).

@jimmylai
Copy link
Contributor

Merge this for now and will submit the proposed change in another PR.

@jimmylai jimmylai merged commit f32389a into Instagram:master May 27, 2020
@zsol zsol deleted the fix-stopiteration-during-attribute-infer branch June 1, 2020 08:53
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