{"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":"python","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,"search_vec":"'1.4':53 'activ':57 'bug':64 'check':30 'code':75 'code-qu':74 'compat':67 'current':50 'design':14 'enforc':16 'f':4,21,47,72 'f-string':3,20,46,71 'fix':65 'flake8':1,7,12,69 'flake8-use-fstring':6 'format':25,34,37,77 'fstring':9 'infrequ':59 'instanc':32 'lint':70 'mainten':60 'method':38 'plugin':13 'primarili':62 'project':28 'python':27 'qualiti':76 'releas':61 'replac':44 'see':56 'string':5,22,24,48,73 'suggest':42 'though':58 'updat':68 'use':2,8,18,40 'version':51 'within':26","created_at":"2026-04-15T18:36:38.391706+00:00","updated_at":"2026-04-16T15:05:17.814727+00:00","problems":[{"fix":"Rewrite the string formatting using an f-string. Example: `print(f'Hello, {name}!')` instead of `print('Hello, %s!' % name)`.","cause":"The code uses the old-style `%` operator for string formatting, which flake8-use-fstring flags as a non-f-string usage.","error":"FS001: % formatting is used."},{"fix":"Rewrite the string formatting using an f-string. Example: `print(f'Hello, {name}!')` instead of `print('Hello, {}'.format(name))`.","cause":"The code uses the `.format()` method for string formatting, which flake8-use-fstring flags as a non-f-string usage.","error":"FS002: .format formatting is used."},{"fix":"If the string is intended to be an f-string, add the `f` prefix (e.g., `f'Hello, {name}'`). If it's a regular string with literal curly braces, escape them (e.g., `'{{literal}}'`). To enable this check, add `--enable-extensions=FS003` to your flake8 command or `enable-extensions = FS003` in your `.flake8` configuration file.","cause":"A string literal contains curly braces (`{}`) but is not prefixed with `f` (or `F`), suggesting it might be an f-string that's missing its prefix. This check (`FS003`) is disabled by default in flake8-use-fstring.","error":"FS003: f-string missing prefix (ignored by default)."},{"fix":"Adjust the 'percent-greedy' setting in your `.flake8` configuration file (e.g., `percent-greedy = 0` for the default, which only reports errors for string literals) or use `--percent-greedy=0` on the command line to reduce false positives.","cause":"flake8-use-fstring is reporting a false positive for `%` formatting on a non-string type, which often occurs when the 'percent-greedy' level is set too high (e.g., level 1 or 2).","error":"FS001: % formatting is used."},{"fix":"Adjust the 'format-greedy' setting in your `.flake8` configuration file (e.g., `format-greedy = 0` for the default, which only reports errors for string literals) or use `--format-greedy=0` on the command line to reduce false positives.","cause":"flake8-use-fstring is reporting a false positive for `.format()` usage on a non-string type, which often occurs when the 'format-greedy' level is set too high (e.g., level 1 or 2).","error":"FS002: .format formatting is used."}],"ecosystem":"pypi","meta_description":null,"install_score":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null,"cli_name":"flake8","cli_version":"7.3.0 (flake8-use-fstring: 1.4, mccabe: 0.7.0, pycodestyle: 2.14.0, pyflakes:","type":"library","homepage":null,"github":"https://github.com/MichaelKim0407/flake8-use-fstring","docs":null,"changelog":null,"pypi":"https://pypi.org/project/flake8-use-fstring/","npm":null,"openapi_spec":null,"status_page":null,"smithery":null,"categories":["testing"],"base_url":null,"auth_type":null,"provenance":{"verified_status":null,"verified_at":null,"last_verified":"2026-04-15","next_check":"2026-07-14","install_tag":null}}