You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: python/ql/src/Exceptions/NotImplementedIsNotAnException.qhelp
+10-9Lines changed: 10 additions & 9 deletions
Original file line number
Diff line number
Diff line change
@@ -4,25 +4,25 @@
4
4
<qhelp>
5
5
<overview>
6
6
7
-
<p><code>NotImplemented</code> is not an Exception, but is often mistakenly used in place of <code>NotImplementedError</code>.
8
-
Executing <code>raise NotImplemented</code> or <code>raise NotImplemented()</code> will raise a <code>TypeError</code>.
9
-
When <code>raise NotImplemented</code> is used to mark code that is genuinely never called, this mistake is benign.
10
-
11
-
However, should it be called, then a <code>TypeError</code> will be raised rather than the expected <code>NotImplemented</code>,
12
-
which might make debugging the issue difficult.
7
+
<p>
8
+
The constant <code>NotImplemented</code> is not an <code>Exception</code>, but is often confused for <code>NotImplementedError</code>.
9
+
If it is used as an exception, such as in <code>raise NotImplemented</code> or <code>raise NotImplemented("message")</code>,
10
+
a <code>TypeError</code> will be raised rather than the expected <code>NotImplemented</code>. This may make debugging more difficult.
13
11
</p>
14
12
15
-
<p>The correct use of <code>NotImplemented</code> is to implement binary operators.
13
+
<p><code>NotImplemented</code> should only be used as a special return value for implementing special methods such as <code>__lt__</code>.
16
14
Code that is not intended to be called should raise <code>NotImplementedError</code>.</p>
17
15
18
16
</overview>
19
17
<recommendation>
20
-
<p>Replace uses of <code>NotImplemented</code> with <code>NotImplementedError</code>.</p>
18
+
<p>If a <code>NotImplementedError</code> is intended to be raised, replace the use of <code>NotImplemented</code>
19
+
with that. If <code>NotImplemented</code> is intended to be returned rather than raised, replace the <code>raise</code> with <code>return NotImplemented</code>.
20
+
</p>
21
21
</recommendation>
22
22
<example>
23
23
24
24
<p>
25
-
In the example below, the method <code>wrong</code> will incorrectly raise a <code>TypeError</code> when called.
25
+
In the following example, the method <code>wrong</code> will incorrectly raise a <code>TypeError</code> when called.
26
26
The method <code>right</code> will raise a <code>NotImplementedError</code>.
27
27
</p>
28
28
@@ -34,6 +34,7 @@ The method <code>right</code> will raise a <code>NotImplementedError</code>.
34
34
<references>
35
35
36
36
<li>Python Language Reference: <ahref="https://docs.python.org/library/exceptions.html#NotImplementedError">The NotImplementedError exception</a>.</li>
37
+
<li>Python Language Reference: <ahref="https://docs.python.org/3/library/constants.html#NotImplemented">The NotImplemented constant</a>.</li>
37
38
<li>Python Language Reference: <ahref="https://docs.python.org/3/reference/datamodel.html#emulating-numeric-types">Emulating numeric types</a>.</li>
0 commit comments