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.

Author mark.dickinson
Recipients Arfrever, alex, barry, docs@python, eric.snow, ethan.furman, mark.dickinson, mjacob, ncoghlan, python-dev, rhettinger, serhiy.storchaka, vstinner
Date 2020-05-28.08:41:58
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1590655318.62.0.0309690680564.issue17576@roundup.psfhosted.org>
In-reply-to
Content
[Serhiy]

> * Undeprecate accepting __index__ and __int__ returning instances of int sublasses. There is no difference from the side of using int and index(), but it can simplify user implementations of __index__ and __int__.

I'm not sure about this. Thinking about the bigger picture, we have a similar deprecation in place for __float__ returning an instance of a float subclass. That one I'd like to keep (and probably make an error for 3.10).

A problem I've run into in Real Code (TM) is needing to convert something float-like to a float, using the same mechanisms that (for example) something like `math.sqrt` uses.

One option is to call "float", but that requires explicitly excluding str, bytes and bytearray, which feels ugly and not very future-proof.

So the code ends up calling __float__. But because __float__ can return an instance of a float subclass, it then still needs some way to convert the return value to an actual float. And that's surprisingly tricky.

So I really *do* want to see the ability of __float__ to return a non-float eventually removed.

Similarly for __int__, there's no easy Python-side way to mimic the effect of calling __int__, followed by converting to an exact int. We have to:

1. Do an explicit check for non-numbers (str, bytes, bytearray)
2. Call int

Or:

1. Call __int__
2. Convert an instance of a possible subclass of int to something of exact type int. I don't know how to do this cleanly in general in Python, and end up resorting to evil tricks like adding `0`.

Deprecating allowing __int__ to return a non-int helps here, because it lets me simply call __int__.

I care much more about the __float__ case than the __int__ case, because the "right way" to duck-type integers is to use __index__ rather than __int__, and for __index__ we have operator.index as a solution.

But it would seem odd to have the rule in place for __float__ but not for __int__ and __index__.


The other way to solve my problem would be to provide an operator module function (operator.as_float?) that does a duck-typed conversion of an arbitrary Python object to a float.
History
Date User Action Args
2020-05-28 08:41:58mark.dickinsonsetrecipients: + mark.dickinson, barry, rhettinger, ncoghlan, vstinner, Arfrever, alex, docs@python, ethan.furman, python-dev, eric.snow, serhiy.storchaka, mjacob
2020-05-28 08:41:58mark.dickinsonsetmessageid: <1590655318.62.0.0309690680564.issue17576@roundup.psfhosted.org>
2020-05-28 08:41:58mark.dickinsonlinkissue17576 messages
2020-05-28 08:41:58mark.dickinsoncreate