PR #3108 (originally) tried to make float a subclass of numbers.Real. Two method implementations were missing, __floor__ and __ceil__ (cpython). These do exist for int and Decimal etc., but @srittau noticed that they actually do not exist for float itself, therefore they cannot be added to float's stubs, and float cannot be made to conform to numbers.Real.
I did some digging and found that abstractly numbers.Real really just wants math.floor() and math.ceil() to work. And in cpython, these two functions work something like this (using floor ):
- If the argument is a
float, do the operation directly in C code.
- Else, if the argument has a
__floor__ method, use it.
- Else, if the argument has a
__float__ method, use it and go to (1).
As it happen, numbers.Real does require __float__() to be implemented. So as a workaround for this issue, I proposed removing __floor__ and __ceil__ from typeshed's numbers.Real stub, since the __float__ fallback can be relied on instead.
Another view is that this is a cpython issue and should be fixed there.
And another view is that numbers is a lost cause for static typing and should just be ignored or replaced with something better (like protocols).
PR #3108 (originally) tried to make
floata subclass ofnumbers.Real. Two method implementations were missing,__floor__and__ceil__(cpython). These do exist forintandDecimaletc., but @srittau noticed that they actually do not exist forfloatitself, therefore they cannot be added tofloat's stubs, andfloatcannot be made to conform tonumbers.Real.I did some digging and found that abstractly
numbers.Realreally just wantsmath.floor()andmath.ceil()to work. And in cpython, these two functions work something like this (usingfloor):float, do the operation directly in C code.__floor__method, use it.__float__method, use it and go to (1).As it happen,
numbers.Realdoes require__float__()to be implemented. So as a workaround for this issue, I proposed removing__floor__and__ceil__from typeshed'snumbers.Realstub, since the__float__fallback can be relied on instead.Another view is that this is a cpython issue and should be fixed there.
And another view is that
numbersis a lost cause for static typing and should just be ignored or replaced with something better (like protocols).