[issue17576] PyNumber_Index() is not int-subclass friendly (or operator.index() docos lie)

Serhiy Storchaka report at bugs.python.org
Thu May 28 04:10:59 EDT 2020


Serhiy Storchaka <storchaka+cpython at gmail.com> added the comment:

The current status:

* Decimal and Fraction are no longer automatically converted to int when pass to functions implemented in C. PyLong_AsLong() etc no longer call __int__. (see issue36048 and issue37999)
* operator.index() and PyNumber_Index() always return an instance of exact type int. (see issue40792)
* int() and PyNumber_Long() always return an instance of exact type int. (see issue26984)
* __index__ is used as a fallback if __int__ is not defined. (see issue20092)

But:

* __index__ and __int__ are not called for int subclasses in operator.index() and int() (also in the C API PyNumber_Index(), PyNumber_Long(), PyLong_AsLong(), etc).
* Instances of int sublasses are accepted as result of __index__ and __int__ (but it is deprecated).
* The Python implementation of operator.index() differs from the C implementation in many ways. (see issue18712)

What I prefer as solutions of the remaining issues:

* It is good to not call __index__ and __int__ for int subclasses. __index__ and __int__ were   designed for converting non-integers to int. There are no good use cases for overriding __index__ and __int__ in int subclasses, and calling them is just a waste of time. We should just document this behavior.

* 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__.

* Either sync the pure Python implementation of operator.index() with the C implementation or get rid of Python implementation of the operator module at all.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue17576>
_______________________________________


More information about the Python-bugs-list mailing list