Skip to content

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

Closed
@rvanlaar

Description

@rvanlaar

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

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions