mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-28 21:05:08 +00:00

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.
1.1 KiB
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)