-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Expression does not infer float as result type #18133
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
Comments
You can set As for the error itself, I believe it is caused by the type declaration of >>> (-1.0)**0.5
(6.123233995736766e-17+1j) There is no way for typeshed to tell if the result would be float or complex because there is no way to distinguish negative and positive floats so it was decided to annotate the method as returning |
To add, you can make this type check without a return math.sqrt(sum((x - mean) ** 2 for x in values) / n) That also has the benefit of being slightly faster: $ python3.13 -m timeit -n100 'for x in range(100000): x**0.5'
100 loops, best of 5: 3.34 msec per loop
$ python3.13 -m timeit -n100 -s 'import math' 'for x in range(100000): math.sqrt(x)'
100 loops, best of 5: 2.25 msec per loop Alternatively, you could use If your actual use case is computing the (population) standard deviation, you might also consider using |
@hamdanal thank you for the explanation. I am aware of the global flags to disable checks. |
Bug Report
The following return line reports an error although the expression returns the correct type.
To Reproduce
Expected Behavior
Emit no error
Actual Behavior
The following return line reports an error
Returning Any from function declared to return "float"
Additional note
Appending
# type: ignore
is not a solution, because this will interfere with pyright:Pyright correctly accepts the return type and will correctly report an error
Unnecessary "# type: ignore" comment
Your Environment
The text was updated successfully, but these errors were encountered: