mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-22 11:25:26 +00:00
[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:
parent
828871dc5c
commit
3ceedf76b8
2 changed files with 20 additions and 2 deletions
|
@ -328,8 +328,8 @@ impl<'db> Type<'db> {
|
||||||
match self {
|
match self {
|
||||||
Type::Function(function_type) => function_type.returns(db).or(Some(Type::Unknown)),
|
Type::Function(function_type) => function_type.returns(db).or(Some(Type::Unknown)),
|
||||||
|
|
||||||
// TODO: handle class constructors
|
// TODO annotated return type on `__new__` or metaclass `__call__`
|
||||||
Type::Class(_class_ty) => Some(Type::Unknown),
|
Type::Class(class) => Some(Type::Instance(*class)),
|
||||||
|
|
||||||
// TODO: handle classes which implement the Callable protocol
|
// TODO: handle classes which implement the Callable protocol
|
||||||
Type::Instance(_instance_ty) => Some(Type::Unknown),
|
Type::Instance(_instance_ty) => Some(Type::Unknown),
|
||||||
|
|
|
@ -2834,6 +2834,24 @@ mod tests {
|
||||||
Ok(())
|
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]
|
#[test]
|
||||||
fn resolve_union() -> anyhow::Result<()> {
|
fn resolve_union() -> anyhow::Result<()> {
|
||||||
let mut db = setup_db();
|
let mut db = setup_db();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue