Raising a tuple¶
ID: py/raises-tupleKind: problemSecurity severity: Severity: warningPrecision: very-highTags: - maintainabilityQuery suites: - python-security-and-quality.qls
Click to see the query in the CodeQL repository
In Python 2, if a tuple is raised then all elements but the first are ignored and only the first part is raised. If the first element is itself a tuple, then the first element of that is used and so on. This unlikely to be the intended effect and will most likely indicate some sort of error.
It is important to note that the exception inraiseException,message isnot a tuple, whereas the exception inex=Exception,message;raiseexis a tuple.
In Python 3, raising a tuple is an error.
Recommendation¶
Given that all but the first element of the tuple is ignored, the tuple should be replaced with its first element in order to improve the clarity of the code. If the subsequent parts of the tuple were intended to form the message, then they should be passed as an argument when creating the exception.
Example¶
In the following example the intended error message is mistakenly used to form a tuple.
defraise_tuple():ex=Exception,"Important diagnostic information"raiseex
This can be fixed, either by using the message to create the exception or using the message in the raise statement, as shown below.
deffixed_raise_tuple1():ex=Exception("Important diagnostic information")raiseexdeffixed_raise_tuple2():raiseException,"Important diagnostic information"
References¶
Python Language Reference:Exceptions.
Python Tutorial:Handling Exceptions.