CodeQL documentation

Raising a tuple

ID: py/raises-tuple
Kind: problem
Security severity: 
Severity: warning
Precision: very-high
   - maintainability
Query 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 in raise Exception, message is not a tuple, whereas the exception in ex = Exception, message; raise ex is a tuple.

In Python 3, raising a tuple is an error.


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.


In the following example the intended error message is mistakenly used to form a tuple.

def raise_tuple():
    ex = Exception, "Important diagnostic information"
    raise ex

This can be fixed, either by using the message to create the exception or using the message in the raise statement, as shown below.

def fixed_raise_tuple1():
    ex = Exception("Important diagnostic information")
    raise ex

def fixed_raise_tuple2():
    raise Exception, "Important diagnostic information"


  • © GitHub, Inc.
  • Terms
  • Privacy