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
28 changes: 28 additions & 0 deletions crates/red_knot_python_semantic/resources/mdtest/type_of/basic.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,31 @@ reveal_type(f()) # revealed: @Todo(unsupported type[X] special form)
```py path=a/b.py
class C: ...
```

## Union of classes

```py
class BasicUser: ...
class ProUser: ...

class A:
class B:
class C: ...

def get_user() -> type[BasicUser | ProUser | A.B.C]:
return BasicUser

# revealed: type[BasicUser] | type[ProUser] | type[C]
reveal_type(get_user())
```

## Illegal parameters

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

# error: [invalid-type-form]
def get_user() -> type[A, B]:
return A
```
23 changes: 22 additions & 1 deletion crates/red_knot_python_semantic/src/types/infer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4663,7 +4663,28 @@ impl<'db> TypeInferenceBuilder<'db> {
todo_type!("unsupported type[X] special form")
}
}
// TODO: unions, subscripts, etc.
ast::Expr::BinOp(binary) if binary.op == ast::Operator::BitOr => {
let union_ty = UnionType::from_elements(
self.db,
[
self.infer_subclass_of_type_expression(&binary.left),
self.infer_subclass_of_type_expression(&binary.right),
],
);
self.store_expression_type(slice, union_ty);

union_ty
}
ast::Expr::Tuple(_) => {
self.infer_type_expression(slice);
self.diagnostics.add(
slice.into(),
"invalid-type-form",
format_args!("type[...] must have exactly one type argument"),
);
Type::Unknown
}
// TODO: subscripts, etc.
_ => {
self.infer_type_expression(slice);
todo_type!("unsupported type[X] special form")
Expand Down
Loading