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
int() can return not exact int instance #71171
Comments
The int constructor can return an instance of int subclass. >>> class BadTrunc:
... def __trunc__(self):
... return True
...
>>> int(BadTrunc())
True When __int__ returns non-exact int, at least a warning is emitted: >>> class BadInt:
... def __int__(self):
... return True
...
>>> int(BadInt())
__main__:1: DeprecationWarning: __int__ returned non-int (type bool). The ability to return an instance of a strict subclass of int is deprecated, and may be removed in a future version of Python.
True The constructor of int subclass always return an instance of correct type: >>> class IntSubclass(int):
... pass
...
>>> type(IntSubclass(BadTrunc()))
<class '__main__.IntSubclass'>
>>> type(IntSubclass(BadInt()))
__main__:1: DeprecationWarning: __int__ returned non-int (type bool). The ability to return an instance of a strict subclass of int is deprecated, and may be removed in a future version of Python.
<class '__main__.IntSubclass'> I don't know if it is worth to deprecate __trunc__ returning non-exact int, since this special method is used in math.trunc(). But I think that the int constructor should convert its result to exact int. If some preparatory period is needed, it can first start to emit FutureWarning. |
Agreed. See also bpo-17576 and previous discussions on python-dev (thread started in March, but most of the messages in April):
I don't have strong feelings about |
Proposed patch makes int() always returning exact int. |
Could you please make a review Mark? |
Sorry, Serhiy. I missed this. I've got some time off coming up, so I plan to look at this in the next few days. |
The patch LGTM. I'm still in two minds about whether |
New changeset 81f229262921 by Serhiy Storchaka in branch 'default': |
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:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: