CLI tool to convert a python project's %-formatted strings to f-strings.
Maintained. Last release 4 months ago, commits in 2 of the last 26 weeks. Weaker on commit activity and release cadence.
pip install flyntflynt - string formatting converter
flynt is a command line tool to automatically convert a project's Python code from old "%-formatted" and .format(...) strings into Python 3.6+'s "f-strings".
F-Strings:
Not only are they more readable, more concise, and less prone to error than other ways of formatting, but they are also faster!
Installation
pip install flynt. It requires Python version 3.9+.
Usage
Flynt will modify the files it runs on. Add your project to version control system before using flynt.
To run: flynt {source_file_or_directory}
- Given a single file, it will 'f-stringify' it: replace all applicable string formatting in this file (file will be modified).
- Given a folder, it will search the folder recursively and f-stringify all the .py files it finds. It skips some hard-coded folder names:
blacklist = {'.tox', 'venv', 'site-packages', '.eggs'}.
It turns the code it runs on into Python 3.6+, since 3.6 is when "f-strings" were introduced.
Command line options
From the output of flynt -h:
usage: flynt [-h] [-v | -q] [--no-multiline | -ll LINE_LENGTH] [-d |
--stdout] [-s] [--no-tp] [--no-tf] [-tc] [-tj] [-f]
[-a] [-e EXCLUDE [EXCLUDE ...]] [-nb] [--version]
[--report]
[src ...]
flynt v.1.0.3
positional arguments:
src source file(s) or directory (or a single `-`
to read stdin and output to stdout)
options:
-h, --help show this help message and exit
-v, --verbose run with verbose output
-q, --quiet run without outputting statistics to stdout
--no-multiline convert only single line expressions
-ll, --line-length LINE_LENGTH
for expressions spanning multiple lines,
convert only if the resulting single line
will fit into the line length limit. Default
value is 88 characters.
-d, --dry-run Do not change the files in-place and print
the diff instead. Note that this must be
used in conjunction with '--fail-on-change'
when used for linting purposes.
--stdout Do not change the files in-place and print
the result instead. This argument implies
--quiet, i.e. no statistics are printed to
stdout, only the resulting code. It is
incompatible with --dry-run and --verbose.
-s, --string Interpret the input as a Python code snippet
and print the converted version. The snippet
must use single quotes or escaped double
quotes.
--no-tp, --no-transform-percent
Don't transform % formatting to f-strings
(default: do so)
--no-tf, --no-transform-format
Don't transform .format formatting to
f-strings (default: do so)
-tc, --transform-concats
Replace string concatenations (defined as +
operations involving string literals) with
f-strings. Available only if flynt is
installed with a 3.9+ interpreter.
-tj, --transform-joins
Replace static joins (where the joiner is a
string literal and the joinee is a static-
length list) with f-strings. Available only
if flynt is installed with a 3.9+
interpreter.
-f, --fail-on-change Fail when changing files (for linting
purposes)
-a, --aggressive Include conversions with potentially changed
behavior. Use -aa to omit int() wrapping for
%d conversions.
-e, --exclude EXCLUDE [EXCLUDE ...]
ignore files with given strings in it's
absolute path.
-nb, --notebook Also search and transform Jupyter notebooks
(.ipynb files). Warning: feature in alpha
and was not thoroughly tested.
--version Print the current version number and exit.
--report Show detailed conversion report