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?
Once a tuple is created, you cannot change its values. Tuples are unchangeable, or immutable as it also is called.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With