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
bpo-20092. Use __index__ in constructors of int, float and complex. #13108
bpo-20092. Use __index__ in constructors of int, float and complex. #13108
Conversation
I'm not sure Here the example given by Steven D'Aprano:
|
Yes, converting integers to float is not always reversible.
This is expected behavior. |
For |
The only cases where |
LGTM: code looks good, and I agree that this is a desirable change.
The one fly in the ointment is that index
can return something that's not an exact int, which means that there's now another code-path by which int
can return something that's not of exact type int
.
>>> class A:
... def __index__(self): return True
...
>>> int(A())
<stdin>:1: DeprecationWarning: __index__ 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.
1
I'd love to finally turn that DeprecationWarning
into an error, but fear we've run out of time for that for 3.8, and we may want to wait for 3.9 anyway to make sure that everyone's had a chance to react to that warning. (Do you remember which version it got introduced in?)
Ah, sorry; apparently we are converting to exact int already. (Yay!) |
Hmm: bug found during manual testing:
|
When you're done making the requested changes, leave the comment: |
Constructors of
int
,float
andcomplex
will now fall back to__index__
if corresponding special methods__int__
,__float__
and__complex__
are not defined.https://bugs.python.org/issue20092