Skip to content

MichaelKim0407/flake8-use-fstring

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

61 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

flake8-use-fstring

Release Status PyPI package Supported Python versions

  • master (release) Build Status Coverage Status
  • develop (main) Build Status Coverage Status

Jump-start into modern Python by forcing yourself to use f-strings.

Works only with python>=3.6 since it was when f-strings were introduced.

Installation

pip install flake8-use-fstring

Reported Errors

  • FS001: % formatting is used.

  • FS002: .format formatting is used.

  • FS003: f-string missing prefix (ignored by default).

Available Configurations

--percent-greedy and --format-greedy

This plugin checks each python statement (logical line) and see if % or .format is used.

Since flake8 is only a code style checker and not a type checker, it's impossible to tell for sure if the value before % or .format is a string.

Thus, greedy level is introduced to control when the plugin should report errors.

  • Level 0 (default): only report error if the value before % or .format is a string literal.

  • Level 1: report error if a string literal appears before % or .format anywhere in the statement.

  • Level 2: report any usage of % or .format.

If the value immediately before % or .format is not a string, using level 1 or 2 may result in false positives. See tests/example.py for examples. Thus level 0 is the default level. However, for most projects it should be reasonable to use greedy level 2 with confidence.

To set greedy levels, set --percent-greedy=<level> and --format-greedy=<level> in the command line, or set percent-greedy=<level> and format-greedy=<level> in the .flake8 config file.

--enable-extensions=FS003 and --fstring-ignore-format

This plugin can also check for strings that appear to be f-strings but don't have the f prefix.

FS003 will be reported if a string:

  • is not prefixed with f
  • contains at least one pair of {}
  • is not prefixed with r (because r-strings are often regexes that would contain {})

Due to the potential for false positives, this check (FS003) is disabled by default. To enable this check, add the --enable-extensions=FS003 command line option, or set enable-extensions=FS003 in the .flake8 config file.

NOTE: If you use the --ignore option of flake8, the default ignore list will be overwritten. See --ignore vs --extend-ignore.

The missing prefix check normally ignores strings that are using % or .format formatting. To check those strings as well, add the --fstring-ignore-format command line option, or set fstring-ignore-format=True in the .flake8 config file.

Additional Notes

Byte literals

Byte literals (strings that are prefixed with a literal b) will not be checked since Python does not support f-strings for them (as of 3.9).