{"id":6641,"library":"flake8-use-fstring","title":"Flake8 Use f-string","description":"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.","status":"active","version":"1.4","language":"en","source_language":"en","source_url":"https://github.com/MichaelKim0407/flake8-use-fstring","tags":["flake8","linting","f-string","code-quality","formatting"],"install":[{"cmd":"pip install flake8-use-fstring","lang":"bash","label":"Install with pip"}],"dependencies":[{"reason":"This is a plugin for Flake8 and requires Flake8 to function.","package":"flake8","optional":false}],"imports":[],"quickstart":{"code":"import os\n\ndef old_style_formatting():\n    name = \"World\"\n    print(\"Hello, %s!\" % name)  # FS001\n    print(\"Hello, {}!\".format(name)) # FS002\n\ndef missing_f_prefix():\n    # This will trigger FS003 if enabled\n    message = \"User: {os.environ.get('USER', 'guest')}\"\n\ndef fstring_example():\n    name = \"Pythonista\"\n    print(f\"Hello, {name}!\")\n\nold_style_formatting()\nmissing_f_prefix()\nfstring_example()\n\n# To run this example:\n# 1. Save the code above as `my_module.py`\n# 2. Run flake8:\n#    $ flake8 my_module.py\n#\n# To see FS003 warnings, enable extensions:\n#    $ flake8 --enable-extensions=FS003 my_module.py\n#\n# For more aggressive checking (e.g., if 'name' was not a literal string):\n#    $ flake8 --percent-greedy=2 --format-greedy=2 my_module.py","lang":"python","description":"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."},"warnings":[{"fix":"Enable with `flake8 --enable-extensions=FS003` or in your `.flake8` config file under `enable-extensions = FS003`.","message":"The `FS003` rule, which checks for missing 'f' prefixes on strings containing curly brackets, is disabled by default. It must be explicitly enabled using `--enable-extensions=FS003` (or `enable-extensions=FS003` in config) because it can produce false positives, especially with regular expressions.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Consider using the default `percent-greedy=0` and `format-greedy=0` to avoid false positives. If enabling higher levels, carefully review reported issues or use `# noqa` to suppress specific lines.","message":"The plugin's 'greedy levels' (`--percent-greedy` and `--format-greedy`) for detecting string formatting can lead to false positives at higher levels (1 or 2) if the value immediately preceding `%` or `.format` is not a string literal. The default level 0 is safer but less aggressive.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Be aware that byte strings will not be linted for f-string compliance by this plugin. If you need to enforce a specific style for byte strings, an alternative linter or custom check would be required.","message":"As of v1.4, `flake8-use-fstring` explicitly skips byte literals (strings prefixed with `b`) when enforcing f-string usage. This means it will not report errors for old-style formatting on byte strings.","severity":"gotcha","affected_versions":">=1.4"},{"fix":"For logging, pass variables as arguments to the logging method (e.g., `logger.info(\"Message with %s\", var)` or `logger.info(\"Message with {}\", var)`). This allows the logging system to defer formatting and extract structured data more efficiently.","message":"While `flake8-use-fstring` encourages f-strings, using them directly for logging messages (e.g., `logger.info(f\"User: {user}\")`) is generally discouraged. The Python `logging` module is optimized for deferred string formatting and structured logging when arguments are passed separately (e.g., `logger.info(\"User: %s\", user)` or `logger.info(\"User: {}\", user)`).","severity":"gotcha","affected_versions":"All versions"},{"fix":"Ensure your `flake8` installation is up-to-date, preferably v4.x or later, when using `flake8-use-fstring` v1.2 and above. Run `pip install --upgrade flake8`.","message":"Version 1.2 of `flake8-use-fstring` introduced support for Flake8 v4. If you are using an older version of Flake8, you may encounter compatibility issues or unexpected behavior.","severity":"compatibility","affected_versions":"<1.2"}],"env_vars":null,"last_verified":"2026-04-15T00:00:00.000Z","next_check":"2026-07-14T00:00:00.000Z","problems":[]}