Skip to content
This repository has been archived by the owner on Jul 5, 2023. It is now read-only.

Inconsistent parsing of f-strings between typed_ast and the builtin ast #64

Closed
PCManticore opened this issue Sep 23, 2018 · 3 comments
Closed

Comments

@PCManticore
Copy link

Hey folks!

I've noticed that typed_ast doesn't parse f-strings in the same way as the builtin ast module does. For instance in the following example, we only get a FormattedValue while the builtin ast module returns a JoinedStr:

import typed_ast.ast3 as typed_ast
import ast

f = typed_ast.parse("f'{a}'")
print(f.body[0].value)

f = ast.parse("f'{a}'")
print(f.body[0].value)

# going to print the following
<_ast3.FormattedValue object at 0x1040a09e8>
<_ast.JoinedStr object at 0x1040a0b00>

It would be great to have the same output as the builtin ast module. They both return a JoinedStr if the variable interpolation element is followed by one or more characters.

@emmatyping
Copy link
Contributor

Hi, when you say "the builtin ast module" what version of Python are you talking about? It maybe be that this is due to #60 not being complete. typed_ast is a fork of the 3.6.0 builtin ast module.

@PCManticore
Copy link
Author

@ethanhs Yes, good catch, I was referring to Python 3.6. I didn't check if this has been fixed in 3.7 though.

@msullivan
Copy link
Collaborator

I took a look at this, and the behavior of ast changed between 3.6.0 and 3.6.1 as a result of https://bugs.python.org/issue28739.

Since typed_ast was a fork of 3.6.0, we matched the 3.6.0 behavior which was inconsistent with later python versions. We have updated typed_ast to be based on 3.7.2, and the inconsistency is fixed.

(Though we don't have versions of typed_ast that are "bug-for-bug compatible" with 3.6.1+, which is a lacuna, if one that doesn't impact mypy's uses at least. If there's demand for such a thing, I'd be happy to review a PR that ported the patch in, though there would be serious questions about version numbers.)

tbbharaj pushed a commit to tbbharaj/typed_ast that referenced this issue Dec 6, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants