9

With a simple custom exception class defined as:

class MyError(Exception):
    pass

And this call:

foo = 'Some more info'
raise MyError("%s: there was an error", foo)

pylint gives:

Exception arguments suggest string formatting might be intended pylint(raising-format-tuple)

What does this message mean?

Salvatore
  • 10,815
  • 4
  • 31
  • 69

1 Answers1

18

Any one of these fixes the message, depending on your version of Python.

foo = 'Some more info'
raise MyError("%s: there was an error" % foo )
raise MyError("{}: there was an error".format(foo))
raise MyError(f"{foo}: there was an error")

The message is triggered when pylint sees the %s tag in the string with no following arguments. Instead of raising an exception with the string "Some more info: there was an error" you'll get an exception with a tuple, where the first element is ": there was an error" and the second is the contents of foo. This is likely not the intended effect.

In the code I was using, there was extensive use of logging, and I suspect the original author confused the exception raising with lazy logging.

Salvatore
  • 10,815
  • 4
  • 31
  • 69
  • 7
    Your comment "_I suspect the original author confused the exception raising with lazy logging_." exactly nailed it !! – Rabarberski Oct 23 '20 at 07:43