mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-29 21:35:58 +00:00
[red-knot] support typing.Union
in type annotations (#14499)
Fix #14498 ## Summary This PR adds `typing.Union` support ## Test Plan I created new tests in mdtest. --------- Co-authored-by: Carl Meyer <carl@astral.sh>
This commit is contained in:
parent
3c52d2d1bd
commit
aecdb8c144
4 changed files with 78 additions and 1 deletions
|
@ -1809,6 +1809,8 @@ pub enum KnownInstanceType<'db> {
|
|||
Literal,
|
||||
/// The symbol `typing.Optional` (which can also be found as `typing_extensions.Optional`)
|
||||
Optional,
|
||||
/// The symbol `typing.Union` (which can also be found as `typing_extensions.Union`)
|
||||
Union,
|
||||
/// A single instance of `typing.TypeVar`
|
||||
TypeVar(TypeVarInstance<'db>),
|
||||
// TODO: fill this enum out with more special forms, etc.
|
||||
|
@ -1819,6 +1821,7 @@ impl<'db> KnownInstanceType<'db> {
|
|||
match self {
|
||||
KnownInstanceType::Literal => "Literal",
|
||||
KnownInstanceType::Optional => "Optional",
|
||||
KnownInstanceType::Union => "Union",
|
||||
KnownInstanceType::TypeVar(_) => "TypeVar",
|
||||
}
|
||||
}
|
||||
|
@ -1826,7 +1829,9 @@ impl<'db> KnownInstanceType<'db> {
|
|||
/// Evaluate the known instance in boolean context
|
||||
pub const fn bool(self) -> Truthiness {
|
||||
match self {
|
||||
Self::Literal | Self::Optional | Self::TypeVar(_) => Truthiness::AlwaysTrue,
|
||||
Self::Literal | Self::Optional | Self::TypeVar(_) | Self::Union => {
|
||||
Truthiness::AlwaysTrue
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1835,6 +1840,7 @@ impl<'db> KnownInstanceType<'db> {
|
|||
match self {
|
||||
Self::Literal => "typing.Literal",
|
||||
Self::Optional => "typing.Optional",
|
||||
Self::Union => "typing.Union",
|
||||
Self::TypeVar(typevar) => typevar.name(db),
|
||||
}
|
||||
}
|
||||
|
@ -1844,6 +1850,7 @@ impl<'db> KnownInstanceType<'db> {
|
|||
match self {
|
||||
Self::Literal => KnownClass::SpecialForm,
|
||||
Self::Optional => KnownClass::SpecialForm,
|
||||
Self::Union => KnownClass::SpecialForm,
|
||||
Self::TypeVar(_) => KnownClass::TypeVar,
|
||||
}
|
||||
}
|
||||
|
@ -1864,6 +1871,7 @@ impl<'db> KnownInstanceType<'db> {
|
|||
match (module.name().as_str(), instance_name) {
|
||||
("typing" | "typing_extensions", "Literal") => Some(Self::Literal),
|
||||
("typing" | "typing_extensions", "Optional") => Some(Self::Optional),
|
||||
("typing" | "typing_extensions", "Union") => Some(Self::Union),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4567,6 +4567,13 @@ impl<'db> TypeInferenceBuilder<'db> {
|
|||
let param_type = self.infer_type_expression(parameters);
|
||||
UnionType::from_elements(self.db, [param_type, Type::none(self.db)])
|
||||
}
|
||||
KnownInstanceType::Union => match parameters {
|
||||
ast::Expr::Tuple(t) => UnionType::from_elements(
|
||||
self.db,
|
||||
t.iter().map(|elt| self.infer_type_expression(elt)),
|
||||
),
|
||||
_ => self.infer_type_expression(parameters),
|
||||
},
|
||||
KnownInstanceType::TypeVar(_) => Type::Todo,
|
||||
}
|
||||
}
|
||||
|
|
|
@ -373,6 +373,7 @@ impl<'db> ClassBase<'db> {
|
|||
Type::KnownInstance(known_instance) => match known_instance {
|
||||
KnownInstanceType::TypeVar(_)
|
||||
| KnownInstanceType::Literal
|
||||
| KnownInstanceType::Union
|
||||
| KnownInstanceType::Optional => None,
|
||||
},
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue