RaisingNotImplemented¶
ID: py/raise-not-implementedKind: problemSecurity severity: Severity: warningPrecision: very-highTags: - quality - reliability - error-handlingQuery suites: - python-security-and-quality.qls
Click to see the query in the CodeQL repository
The constantNotImplemented is not anException, but is often confused forNotImplementedError. If it is used as an exception, such as inraiseNotImplemented orraiseNotImplemented("message"), aTypeError will be raised rather than the expectedNotImplemented. This may make debugging more difficult.
NotImplemented should only be used as a special return value for implementing special methods such as__lt__. Code that is not intended to be called should raiseNotImplementedError.
Recommendation¶
If aNotImplementedError is intended to be raised, replace the use ofNotImplemented with that. IfNotImplemented is intended to be returned rather than raised, replace theraise withreturnNotImplemented.
Example¶
In the following example, the methodwrong will incorrectly raise aTypeError when called. The methodright will raise aNotImplementedError.
classAbstract(object):defwrong(self):# Will raise a TypeErrorraiseNotImplemented()defright(self):raiseNotImplementedError()
References¶
Python Language Reference:The NotImplementedError exception.
Python Language Reference:The NotImplemented constant.
Python Language Reference:Emulating numeric types.