missing-f-string-syntax (RUF027)#
Fix is always available.
This rule is unstable and in preview. The --preview
flag is required for use.
What it does#
Checks for strings that contain f-string syntax but are not f-strings.
Why is this bad?#
An f-string missing an f
at the beginning won't format anything, and instead
treat the interpolation syntax as literal.
Since there are many possible string literals which contain syntax similar to f-strings yet are not intended to be, this lint will disqualify any literal that satisfies any of the following conditions:
- The string literal is a standalone expression. For example, a docstring.
- The literal is part of a function call with argument names that match at least one variable (for example:
format("Message: {value}", value = "Hello World")
) - The literal (or a parent expression of the literal) has a direct method call on it (for example:
"{value}".format(...)
) - The string has no
{...}
expression sections, or uses invalid f-string syntax. - The string references variables that are not in scope, or it doesn't capture variables at all.
- Any format specifiers in the potential f-string are invalid.
Example#
Use instead: