-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
__ceil__, __floor__ and numbers.Real #3195
Comments
If I understand correctly, the issue boils down to the problem that |
The reason it works at runtime is that |
I guess math.ceil() and math.floor() special-case float. Can you file a bug in bugs.python.org about the missing methods? They should be added for completeness. |
|
@bluetech this bug is considered fixed, but I am still having error. |
These methods need to be added to typeshed for Python 3.9+. |
@srittau I tried with 3.6, 3.7 and 3.8 using floor. It is not accepting float as return. |
For versions <3.9, either type checkers needs to implement special logic (unlikely to happen, though), or you need to |
You added Will you consider "lying" and change the condition to >=3? The rationale for doing this is explained in the original issue:
|
I still maintain that it's better to special-case this is type checkers. In my experience, lying in the stubs has always proved problematic. While it solves one problem, it usually creates others. This is especially the case, since nowadays type stubs are used by more than type checkers. |
That's understandable - thanks for considering! |
I'm curious if there are specific problems you anticipate from the lie? All I can see for now are false negatives if someone does The other major users of type stubs (afaik) are IDEs. I guess suggesting This problem will go away by itself in another couple years, so not the biggest deal. And I will definitely acknowledge that my last attempt to lie in typeshed crashed and burned :-) But still curious what you foresee! |
To be honest, I can't make a strong case in this particular instance. But I remember an argument before to include overridden methods in sub-classes, even if the signature is not changed, if the sub-class implements a particular method. I don't remember the specifics. I also remember several cases where we deviated from the implementation just to revert the changes later. These are the things that make me wary. And I find it more appropriate for type checkers to work around the quirks of the Python language than trying to work around those quirks in stubs. |
PR #3108 (originally) tried to make
float
a subclass ofnumbers.Real
. Two method implementations were missing,__floor__
and__ceil__
(cpython). These do exist forint
andDecimal
etc., but @srittau noticed that they actually do not exist forfloat
itself, therefore they cannot be added tofloat
's stubs, andfloat
cannot be made to conform tonumbers.Real
.I did some digging and found that abstractly
numbers.Real
really 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.Real
does require__float__()
to be implemented. So as a workaround for this issue, I proposed removing__floor__
and__ceil__
from typeshed'snumbers.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).The text was updated successfully, but these errors were encountered: