Static checker that number of arguments to python logging matches number of placeholders

22 Views Asked by At

I oversee a large Python (FastAPI) application, and analyzing our recent bugs, one of the most common type of runtime bug that we have seen are in error-handling code, where we have something like:

...
except SomeError e:
    logging.error("There was an error with updating the settings for user %s. Update was %s", user.uuid)

The error that we trigger is:

TypeError: not enough arguments for format string
Call stack:
  File "<stdin>", line 1, in <module>
Message: 'There was an error with updating the settings for user %s. Update was %s'
Arguments: (3,)

Which of course makes sense: we are supplying the wrong number of arguments to the logging.error function. What I'm wondering is if there is any way for us to detect this using static tools before our code is ever run. Realistically, we're not going to have unit tests cover 100% of our code, especially not error-handling code, and this type of mistake is very tough to spot on code reviews.

It feels like exactly the sort of problem that static analysis tools should be able to catch, similar to supplying the wrong number of arguments to a function.

Is there any such tool?

0

There are 0 best solutions below