ruff/crates/red_knot_python_semantic/resources/mdtest/call/builtins.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

Calling builtins

bool with incorrect arguments

class NotBool:
    __bool__ = None

# error: [too-many-positional-arguments] "Too many positional arguments to class `bool`: expected 1, got 2"
bool(1, 2)

# TODO: We should emit an `unsupported-bool-conversion` error here because the argument doesn't implement `__bool__` correctly.
bool(NotBool())

Calls to type()

A single-argument call to type() returns an object that has the argument's meta-type. (This is tested more extensively in crates/red_knot_python_semantic/resources/mdtest/attributes.md, alongside the tests for the __class__ attribute.)

reveal_type(type(1))  # revealed: Literal[int]

But a three-argument call to type creates a dynamic instance of the type class:

reveal_type(type("Foo", (), {}))  # revealed: type

Other numbers of arguments are invalid

# error: [no-matching-overload] "No overload of class `type` matches arguments"
type("Foo", ())

# error: [no-matching-overload] "No overload of class `type` matches arguments"
type("Foo", (), {}, weird_other_arg=42)