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 error in end_{line,col} tracking #100

Merged
merged 2 commits into from
Feb 11, 2022
Merged

Conversation

jgalenson
Copy link

The current implementation sets the end line and column of a node to the end values for the next token after visiting that node. However, since visiting a node consumes all of its tokens, this sets them to the end of the first token outside the node. This makes it seem like the node contains the token after it.

For example, in the test added in this commit for "a = b + c", the current implementation has a's end_col as 3 and b's as 7, which means a contains the "=" and b contains the "+".

This commit fixes the issue by setting the end values to the start values for the next token.

The current implementation sets the end line and column of a node to the end values for the next token after visiting that node.  However, since visiting a node consumes all of its tokens, this sets them to the end of the first token outside the node.  This makes it seem like the node contains the token after it.

For example, in the test added in this commit for "a = b + c", the current implementation has a's end_col as 3 and b's as 7, which means a contains the "=" and b contains the "+".

This commit fixes the issue by setting the end values to the start values for the next token.
@jgalenson
Copy link
Author

Friendly ping @soupytwist


self.assertEqual(2, fmt.get(a, 'end_col'))
self.assertEqual(6, fmt.get(b, 'end_col'))
self.assertEqual(9, fmt.get(c, 'end_col'))
Copy link
Collaborator

Choose a reason for hiding this comment

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

This doesn't work in python2.7, which (tbh) isn't that big a deal, but pasta still supports it.

2.7:

"a + b"

NAME      'a' (1, 0), (1, 1)
OP        '+' (1, 2), (1, 3)
NAME      'b' (1, 4), (1, 5)
ENDMARKER ''  (2, 0), (2, 0)

3.9

"a + b"

NAME      'a' (1, 0), (1, 1)
OP        '+' (1, 2), (1, 3)
NAME      'b' (1, 4), (1, 5)
NEWLINE   ''  (1, 5), (1, 6)
ENDMARKER ''  (2, 0), (2, 0)

I'm not going to insist on reworking a bunch of token nonsense to get this to work, so proposing this instead:

# NB: 0 to support python2.7 which does not generate a NEWLINE token
self.assertIn(fmt.get(c, 'end_col'), (9, 0))

Copy link
Author

Choose a reason for hiding this comment

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

Thanks for finding that and suggesting the fix!

@soupytwist soupytwist merged commit 196d9fb into google:astlib Feb 11, 2022
@soupytwist
Copy link
Collaborator

Thanks!

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 this pull request may close these issues.

None yet

2 participants