This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: __round__ doesn't behave well with return NotImplemented
Type: enhancement Stage: resolved
Components: Interpreter Core Versions: Python 3.9
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Hameer Abbasi, eric.smith, mark.dickinson, serhiy.storchaka
Priority: normal Keywords:

Created on 2020-03-30 15:31 by Hameer Abbasi, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (4)
msg365323 - (view) Author: Hameer Abbasi (Hameer Abbasi) Date: 2020-03-30 15:31
Minimal reproducer:

>>> class A:
...     def __round__(self):
...         return NotImplemented
>>> round(A())

Should give a TypeError.

This can be useful when deciding, for example, if a given a.dtype implements round based on the dtype
msg365327 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2020-03-30 15:43
NotImplemented is documented as only being used for binary operators:

Changing that seems like a pretty large issue. I'd suggest discussing this on python-ideas.

Maybe better for your situation is to actually raise NotImplementedError yourself, or ValaueError if that's what you really want to see.

Changing version numbers, since this feature could only go in to 3.9.
msg365331 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2020-03-30 16:18
If you want to get a TypeError, raise a TypeError.
msg365375 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2020-03-31 10:46
Agreed with Eric and Serhiy. This seems to be a misunderstanding about the scope of NotImplemented.

@Hameer Abbasi: if you have an example of a problem that would be solved by this behaviour change, and which can't be solved simply by raising TypeError or another exception within __round__, feel free to open a discussion on the python-ideas mailing list.
Date User Action Args
2022-04-11 14:59:28adminsetgithub: 84298
2020-03-31 10:46:40mark.dickinsonsetstatus: open -> closed
resolution: not a bug
messages: + msg365375

stage: resolved
2020-03-30 17:12:30mark.dickinsonsetnosy: + mark.dickinson
2020-03-30 16:18:34serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg365331
2020-03-30 15:43:44eric.smithsetversions: + Python 3.9, - Python 3.5, Python 3.6, Python 3.7, Python 3.8
nosy: + eric.smith

messages: + msg365327

type: behavior -> enhancement
2020-03-30 15:33:38Hameer Abbasisettype: behavior
2020-03-30 15:31:16Hameer Abbasicreate