classification
Title: raise NotImplemented vs return NotImplemented
Type: resource usage Stage: resolved
Components: Versions: Python 3.8, Python 3.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: mark.dickinson, thatiparthy, yselivanov
Priority: normal Keywords:

Created on 2017-12-31 11:44 by thatiparthy, last changed 2018-05-29 19:11 by yselivanov. This issue is now closed.

Messages (3)
msg309277 - (view) Author: Srinivas Reddy Thatiparthy(శ్రీనివాస్ రెడ్డి తాటిపర్తి) (thatiparthy) * Date: 2017-12-31 11:44
I ran these queries on cpython repo.

➜  cpython git:(master) ✗ grep -r . -e return  --include=\*.py  | grep NotImplemented | wc -l
196

➜  cpython git:(master) ✗ grep -r . -e raise   --include=\*.py  | grep NotImplemented | wc -l
295

I have always used raise NotImplemented or raise NotImplementedError. But when does it make sense to return NotImplemented?
msg309278 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2017-12-31 11:57
Here's the documentation, which I think explains all this clearly: briefly, `return NotImplemented` and `raise NotImplementedError` are the normal usages. `raise NotImplemented` doesn't make sense, and shouldn't be used: it'll end up raising a `TypeError`, sine `NotImplemented` is neither a subclass nor an instance of BaseException.

NotImplemented: https://docs.python.org/3.7/library/constants.html#NotImplemented

NotImplementedError:
https://docs.python.org/3.7/library/exceptions.html#NotImplementedError

Almost all of the grep results you're seeing do follow the expected patterns (either `return NotImplemented` or `raise NotImplementedError`, but a quick grep does show some questionable uses of `raise NotImplemented` in the standard library:

    MacBook-Pro:cpython mdickinson$ grep -r . -e raise   --include=\*.py  | grep "\bNotImplemented\b"
    ./Lib/asyncio/transports.py:    The implementation here raises NotImplemented for every method
    ./Lib/idlelib/debugger_r.py:        raise NotImplemented("dict_keys not public or pickleable")
    ./Lib/ssl.py:        raise NotImplemented("Can't dup() %s instances" %

The other way around also turns up an odd-looking `return NotImplementedError` (along with a false positive):

    MacBook-Pro:cpython mdickinson$ grep -r . -e return   --include=\*.py  | grep "\bNotImplementedError\b"
    ./Lib/ctypes/test/test_simplesubclasses.py:            return NotImplementedError
    ./Lib/test/test_asyncio/test_transports.py:        self.assertRaises(NotImplementedError, transport.get_returncode)
msg318070 - (view) Author: Yury Selivanov (yselivanov) * (Python committer) Date: 2018-05-29 19:11
Seems this issue isn't a bug and can be closed.
History
Date User Action Args
2018-05-29 19:11:30yselivanovsetstatus: open -> closed

nosy: + yselivanov
messages: + msg318070

resolution: not a bug
stage: resolved
2017-12-31 11:57:10mark.dickinsonsetnosy: + mark.dickinson
messages: + msg309278
2017-12-31 11:44:32thatiparthycreate