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

Have "exact_type" attribute for tokens #58

Open
hydrargyrum opened this issue Apr 25, 2020 · 2 comments
Open

Have "exact_type" attribute for tokens #58

hydrargyrum opened this issue Apr 25, 2020 · 2 comments

Comments

@hydrargyrum
Copy link

hydrargyrum commented Apr 25, 2020

With tokenize module:

>>> next(tokenize.generate_tokens(iter(['()\n']).__next__)).type == token.OP
True
>>> next(tokenize.generate_tokens(iter(['()\n']).__next__)).exact_type == token.LPAR
True

With asttokens:

>>> asttokens.ASTTokens('()\n', True).tokens[0].type == token.OP
True
>>> asttokens.ASTTokens('()\n', True).tokens[0].exact_type == token.LPAR
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'Token' object has no attribute 'exact_type'

The exact type gives a more detailed type when type is generic OP value.

@alexmojaki
Copy link
Contributor

What's the advantage of exact_type compared to checking the contents of the token (I can't remember the attribute - probably .string or .text)?

@hydrargyrum
Copy link
Author

Cleaner code, clarity, using enums rather than hardcoded strings, less typo risk, easier to grep a named constant rather than a metacharacter, consistence/compatibility with code using the tokenize standard module.

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