mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-29 21:35:58 +00:00

This is the first use of the new `lint()` reporter. I somewhat skipped a step here and also modified the actual diagnostic message itself. The snapshots should tell the story. We couldn't do this before because we had no way of differentiating between "message for the diagnostic as a whole" and "message for a specific code annotation." Now we can, so we can write more precise messages based on the assumption that users are also seeing the code snippet. The downside here is that the actual message text can become quite vague in the absence of the code snippet. This occurs, for example, with concise diagnostic formatting. It's unclear if we should do anything about it. I don't really see a way to make it better that doesn't involve creating diagnostics with messages for each mode, which I think would be a major PITA. The upside is that this code gets a bit simpler, and we very specifically avoid doing extra work if this specific lint is disabled.
1.4 KiB
1.4 KiB
typing.Callable
from typing import Callable
def _(c: Callable[[], int]):
reveal_type(c()) # revealed: int
def _(c: Callable[[int, str], int]):
reveal_type(c(1, "a")) # revealed: int
# error: [invalid-argument-type] "Argument to this function is incorrect: Expected `int`, found `Literal["a"]`"
# error: [invalid-argument-type] "Argument to this function is incorrect: Expected `str`, found `Literal[1]`"
reveal_type(c("a", 1)) # revealed: int
The Callable
annotation can only be used to describe positional-only parameters.
def _(c: Callable[[int, str], None]):
# error: [unknown-argument] "Argument `a` does not match any known parameter"
# error: [unknown-argument] "Argument `b` does not match any known parameter"
# error: [missing-argument] "No arguments provided for required parameters 1, 2"
reveal_type(c(a=1, b="b")) # revealed: None
If the annotation uses a gradual form (...
) for the parameter list, then it can accept any kind of
parameter with any type.
def _(c: Callable[..., int]):
reveal_type(c()) # revealed: int
reveal_type(c(1)) # revealed: int
reveal_type(c(1, "str", False, a=[1, 2], b=(3, 4))) # revealed: int
An invalid Callable
form can accept any parameters and will return Unknown
.
# error: [invalid-type-form]
def _(c: Callable[42, str]):
reveal_type(c()) # revealed: Unknown