Issue 32464: raise NotImplemented vs return NotImplemented (original) (raw)
Issue32464
Created on 2017-12-31 11:44 by thatiparthy, last changed 2022-04-11 14:58 by admin. 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) * ![]() |
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) * ![]() |
Date: 2018-05-29 19:11 | |
Seems this issue isn't a bug and can be closed. |
History | |||
---|---|---|---|
Date | User | Action | Args |
2022-04-11 14:58:56 | admin | set | github: 76645 |
2018-05-29 19:11:30 | yselivanov | set | status: open -> closednosy: + yselivanovmessages: + resolution: not a bugstage: resolved |
2017-12-31 11:57:10 | mark.dickinson | set | nosy: + mark.dickinsonmessages: + |
2017-12-31 11:44:32 | thatiparthy | create |