A % format and its values do not match

tx0/python:percent-formatdefaultwarningbug

What it finds

"%s and %s" % (a,): formatting with % raises an exception when a constant format and the values written next to it do not fit together. The values are known when they are a tuple display, a dict display or a single literal; a name or a call could hold a tuple of any length, so then only the format itself is checked.

Reported are conversions Python does not know, a % at the end of the format, a tuple whose length differs from the number of conversions, a single value for a format that needs another number, a tuple or literal for a format that names its values, and names missing from a dict display. Formats that mix named and unnamed conversions are left alone, since a dict can then also be a value.

From the rule's source, percent_format.rs.

Example from the rule's tests
def messages(a, b, values, mapping):    return [        "%s and %s" % (a,),tx0 reports: the format needs 2 values, but 1 is given        "%s" % (a, b),tx0 reports: the format needs 1 value, but 2 are given        "%d%%" % (a, b),tx0 reports: the format needs 1 value, but 2 are given        "%*d" % (a,),tx0 reports: the format needs 2 values, but 1 is given        "done" % (a,),tx0 reports: the format has no conversions, but 1 is given        "%s %s" % a.name,        "%s %s" % 5,tx0 reports: the format needs 2 values, but 1 is given        "%s %s" % {"a": 1},tx0 reports: the format needs 2 values, but 1 dict is given        "%(a)s" % (a,),tx0 reports: the format names its values, so it needs a mapping        "%(a)s %(b)s" % {"a": 1},tx0 reports: the format uses the key b, which the dict does not have        "%q" % a,tx0 reports: the format has a conversion %q that Python does not know        "50%" % a,tx0 reports: the format ends with a single %        b"%b" % (a,),        "%b" % (a,),tx0 reports: conversion %b        "%s and %s" % (a, b),        "%s" % (a),        "%s %s" % values,        "%s %s" % (*values,),        "%(a)s" % mapping,        "%(a)s %(b)s" % {"a": 1, "b": 2, "c": 3},        "%(a)s" % {"a": 1, **mapping},        "%s" % {"a": 1},        "%s %(a)s" % {"a": 1},        "done" % {"a": 1},        f"{a} %s" % (b,),        "%-5.2f %#x %+d %ld" % (a, b, a, b),    ]
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

Turn it on, off or suppress it

The rule runs in every tier. To turn it off for a project, set its level in tx0.toml:

tx0.toml
[rules]
"tx0/python:percent-format" = "off"

To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:

# tx0: ignore[tx0/python:percent-format] reason="..."

tx0 explain tx0/python:percent-format shows the same in the terminal. Found a false positive? Report it.