Skip to content

Commit 20bd9f0

Browse files
committed
Clarify NotImplemented vs NotImplementedError. Initial patch by Emmanuel Barry. Closes issue 27242.
1 parent a9391a4 commit 20bd9f0

2 files changed

Lines changed: 37 additions & 10 deletions

File tree

Doc/library/constants.rst

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,22 @@ A small number of constants live in the built-in namespace. They are:
3333
(e.g. :meth:`__imul__`, :meth:`__iand__`, etc.) for the same purpose.
3434
Its truth value is true.
3535

36-
.. note::
36+
.. note::
37+
38+
When a binary (or in-place) method returns ``NotImplemented`` the
39+
interpreter will try the reflected operation on the other type (or some
40+
other fallback, depending on the operator). If all attempts return
41+
``NotImplemented``, the interpreter will raise an appropriate exception.
42+
Incorrectly returning ``NotImplemented`` will result in a misleading
43+
error message or the ``NotImplemented`` value being returned to Python code.
44+
45+
See :ref:`implementing-the-arithmetic-operations` for examples.
3746

38-
When ``NotImplemented`` is returned, the interpreter will then try the
39-
reflected operation on the other type, or some other fallback, depending
40-
on the operator. If all attempted operations return ``NotImplemented``, the
41-
interpreter will raise an appropriate exception.
47+
.. note::
4248

43-
See
44-
:ref:`implementing-the-arithmetic-operations`
45-
for more details.
49+
``NotImplentedError`` and ``NotImplemented`` are not interchangeable,
50+
even though they have similar names and purposes.
51+
See :exc:`NotImplementedError` for details on when to use it.
4652

4753

4854
.. data:: Ellipsis

Doc/library/exceptions.rst

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,9 +228,21 @@ The following exceptions are the exceptions that are usually raised.
228228
.. exception:: NotImplementedError
229229

230230
This exception is derived from :exc:`RuntimeError`. In user defined base
231-
classes, abstract methods should raise this exception when they require derived
232-
classes to override the method.
231+
classes, abstract methods should raise this exception when they require
232+
derived classes to override the method, or while the class is being
233+
developed to indicate that the real implementation still needs to be added.
233234

235+
.. note::
236+
237+
It should not be used to indicate that an operater or method is not
238+
meant to be supported at all -- in that case either leave the operator /
239+
method undefined or, if a subclass, set it to :data:`None`.
240+
241+
.. note::
242+
243+
``NotImplementedError`` and ``NotImplemented`` are not interchangeable,
244+
even though they have similar names and purposes. See
245+
:data:`NotImplemented` for details on when to use it.
234246

235247
.. exception:: OSError([arg])
236248
OSError(errno, strerror[, filename[, winerror[, filename2]]])
@@ -436,6 +448,15 @@ The following exceptions are the exceptions that are usually raised.
436448
Raised when an operation or function is applied to an object of inappropriate
437449
type. The associated value is a string giving details about the type mismatch.
438450

451+
This exception may be raised by user code to indicate that an attempted
452+
operation on an object is not supported, and is not meant to be. If an object
453+
is meant to support a given operation but has not yet provided an
454+
implementation, :exc:`NotImplementedError` is the proper exception to raise.
455+
456+
Passing arguments of the wrong type (e.g. passing a :class:`list` when an
457+
:class:`int` is expected) should result in a :exc:`TypeError`, but passing
458+
arguments with the wrong value (e.g. a number outside expected boundaries)
459+
should result in a :exc:`ValueError`.
439460

440461
.. exception:: UnboundLocalError
441462

0 commit comments

Comments
 (0)