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

AttributeError: 'FormattedValue' object has no attribute 's' #1

Closed
cioma opened this issue Mar 12, 2019 · 7 comments
Closed

AttributeError: 'FormattedValue' object has no attribute 's' #1

cioma opened this issue Mar 12, 2019 · 7 comments
Labels
fixed This issue has been fixed grammar Relate to Python grammar

Comments

@cioma
Copy link

cioma commented Mar 12, 2019

I have a Python source file (see attachment) that gives the following error when I try to convert it with f2format v0.4.3 on Ubuntu 18.04 64-bit using Python 3.6.5:
AttributeError: 'FormattedValue' object has no attribute 's'
register.py.zip

@JarryShaw JarryShaw added bug Something isn't working typed_ast Relate to` typed_ast` grammar Relate to Python grammar and removed typed_ast Relate to` typed_ast` labels Mar 13, 2019
@JarryShaw
Copy link
Member

Seems it's a grammar I didn't think of. Thx for reporting.

@cioma
Copy link
Author

cioma commented Mar 13, 2019

Many thanks!
There is another file that fails with different error, I'll open a separate issue for that.

@JarryShaw
Copy link
Member

JarryShaw commented Mar 13, 2019

Expressions cannot contain ':' or '!' outside of strings or parentheses, brackets, or braces. The exception is that the '!=' operator is allowed as a special case. (c.f. PEP 498, thx to @gousaiyang)

As described in PEP 498, f-string may contains evaluated expressions as format specifiers. AttributeError in this issue and SyntaxError in issue #2 are both because of this.

  • for ast.FormattedValue.format_spec, which is an ast.JoinedStr, the elements in its values attribute (a list) can either be of an ast.Str (thus has an s attribute) or other types (ast.Name in this case) [AttributeError resolved in v0.4.4.dev1 but other types not reviewed]
  • when f2format extracts all entries of expressions in the f-strings, it uses brute force to find the end of expression; when the format specifiers are expressions, it has problem to find the right ending point [to revise f2format.core.convert.find_rbrace, maybe try some cleverer ways]

JarryShaw added a commit that referenced this issue Mar 13, 2019
 * identified bugs (c.f. issue #1)
 * upload example files from bug report (c.f. issue #1 & #2)
@cioma
Copy link
Author

cioma commented Mar 13, 2019

I'll also see a way to modify f-string in my code to make it easier for f2format.

@cioma
Copy link
Author

cioma commented Mar 14, 2019

It seems in my case f2format fails when trying to convert an f-string that has format specifiers which contain evaluated expressions (see PEP 498, Format specifiers)

E.g. a line like this:
bits = f'{bitfield.bits:{bits_width}}'

Shall be converted like this:
bits = '{:{}}'.format(bitfield.bits, bits_width)

JarryShaw added a commit that referenced this issue Apr 6, 2019
 * fixed grammar bugs as reported in #1 & #2
 * other regular updates
@JarryShaw JarryShaw unpinned this issue Apr 6, 2019
@JarryShaw
Copy link
Member

This issue should have been revolved in version 0.5.0. Thx for your report @cioma

@JarryShaw JarryShaw added fixed This issue has been fixed and removed bug Something isn't working labels Apr 6, 2019
@cioma
Copy link
Author

cioma commented Apr 6, 2019

Seems to be working fine for me, many thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fixed This issue has been fixed grammar Relate to Python grammar
Projects
None yet
Development

No branches or pull requests

2 participants