Skip to content

[red-knot] support narrowing on bool(E) #14547

@carljm

Description

@carljm

We currently support type narrowing on various kinds of expressions in a test clause, e.g.

x = 1 if flag() else None

reveal_type(x)  # revealed: Literal[1] | None
if x is not None:
    reveal_type(x)  # revealed: Literal[1]

This task is to add support for the case where the test expression is wrapped in an explicit call to bool(...), e.g. if bool(x is not None):. This is not a common pattern, but pyright supports it, it's always correct, and it's not hard to implement, so there's little reason not to support it.

For any test expression E which we would draw narrowing conclusions from, we should draw the same conclusions from the test expression bool(E).

Metadata

Metadata

Assignees

No one assigned

    Labels

    tyMulti-file analysis & type inference

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions