[red-knot] Infer type of class constructor call expression (#13171)

This tiny PR implements the following type inference: the type of
`Foo(...)` will be `Foo`.

---------

Co-authored-by: Carl Meyer <carl@astral.sh>
This commit is contained in:
Dylan 2024-08-30 18:48:06 -05:00 committed by GitHub
parent 828871dc5c
commit 3ceedf76b8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 20 additions and 2 deletions

View file

@ -328,8 +328,8 @@ impl<'db> Type<'db> {
match self {
Type::Function(function_type) => function_type.returns(db).or(Some(Type::Unknown)),
// TODO: handle class constructors
Type::Class(_class_ty) => Some(Type::Unknown),
// TODO annotated return type on `__new__` or metaclass `__call__`
Type::Class(class) => Some(Type::Instance(*class)),
// TODO: handle classes which implement the Callable protocol
Type::Instance(_instance_ty) => Some(Type::Unknown),

View file

@ -2834,6 +2834,24 @@ mod tests {
Ok(())
}
#[test]
fn class_constructor_call_expression() -> anyhow::Result<()> {
let mut db = setup_db();
db.write_dedented(
"src/a.py",
"
class Foo: ...
x = Foo()
",
)?;
assert_public_ty(&db, "src/a.py", "x", "Foo");
Ok(())
}
#[test]
fn resolve_union() -> anyhow::Result<()> {
let mut db = setup_db();