bpo-40792: Make PyNumber_Index() always returning exact int. by serhiy-storchaka · Pull Request #20443 · python/cpython

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM in principle. (Modulo failing CI tests.)

_PyNumber_Index does feel a bit like a premature optimization. Though since the optimization is already present in the original behaviour of PyNumber_Index, I suppose it would be better described as a delayed pessimization.

I suspect we don't have very many cases where an integer-expecting function needs to be fast when passed an instance of an integer subclass - there just aren't that many interesting integer subclasses around, besides bool. (NumPy no longer has any.) I'd probably discourage use of _PyNumber_Index in new code, for simplicity, but it makes sense to use it as a replacement for PyNumber_Index in existing code.