[red-knot] Assignability of class literals to Callables (#17704)

## Summary

Subtyping was already modeled, but assignability also needs an explicit
branch. Removes 921 ecosystem false positives.

## Test Plan

New Markdown tests.
This commit is contained in:
David Peter 2025-04-29 15:04:22 +02:00 committed by GitHub
parent ca4fdf452d
commit 79f8473e51
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 19 additions and 0 deletions

View File

@ -529,6 +529,18 @@ c: Callable[[Any], str] = A().f
c: Callable[[Any], str] = A().g
```
### Class literal types
```py
from typing import Any, Callable
c: Callable[[str], Any] = str
c: Callable[[str], Any] = int
# error: [invalid-assignment]
c: Callable[[str], Any] = object
```
### Overloads
`overloaded.pyi`:

View File

@ -1503,6 +1503,13 @@ impl<'db> Type<'db> {
}
}
(Type::ClassLiteral(class_literal), Type::Callable(_)) => {
if let Some(callable) = class_literal.into_callable(db) {
return callable.is_assignable_to(db, target);
}
false
}
(Type::FunctionLiteral(self_function_literal), Type::Callable(_)) => {
self_function_literal
.into_callable_type(db)