question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support for 3.6 f-string syntax

See original GitHub issue

Apologies for the brief report, but here’s a simple bug reproduction session in the shell:

$ mktmpenv -p python3.6  # this command is from virtualenvwrapper
(tmp-38ce2dcf0268df9) $ python --version
Python 3.6.0b2
(tmp-38ce2dcf0268df9) $ cat test.py 
target = "world"
print(f"Hello, {target}!")
(tmp-38ce2dcf0268df9) $ python test.py 
Hello, world!
(tmp-38ce2dcf0268df9) $ pip install yapf==0.14.0
(tmp-38ce2dcf0268df9) $ yapf -i test.py
Traceback (most recent call last):
  File "/home/dpassaro/.virtualenvs/tmp-38ce2dcf0268df9/lib/python3.6/site-packages/yapf/yapflib/pytree_utils.py", line 102, in ParseCodeToTree
    tree = parser_driver.parse_string(code, debug=False)
  File "/home/dpassaro/.pyenv/versions/3.6.0b2/lib/python3.6/lib2to3/pgen2/driver.py", line 106, in parse_string
    return self.parse_tokens(tokens, debug)
  File "/home/dpassaro/.pyenv/versions/3.6.0b2/lib/python3.6/lib2to3/pgen2/driver.py", line 71, in parse_tokens
    if p.addtoken(type, value, (prefix, start)):
  File "/home/dpassaro/.pyenv/versions/3.6.0b2/lib/python3.6/lib2to3/pgen2/parse.py", line 159, in addtoken
    raise ParseError("bad input", type, value, context)
lib2to3.pgen2.parse.ParseError: bad input: type=3, value='"Hello, {target}!"', context=('', (2, 7))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/dpassaro/.virtualenvs/tmp-38ce2dcf0268df9/bin/yapf", line 11, in <module>
    sys.exit(run_main())
  File "/home/dpassaro/.virtualenvs/tmp-38ce2dcf0268df9/lib/python3.6/site-packages/yapf/__init__.py", line 296, in run_main
    sys.exit(main(sys.argv))
  File "/home/dpassaro/.virtualenvs/tmp-38ce2dcf0268df9/lib/python3.6/site-packages/yapf/__init__.py", line 188, in main
    parallel=args.parallel)
  File "/home/dpassaro/.virtualenvs/tmp-38ce2dcf0268df9/lib/python3.6/site-packages/yapf/__init__.py", line 236, in FormatFiles
    in_place, print_diff, verify)
  File "/home/dpassaro/.virtualenvs/tmp-38ce2dcf0268df9/lib/python3.6/site-packages/yapf/__init__.py", line 259, in _FormatFile
    logger=logging.warning)
  File "/home/dpassaro/.virtualenvs/tmp-38ce2dcf0268df9/lib/python3.6/site-packages/yapf/yapflib/yapf_api.py", line 90, in FormatFile
    verify=verify)
  File "/home/dpassaro/.virtualenvs/tmp-38ce2dcf0268df9/lib/python3.6/site-packages/yapf/yapflib/yapf_api.py", line 126, in FormatCode
    tree = pytree_utils.ParseCodeToTree(unformatted_source)
  File "/home/dpassaro/.virtualenvs/tmp-38ce2dcf0268df9/lib/python3.6/site-packages/yapf/yapflib/pytree_utils.py", line 108, in ParseCodeToTree
    tree = parser_driver.parse_string(code, debug=False)
  File "/home/dpassaro/.pyenv/versions/3.6.0b2/lib/python3.6/lib2to3/pgen2/driver.py", line 106, in parse_string
    return self.parse_tokens(tokens, debug)
  File "/home/dpassaro/.pyenv/versions/3.6.0b2/lib/python3.6/lib2to3/pgen2/driver.py", line 71, in parse_tokens
    if p.addtoken(type, value, (prefix, start)):
  File "/home/dpassaro/.pyenv/versions/3.6.0b2/lib/python3.6/lib2to3/pgen2/parse.py", line 159, in addtoken
    raise ParseError("bad input", type, value, context)
lib2to3.pgen2.parse.ParseError: bad input: type=3, value='"Hello, {target}!"', context=('', (2, 7))

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:35
  • Comments:31 (8 by maintainers)

github_iconTop GitHub Comments

11reactions
bolinfestcommented, Apr 14, 2017

Can we accept the really hacky fix then? Once you start using f-strings, there’s no going back to %s.

9reactions
bwendlingcommented, Apr 18, 2017

Notice that this is something they plan on fixing in lib2to3

https://bugs.python.org/issue23894

Read more comments on GitHub >

github_iconTop Results From Across the Web

Python 3's f-Strings: An Improved String Formatting Syntax ...
As of Python 3.6, f-strings are a great new way to format strings. Not only are they more readable, more concise, and less...
Read more >
What's New In Python 3.6 — Python 3.11.1 documentation
PEP 498 introduces a new kind of string literals: f-strings, or formatted string literals. Formatted string literals are prefixed with 'f' and are...
Read more >
Python f-String Tutorial – String Formatting in ... - freeCodeCamp
use the title() method on the string author ,; store the returned string in another variable, and; print it using an f-String, as...
Read more >
formatting strings in Python with f-string - ZetCode
Python f-string is the newest Python syntax to do string formatting. It is available since Python 3.6. Python f-strings provide a faster, ...
Read more >
A Guide to Formatting with f-strings in Python - CIS Sandbox
The release of Python version 3.6 introduced formatted string literals, simply called ... outside of the curly braces are literal strings. f-strings support...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found