-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Update documentation to reflect that bool is a subtype of int #8069
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
@rvanlaar @JukkaL Are we thinking it should go in https://github.com/python/mypy/blob/master/docs/source/dynamic_typing.rst ? |
This should probably go somewhere in https://github.com/python/mypy/blob/master/docs/source/builtin_types.rst. |
@JukkaL would you consider Sometimes I see code like: def some(a: int): ...
some(x > 1) And it drives me crazy! Currently AFAIK there's no way to find this issue with |
@sobolevn I don't see any runtime problems that can occur from from receiving a bool when expecting an int. Since bool is essentially an int. only the opposite seems necessary def some(a: bool): ...
some(x and 4) Like Also its seems python3 uses ints and bools interchangeably in the std functions:
from https://docs.python.org/3/library/stdtypes.html#typesnumeric But it can force a more explicit form of writing. If for that reason it is still worth it? maybe. First timer here, open for feedback-> PR #8346 decided to go with subclass |
@FooTaGe It doesn't break runtime, but returning an int, 0 or 1, when the expectation is an actual integer gives weird runtime problems. I had written a function that accidentally returned the boolean value of an integer. Which did result in weird bugs. It was of course a programming error. However it was exactly the type of error I would expect a typing system to catch. I sincerely hope that a |
See psf/black#762 for a concrete example of a bug that the type checker didn't catch because |
@JukkaL am I correct, that |
@sobolevn Feel free to open a separate issue about a strictness option. This will be more likely to be implemented once/if mypy supports disabling and enabling individual error codes. We could then just define a new error code for this check that isn't enabled by default. The benefit would be that we wouldn't need to add a yet another command-line option just for this feature. |
Bool is a subtype of int.
The goals of this issue is to document this in a good way in the mypy documentation.
A short example:
This can cause problems when a function is expected to return an int, but in reality returns a bool.
Take this code for example:
The expectation is that this function will return only positive integers or 0.
What happens is that it will return
bool
for positive integers and0
for non positive ints.This is a weird edge case for python3. See also this blog:
https://medium.com/@timb07/python-bool-wat-ab0e28d879c7
As per: #8053
The text was updated successfully, but these errors were encountered: