Understand type[A | B] special form in annotations (#14830)
Some checks are pending
CI / Determine changes (push) Waiting to run
CI / cargo fmt (push) Waiting to run
CI / cargo clippy (push) Blocked by required conditions
CI / cargo test (linux) (push) Blocked by required conditions
CI / cargo test (linux, release) (push) Blocked by required conditions
CI / cargo test (windows) (push) Blocked by required conditions
CI / cargo test (wasm) (push) Blocked by required conditions
CI / cargo build (release) (push) Waiting to run
CI / cargo build (msrv) (push) Blocked by required conditions
CI / cargo fuzz build (push) Blocked by required conditions
CI / fuzz parser (push) Blocked by required conditions
CI / test scripts (push) Blocked by required conditions
CI / ecosystem (push) Blocked by required conditions
CI / cargo shear (push) Blocked by required conditions
CI / python package (push) Waiting to run
CI / pre-commit (push) Waiting to run
CI / mkdocs (push) Waiting to run
CI / formatter instabilities and black similarity (push) Blocked by required conditions
CI / test ruff-lsp (push) Blocked by required conditions
CI / benchmarks (push) Blocked by required conditions

resolves https://github.com/astral-sh/ruff/issues/14703

I decided to use recursion to get the type, so if anything is added to
the single element inference it will be applied for the union.
Also added this
[change](https://github.com/astral-sh/ruff/issues/14703#issuecomment-2510286217)
in this PR since it was easy.

---------

Co-authored-by: Carl Meyer <carl@astral.sh>
This commit is contained in:
Shaygan Hooshyari 2024-12-07 18:34:50 +01:00 committed by GitHub
parent d34013425f
commit 269e47be96
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 50 additions and 1 deletions

View file

@ -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
```

View file

@ -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")