NotImplemented is not an Exception¶
ID: py/raise-not-implemented Kind: problem Severity: warning Precision: very-high Tags: - reliability - maintainability Query suites: - python-security-and-quality.qls
Click to see the query in the CodeQL repository
NotImplemented is not an Exception, but is often mistakenly used in place of
raise NotImplemented or
raise NotImplemented() will raise a
raise NotImplemented is used to mark code that is genuinely never called, this mistake is benign. However, should it be called, then a
TypeError will be raised rather than the expected
NotImplemented, which might make debugging the issue difficult.
The correct use of
NotImplemented is to implement binary operators. Code that is not intended to be called should raise
Replace uses of
In the example below, the method
wrong will incorrectly raise a
TypeError when called. The method
right will raise a
class Abstract(object): def wrong(self): # Will raise a TypeError raise NotImplemented() def right(self): raise NotImplementedError()
Python Language Reference: The NotImplementedError exception.
Python Language Reference: Emulating numeric types.