Flake8 Use f-string

1.4 · active · verified Wed Apr 15

Flake8-use-fstring is a Flake8 plugin designed to enforce the use of f-strings for string formatting within Python projects. It checks for instances where `%` formatting or the `.format()` method are used and suggests their replacement with f-strings. The current version is 1.4, and it sees active, though infrequent, maintenance releases, primarily for bug fixes and compatibility updates.

Warnings

Install

Quickstart

Install `flake8-use-fstring` and simply run `flake8` on your Python files. The plugin automatically integrates and reports `FS001` for `%` formatting and `FS002` for `.format()` usage. To also check for strings that appear to be f-strings but lack the `f` prefix (`FS003`), you need to explicitly enable this extension. You can also adjust the 'greedy' levels for string formatting detection.

import os

def old_style_formatting():
    name = "World"
    print("Hello, %s!" % name)  # FS001
    print("Hello, {}!".format(name)) # FS002

def missing_f_prefix():
    # This will trigger FS003 if enabled
    message = "User: {os.environ.get('USER', 'guest')}"

def fstring_example():
    name = "Pythonista"
    print(f"Hello, {name}!")

old_style_formatting()
missing_f_prefix()
fstring_example()

# To run this example:
# 1. Save the code above as `my_module.py`
# 2. Run flake8:
#    $ flake8 my_module.py
#
# To see FS003 warnings, enable extensions:
#    $ flake8 --enable-extensions=FS003 my_module.py
#
# For more aggressive checking (e.g., if 'name' was not a literal string):
#    $ flake8 --percent-greedy=2 --format-greedy=2 my_module.py

view raw JSON →