pythonpython-3.xpython-3.5f-string

how to fix f-strings breaking backward compatibility python 3.5/3.6+


I have found some code which is apparently designed for python 3.6.

For some reason I need to run that code in a python 3.5 environment. Supposedly, the two versions should be backward compatible. However, the original code abuses in f-strings, raising hundreds of errors in py35

do you know any tool to remove those fstrings, or a clean workaround?


Solution

  • Yes you can - it is possible to remove f-string and to replace them by their equivalent format() call. That is F"my{x:s}" becomes "my{:s}".format(x).

    In a number of cases, that works with some grep/sed but it is better to use a tool that understands the python syntax. As I needed to support older python versions removing other features as well, I have written my own tool for that. Look for the --replace-fstring transformer.

    https://github.com/gdraheim/strip_python3 or pip install strip-python3