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#
Searches for strings that look like they were meant to be f-strings, but are missing an f
prefix.
Why is this bad?#
Expressions inside curly braces are only evaluated if the string has an f
prefix.
Details#
There are many possible string literals which are not meant to be f-strings despite containing f-string-like syntax. As such, this lint ignores all strings where one of the following conditions applies:
- The string is a standalone expression. For example, the rule ignores all docstrings.
- The string is part of a function call with argument names that match at least one variable
(for example:
format("Message: {value}", value="Hello World")
) - The string (or a parent expression of the string) 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.
- The string is part of a function call that is known to expect a template string rather than an
evaluated f-string: for example, a [
logging
][logging] call, a [gettext
][gettext] call, or a [fastAPI
path].
Example#
Use instead:
[logging
][logging]: https://docs.python.org/3/howto/logging-cookbook.html#using-particular-formatting-styles-throughout-your-application
[gettext
][gettext]: https://docs.python.org/3/library/gettext.html