Skip to content

__ceil__, __floor__ and numbers.Real #3195

Closed
@bluetech

Description

@bluetech

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 ):

  1. If the argument is a float, do the operation directly in C code.
  2. Else, if the argument has a __floor__ method, use it.
  3. 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).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions