Skip to content

int.__round__(None) is not supported #120080

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

Closed
JelleZijlstra opened this issue Jun 4, 2024 · 7 comments
Closed

int.__round__(None) is not supported #120080

JelleZijlstra opened this issue Jun 4, 2024 · 7 comments
Labels
3.14 bugs and security fixes easy type-bug An unexpected behavior, bug, or error

Comments

@JelleZijlstra
Copy link
Member

JelleZijlstra commented Jun 4, 2024

Bug report

Bug description:

The __round__ method on floats accepts None as an explicit argument, but the one on int does not:

>>> (1.0).__round__(None)
1
>>> (1).__round__(None)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'NoneType' object cannot be interpreted as an integer

This goes against the general principle that any operation that works on a float should also work on an int.

(Note that both round(x) and round(x, None) work on both floats and ints; this issue is entirely about direct calls to .__round__().)

CPython versions tested on:

3.12, CPython main branch

Operating systems tested on:

No response

Linked PRs

@JelleZijlstra JelleZijlstra added type-bug An unexpected behavior, bug, or error easy 3.12 only security fixes 3.13 bugs and security fixes 3.14 bugs and security fixes and removed 3.12 only security fixes 3.13 bugs and security fixes labels Jun 4, 2024
@serhiy-storchaka
Copy link
Member

I do not think that there is a bug. The operation that works on a float and on an int is round(x, None), the rest is an implementation detail. You do not expect that int.__add__ works on a float, aren't?

vstinner pushed a commit that referenced this issue Jun 7, 2024
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Jun 7, 2024
…of the ``int.__round__`` (pythonGH-120088)

(cherry picked from commit 57ad769)

Co-authored-by: Kirill Podoprigora <[email protected]>
Co-authored-by: Nikita Sobolev <[email protected]>
@vstinner
Copy link
Member

vstinner commented Jun 7, 2024

Fixed by commit: 57ad769. See my rationale for the change: #120088 (comment).

@vstinner vstinner closed this as completed Jun 7, 2024
@serhiy-storchaka
Copy link
Member

Note that Decimal.__round__ does not accept None. And perhaps many third-party numeric types do not accept it. Implementing this for int.__round__ sends a false signal that this is required. It won't be long before somebody open an issue about Decimal.__round__, and then authors of third-party libraries will be under pressure to implement this unneeded feature in their code. This opened a can of worms, and we do not know what long term consequences will be.

Not everything that can be implemented should be implemented, and I believe that this is such a case.

@vstinner
Copy link
Member

vstinner commented Jun 7, 2024

My motivation to merge the PR was to avoid <unrepresentable> in the function signature. This marker causes issues. See for example: https://discuss.python.org/t/ac-null-defaults-prevent-correct-signatures-lets-add-inspect-unrepresentable-to-fix-this/30753

@serhiy-storchaka
Copy link
Member

#73536 is the general solution of this problem.

@vstinner
Copy link
Member

vstinner commented Jun 7, 2024

#73536 is the general solution of this problem.

Ok, but this issue is open since 2017 and is not fixed yet.

IMO accepting None is just a harmless way to fix the issue.

@serhiy-storchaka
Copy link
Member

Few months ago I finally created a draft (#117671). I hope this will be included in 3.14.

Eclips4 added a commit to Eclips4/cpython that referenced this issue Jun 10, 2024
mrahtz pushed a commit to mrahtz/cpython that referenced this issue Jun 30, 2024
noahbkim pushed a commit to hudson-trading/cpython that referenced this issue Jul 11, 2024
noahbkim pushed a commit to hudson-trading/cpython that referenced this issue Jul 11, 2024
estyxx pushed a commit to estyxx/cpython that referenced this issue Jul 17, 2024
estyxx pushed a commit to estyxx/cpython that referenced this issue Jul 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.14 bugs and security fixes easy type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

3 participants