Skip to content

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

Closed
rvanlaar opened this issue Dec 4, 2019 · 8 comments
Closed

Update documentation to reflect that bool is a subtype of int #8069

rvanlaar opened this issue Dec 4, 2019 · 8 comments

Comments

@rvanlaar
Copy link
Contributor

rvanlaar commented Dec 4, 2019

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:

def example(x: int) -> int:
    if x := x > 0:
        return x
    return 0

The expectation is that this function will return only positive integers or 0.
What happens is that it will return bool for positive integers and 0 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

@JukkaL JukkaL changed the title Update documentation to reflect that bool is a subtype of int. Update documentation to reflect that bool is a subtype of int Dec 4, 2019
@orcutt989
Copy link

orcutt989 commented Jan 10, 2020

@JukkaL
Copy link
Collaborator

JukkaL commented Jan 13, 2020

@sobolevn
Copy link
Member

sobolevn commented Jan 19, 2020

@JukkaL would you consider --strict-bool option to treat bool like not-an-int a good idea?

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 mypy.

@FooTaGe
Copy link
Contributor

FooTaGe commented Jan 30, 2020

@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 Dict[str, int] is the correct type for Dict[Any, Any]

Also its seems python3 uses ints and bools interchangeably in the std functions:

Operations and built-in functions that have a Boolean result always return 0 or False for false and 1 or True for true, unless otherwise stated. (Important exception: the Boolean operations or and and always return one of their operands.)

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

@rvanlaar
Copy link
Contributor Author

@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 --strict-bool option lands into MyPy.

@JelleZijlstra
Copy link
Member

See psf/black#762 for a concrete example of a bug that the type checker didn't catch because bool is a subclass of int.

@JukkaL JukkaL closed this as completed in 1f9d87e Jan 31, 2020
@sobolevn
Copy link
Member

@JukkaL am I correct, that --strict-bool won't be happening? Do we need to still open a new issue for it?

@JukkaL
Copy link
Collaborator

JukkaL commented Feb 4, 2020

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants