mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-29 13:25:17 +00:00
Support inference for PEP 604 union annotations (#13964)
## Summary Supports return type inference for, e.g., `def f() -> int | None:`. --------- Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
This commit is contained in:
parent
c593ccb529
commit
6f52d573ef
3 changed files with 31 additions and 10 deletions
|
@ -3494,14 +3494,20 @@ impl<'db> TypeInferenceBuilder<'db> {
|
|||
Type::Todo
|
||||
}
|
||||
|
||||
// TODO PEP-604 unions
|
||||
ast::Expr::BinOp(binary) => {
|
||||
self.infer_binary_expression(binary);
|
||||
#[allow(clippy::single_match_else)]
|
||||
match binary.op {
|
||||
// PEP-604 unions are okay
|
||||
ast::Operator::BitOr => Type::Todo,
|
||||
// PEP-604 unions are okay, e.g., `int | str`
|
||||
ast::Operator::BitOr => {
|
||||
let left_ty = self.infer_type_expression(&binary.left);
|
||||
let right_ty = self.infer_type_expression(&binary.right);
|
||||
UnionType::from_elements(self.db, [left_ty, right_ty])
|
||||
}
|
||||
// anything else is an invalid annotation:
|
||||
_ => Type::Unknown,
|
||||
_ => {
|
||||
self.infer_binary_expression(binary);
|
||||
Type::Unknown
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue