Rule Definition
Python Exceptions accept all arguments passed to them being stored in the .args attribute as a tuple. Various libraries and parts of Python rely on this behaviour. To prevent source of errors and unexpected behaviour, avoid inconsistent initialization when subclassing a new exception by passing the same number of arguments to the parent __init__ call.
Remediation
Initialize parent exception class with the same number of parameters.
Violation Code Sample
>>> class CustomError(Exception):
>>> def __init__(self, message, status):
>>> self.message = message
>>> self.status = status
Fixed Code Sample
>>> class CustomError(Exception):
>>> def __init__(self, message, status):
>>> self.message = message
>>> self.status = status
>>> # parent exception class is initialized with same number of parameters
>>> super().__init__(message, status)
Reference
D. Beazley and B.K. Jones, Python Cookbook, 3rd Ed. (O'Reilly Media, Sebastopol, CA, May 2013), p. 579
Related Technologies
Technical Criterion
PCI-DSS4-Requirement-6.2.4 - Software engineering techniques or other methods are defined and in use by software development personnel to prevent or mitigate common software attacks and related vulnerabilities
About CAST Appmarq
CAST Appmarq is by far the biggest repository of data about real IT systems. It's built on thousands of analyzed applications, made of 35 different technologies, by over 300 business organizations across major verticals. It provides IT Leaders with factual key analytics to let them know if their applications are on track.