ruff/crates/red_knot_python_semantic/resources/mdtest/narrow/bool-call.md
Douglas Creager e17cd350b6
[red-knot] Support multiple overloads when binding parameters at call sites (#16568)
This updates the `Signature` and `CallBinding` machinery to support
multiple overloads for a callable. This is currently only used for
`KnownFunction`s that we special-case in our type inference code. It
does **_not_** yet update the semantic index builder to handle
`@overload` decorators and construct a multi-signature `Overloads`
instance for real Python functions.

While I was here, I updated many of the `try_call` special cases to use
signatures (possibly overloaded ones now) and `bind_call` to check
parameter lists. We still need some of the mutator methods on
`OverloadBinding` for the special cases where we need to update return
types based on some Rust code.
2025-03-11 15:08:17 -04:00

1.1 KiB

Narrowing for bool(..) checks

def _(flag: bool):

    x = 1 if flag else None

    # valid invocation, positive
    reveal_type(x)  # revealed: Literal[1] | None
    if bool(x is not None):
        reveal_type(x)  # revealed: Literal[1]

    # valid invocation, negative
    reveal_type(x)  # revealed: Literal[1] | None
    if not bool(x is not None):
        reveal_type(x)  # revealed: None

    # no args/narrowing
    reveal_type(x)  # revealed: Literal[1] | None
    if not bool():
        reveal_type(x)  # revealed: Literal[1] | None

    # invalid invocation, too many positional args
    reveal_type(x)  # revealed: Literal[1] | None
    # error: [too-many-positional-arguments] "Too many positional arguments to class `bool`: expected 1, got 2"
    if bool(x is not None, 5):
        reveal_type(x)  # revealed: Literal[1] | None

    # invalid invocation, too many kwargs
    reveal_type(x)  # revealed: Literal[1] | None
    # error: [unknown-argument] "Argument `y` does not match any known parameter of class `bool`"
    if bool(x is not None, y=5):
        reveal_type(x)  # revealed: Literal[1] | None