Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Comment thread
AlexWaygood marked this conversation as resolved.
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Call `type[...]`

## Single class

### Trivial constructor

```py
class C: ...

def _(subclass_of_c: type[C]):
reveal_type(subclass_of_c()) # revealed: C
```

### Non-trivial constructor

```py
class C:
def __init__(self, x: int): ...

def _(subclass_of_c: type[C]):
reveal_type(subclass_of_c(1)) # revealed: C

# TODO: Those should all be errors
reveal_type(subclass_of_c("a")) # revealed: C
reveal_type(subclass_of_c()) # revealed: C
reveal_type(subclass_of_c(1, 2)) # revealed: C
Comment on lines +23 to +26
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These will be fixed by #16512

```

## Dynamic base

```py
from typing import Any
from knot_extensions import Unknown

def _(subclass_of_any: type[Any], subclass_of_unknown: type[Unknown]):
reveal_type(subclass_of_any()) # revealed: Any
reveal_type(subclass_of_any("any", "args", 1, 2)) # revealed: Any
reveal_type(subclass_of_unknown()) # revealed: Unknown
reveal_type(subclass_of_unknown("any", "args", 1, 2)) # revealed: Unknown
```

## Unions of classes

```py
class A: ...
class B: ...

def _(subclass_of_ab: type[A | B]):
reveal_type(subclass_of_ab()) # revealed: A | B
```
7 changes: 7 additions & 0 deletions crates/red_knot_python_semantic/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2678,6 +2678,13 @@ impl<'db> Type<'db> {
)))
}

Type::SubclassOf(subclass_of_type) => match subclass_of_type.subclass_of() {
ClassBase::Dynamic(dynamic_type) => Ok(CallOutcome::Single(
CallBinding::from_return_type(Type::Dynamic(dynamic_type)),
)),
ClassBase::Class(class) => Type::class_literal(class).try_call(db, arguments),
},

instance_ty @ Type::Instance(_) => {
instance_ty
.try_call_dunder(db, "__call__", arguments)
Expand Down
Loading