Skip to content

Commit dccb03d

Browse files
authored
[ty] Avoid panicking on overloaded Callable type context (#24661)
Resolves astral-sh/ty#3278.
1 parent 61f9a0a commit dccb03d

2 files changed

Lines changed: 16 additions & 6 deletions

File tree

crates/ty_python_semantic/resources/mdtest/bidirectional.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,16 @@ reveal_type(f7) # revealed: (int, /) -> None
505505
# TODO: This should reveal `(*args: int, *, x=1) -> None` once we support `Unpack`.
506506
f8: Callable[[*tuple[int, ...], int], None] = lambda *args, x=1: None
507507
reveal_type(f8) # revealed: (*args, *, x=1) -> None
508+
509+
def _(x: bool):
510+
signatures = {
511+
"upper": str.upper,
512+
"lower": str.lower,
513+
"title": str.title,
514+
}
515+
516+
# revealed: (x) -> Unknown
517+
f = signatures.get("", reveal_type(lambda x: x))
508518
```
509519

510520
We do not currently account for type annotations present later in the scope:

crates/ty_python_semantic/src/types/infer/builder.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6447,16 +6447,16 @@ impl<'db, 'ast> TypeInferenceBuilder<'db, 'ast> {
64476447

64486448
let callable_tcx = if let Some(tcx) = tcx.annotation
64496449
// TODO: We could perform multi-inference here if there are multiple `Callable` annotations
6450-
// in the union.
6450+
// in the union/intersection.
64516451
&& let Some(callable) = tcx
64526452
.filter_union(self.db(), Type::is_callable_type)
64536453
.as_callable()
64546454
{
6455-
let [signature] = callable.signatures(self.db()).overloads.as_slice() else {
6456-
panic!("`Callable` type annotations cannot be overloaded");
6457-
};
6458-
6459-
Some(signature)
6455+
match callable.signatures(self.db()).overloads.as_slice() {
6456+
[signature] => Some(signature),
6457+
// TODO: We could similarly perform multi-inference here if there are multiple overloads.
6458+
_ => None,
6459+
}
64606460
} else {
64616461
None
64626462
};

0 commit comments

Comments
 (0)