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

Wrong token range given for list with trailing comma #1

Closed
LiraNuna opened this issue Jun 3, 2017 · 5 comments
Closed

Wrong token range given for list with trailing comma #1

LiraNuna opened this issue Jun 3, 2017 · 5 comments
Assignees

Comments

@LiraNuna
Copy link
Contributor

LiraNuna commented Jun 3, 2017

Hello,

Great library! I've been using it to implement this pre-commit hook that ensures trailing commas exist.

Here is a simple repro case to illustrate an issue I'm facing:

import ast, asttokens


class TestcaseVisitor(ast.NodeVisitor):
    def __init__(self, atok):
        self.atok = atok

    def visit_List(self, node):
        super().generic_visit(node)

        tokens = self.atok.token_range(node.first_token, node.last_token, include_extra=True)
        for token in tokens:
            print(token)


atok = asttokens.ASTTokens("""
[
    1,
]
""", parse=True)
visitor = TestcaseVisitor(atok)
visitor.visit(atok.tree)

This will output

OP:'['
NL:'\n'
NUMBER:'1'

Where the expected result should be

OP:'['
NL:'\n'
NUMBER:'1'
OP:','
NL:'\n'
OP:']'

Please note that removing the trailing comma will not reproduce the bug.
This issue is not limited to a single param - as long as there's a trailing comma. The following will produce similar a issue:

[
    1,
    2,
]
[
    1,
    2,
    3,
]

and so on

@LiraNuna LiraNuna changed the title Wrong token range given for single-item list with trailing comma Wrong token range given for list with trailing comma Jun 3, 2017
@LiraNuna
Copy link
Contributor Author

LiraNuna commented Jun 4, 2017

It seems like the problem is not limited to just List nodes but to Set and Dict nodes as well.

{
    1,
}

will also reproduce this issue. However, Tuple will not. This code will not reproduce the issue:

(
    1,
)

LiraNuna added a commit to LiraNuna/py-ensure-trailing-commas that referenced this issue Jun 4, 2017
Because of gristlabs/asttokens#1, some valid code will generate exceptions for when commas do not need to be added
@dsagal
Copy link
Member

dsagal commented Jun 5, 2017

Thank you for finding the bug and collecting all the details. I'll take a look.

@dsagal
Copy link
Member

dsagal commented Jun 5, 2017

Could you check out branch 'trailing_commas' to verify if it fixes the problem you found? I think it should also resolve Issue #2.

@LiraNuna
Copy link
Contributor Author

LiraNuna commented Jun 5, 2017

The branch seems to fix it! Thanks for the quick resolution and for the great library.

@dsagal
Copy link
Member

dsagal commented Jun 5, 2017

Excellent! I'll close this issue then.

@dsagal dsagal closed this as completed Jun 5, 2017
dsagal pushed a commit that referenced this issue Oct 23, 2019
Use simpler and more robust tree equality checking instead of astroid as_string
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

No branches or pull requests

2 participants