Registry / testing / flake8-use-fstring

flake8-use-fstring

JSON →
library1.4pypypiunverified

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.

pip install flake8-use-fstring
INSTALL
IMPORT
SIG · FLAKE8-USE-FSTRING
F
flake8-use-fstring
testingpythonv1.4
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

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
flake8 --version
Debug
Known issues
gotchaThe `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.
fix
Enable with `flake8 --enable-extensions=FS003` or in your `.flake8` config file under `enable-extensions = FS003`.
affects: All versions
gotchaThe 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.
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.
affects: All versions
gotchaAs 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.
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.
affects: >=1.4
gotchaWhile `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)`).
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.
affects: All versions
compatibilityVersion 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.
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`.
affects: <1.2
Errors
Common errors & fixes
FS001: % formatting is used.
The code uses the old-style `%` operator for string formatting, which flake8-use-fstring flags as a non-f-string usage.
fix
Rewrite the string formatting using an f-string. Example: `print(f'Hello, {name}!')` instead of `print('Hello, %s!' % name)`.
FS002: .format formatting is used.
The code uses the `.format()` method for string formatting, which flake8-use-fstring flags as a non-f-string usage.
fix
Rewrite the string formatting using an f-string. Example: `print(f'Hello, {name}!')` instead of `print('Hello, {}'.format(name))`.
FS003: f-string missing prefix (ignored by default).
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.
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.
FS001: % formatting is used.
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).
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.
FS002: .format formatting is used.
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).
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.
Upgrade
Version history
1.4latest on PyPI · released Jul 27, 2022
Audit
Dependencies
flake8requiredThis is a plugin for Flake8 and requires Flake8 to function.
Agent activity
2 hits · last 30 days
node
2
Resources
flake8-use-fstring — pip install flake8-use-fstring · libregistry