Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 711
Matthew Salvatore Viglione Avatar asked Jun 23 '20 20:06

Matthew Salvatore Viglione


People also ask

How do I change the tuple format in Python?

Once a tuple is created, you cannot change its values. Tuples are unchangeable, or immutable as it also is called.

How do I print a tuple string?

Approach 1: using simple for loopCreate an empty string and using a for loop iterate through the elements of the tuple and keep on adding each element to the empty string. In this way, the tuple is converted to a string.


1 Answers

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.

like image 116
Matthew Salvatore Viglione Avatar answered Oct 26 '22 18:10

Matthew Salvatore Viglione