-
Notifications
You must be signed in to change notification settings - Fork 33
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
Parser does not handle Python3.6 f-strings #4
Comments
There is unfortunately an open bug in Python with annotating AST nodes from f-strings with proper line and column info: https://bugs.python.org/issue29051. In addition, the f-string literal is a single token, but the expressions inside would also need to be tokens: it would require some adjustments to when and how we parse tokens; in particular, the standard I am open to proposals for how to handle it. |
I think there are two issues here: The exception and the lack of inside-tokens. It seems like even though fstrings are a single token, it should still be possible to use the locations given by the tokenize module:
Maybe I'm not understanding the bug, however |
Actually, could you share the full test case that produces the error. I am not seeing it with a just a single f-string literal. |
Okay so after experimentation it seems like this is a min case: def t():
return f'{function(kwarg=24)}' will give:
but
alone will give a different exception:
Most likely happening because of the same issue, but reporting both instances since they "look" different |
I pushed a fix to master, with a test case that includes your examples. The fix is simply to avoid descending inside an f-string, since we have neither locations nor tokens for the inner nodes. Please check that it solves your issue. You can keep this bug open, or close and create a new one. Either way, the remaining bug is that asttokens does not include or annotate the AST subtrees inside fstrings. |
I'm guessing this explains this exception? That's a fair resolution for now since python lacks the ability to parse it. Hopefully when this issue resolves, this library would be able to improve. Thanks for the fast resolution! P.S maybe a version bump is in order? |
Because of a python 3.6.1 bug described in https://bugs.python.org/issue29051, asttokens currently does not descend these nodes. Therefore, we override the visit method to completely ignore those nodes to avoid an exception. More information about this resolution at gristlabs/asttokens#4
Version bumped, and uploaded package with fix to pypi. The exception you mention -- yeah, I guess it makes sense. Hmm... With my workaround, I don't visit those nodes at all. To assign None, I'd have to visit them, but remember that they are under a JoinedStr and skip most of the processing. That's kind of annoying. How much of an issue is it? Might it make sense to let it hang until we can just solve it (issue #6) properly? |
An exception is raised when trying to parse python3.6's new f-strings.
The text was updated successfully, but these errors were encountered: