Skip to content

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:

  1. The string literal is a standalone expression. For example, a docstring.
  2. 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"))
  3. The literal (or a parent expression of the literal) has a direct method call on it (for example: "{value}".format(...))
  4. The string has no {...} expression sections, or uses invalid f-string syntax.
  5. The string references variables that are not in scope, or it doesn't capture variables at all.
  6. Any format specifiers in the potential f-string are invalid.

Example#

name = "Sarah"
dayofweek = "Tuesday"
msg = "Hello {name}! It is {dayofweek} today!"

Use instead:

name = "Sarah"
dayofweek = "Tuesday"
msg = f"Hello {name}! It is {dayofweek} today!"