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-fstringNo compatibility data collected yet for this library.
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.
Enable with `flake8 --enable-extensions=FS003` or in your `.flake8` config file under `enable-extensions = FS003`.
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.
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.
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.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`.
Rewrite the string formatting using an f-string. Example: `print(f'Hello, {name}!')` instead of `print('Hello, %s!' % name)`.Rewrite the string formatting using an f-string. Example: `print(f'Hello, {name}!')` instead of `print('Hello, {}'.format(name))`.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.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.
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.