Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

raise NotImplemented vs return NotImplemented #76645

Closed
srinivasreddy mannequin opened this issue Dec 31, 2017 · 3 comments
Closed

raise NotImplemented vs return NotImplemented #76645

srinivasreddy mannequin opened this issue Dec 31, 2017 · 3 comments
Labels
3.7 (EOL) end of life 3.8 only security fixes performance Performance or resource usage

Comments

@srinivasreddy
Copy link
Mannequin

srinivasreddy mannequin commented Dec 31, 2017

BPO 32464
Nosy @mdickinson, @1st1, @srinivasreddy

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2018-05-29.19:11:30.796>
created_at = <Date 2017-12-31.11:44:32.019>
labels = ['invalid', '3.7', '3.8', 'performance']
title = 'raise NotImplemented vs return NotImplemented'
updated_at = <Date 2018-05-29.19:11:30.795>
user = 'https://github.com/srinivasreddy'

bugs.python.org fields:

activity = <Date 2018-05-29.19:11:30.795>
actor = 'yselivanov'
assignee = 'none'
closed = True
closed_date = <Date 2018-05-29.19:11:30.796>
closer = 'yselivanov'
components = []
creation = <Date 2017-12-31.11:44:32.019>
creator = 'thatiparthy'
dependencies = []
files = []
hgrepos = []
issue_num = 32464
keywords = []
message_count = 3.0
messages = ['309277', '309278', '318070']
nosy_count = 3.0
nosy_names = ['mark.dickinson', 'yselivanov', 'thatiparthy']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'resource usage'
url = 'https://bugs.python.org/issue32464'
versions = ['Python 3.7', 'Python 3.8']

@srinivasreddy
Copy link
Mannequin Author

srinivasreddy mannequin commented Dec 31, 2017

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?

@srinivasreddy srinivasreddy mannequin added 3.7 (EOL) end of life 3.8 only security fixes performance Performance or resource usage labels Dec 31, 2017
@mdickinson
Copy link
Member

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)

@1st1
Copy link
Member

1st1 commented May 29, 2018

Seems this issue isn't a bug and can be closed.

@1st1 1st1 closed this as completed May 29, 2018
@1st1 1st1 added the invalid label May 29, 2018
@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.7 (EOL) end of life 3.8 only security fixes performance Performance or resource usage
Projects
None yet
Development

No branches or pull requests

2 participants