The documentation for NotImplemented says:
Changed in version 3.9: Evaluating
NotImplemented
in a boolean context is deprecated. While it currently evaluates as true, it will emit aDeprecationWarning
. It will raise aTypeError
in a future version of Python.
And indeed that’s what happens at runtime:
% ./python.exe -c 'bool(NotImplemented)'
<string>:1: DeprecationWarning: NotImplemented should not be used in a boolean context
This warning was introduced in Python 3.9 (PR, issue), four years ago. That seems like enough time to consider whether we want to go through with this deprecation.
Personally I don’t have a strong opinion, except that I don’t want the documentation to claim that something will happen when it isn’t happening. I’ll summarize the arguments for and against that show up in the issue.
Arguments in favor of making bool(NotImplemented)
raise:
- Calling
bool()
on NotImplemented frequently indicates a bug, such as callingnot self.__eq__(other)
.
Arguments against:
- All other Python builtins are usable in a boolean context. NotImplemented would be the exception.
- Some (possibly questionable) coding patterns start raising errors, such as
list(filter(None.__ne__, L))
to filter None out of a list.