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: |
Sorry, something went wrong.
|
Yes, converting integers to float is not always reversible. This is expected behavior. |
Sorry, something went wrong.
|
For |
Sorry, something went wrong.
|
The only cases where |
Sorry, something went wrong.
mdickinson
left a comment
There was a problem hiding this comment.
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?)
Sorry, something went wrong.
|
Ah, sorry; apparently we are converting to exact int already. (Yay!) |
Sorry, something went wrong.
|
Hmm: bug found during manual testing: |
Sorry, something went wrong.
mdickinson
left a comment
There was a problem hiding this comment.
Updating my approval to "request changes": there's a missing error check in PyNumber_Float.
Sorry, something went wrong.
|
When you're done making the requested changes, leave the comment: |
Sorry, something went wrong.
Constructors of
int,floatandcomplexwill now fall back to__index__if corresponding special methods__int__,__float__and__complex__are not defined.https://bugs.python.org/issue20092