mirror of
https://github.com/astral-sh/ruff.git
synced 2025-11-20 04:29:47 +00:00
[ty] Improve invalid-argument-type diagnostics where a union type was provided (#21044)
This commit is contained in:
parent
01695513ce
commit
dab3d4e917
3 changed files with 189 additions and 0 deletions
|
|
@ -3519,6 +3519,36 @@ impl<'db> BindingError<'db> {
|
|||
"Expected `{expected_ty_display}`, found `{provided_ty_display}`"
|
||||
));
|
||||
|
||||
if let Type::Union(union) = provided_ty {
|
||||
let union_elements = union.elements(context.db());
|
||||
let invalid_elements: Vec<Type<'db>> = union
|
||||
.elements(context.db())
|
||||
.iter()
|
||||
.filter(|element| !element.is_assignable_to(context.db(), *expected_ty))
|
||||
.copied()
|
||||
.collect();
|
||||
let first_invalid_element = invalid_elements[0].display(context.db());
|
||||
if invalid_elements.len() < union_elements.len() {
|
||||
match &invalid_elements[1..] {
|
||||
[] => diag.info(format_args!(
|
||||
"Element `{first_invalid_element}` of this union \
|
||||
is not assignable to `{expected_ty_display}`",
|
||||
)),
|
||||
[single] => diag.info(format_args!(
|
||||
"Union elements `{first_invalid_element}` and `{}` \
|
||||
are not assignable to `{expected_ty_display}`",
|
||||
single.display(context.db()),
|
||||
)),
|
||||
rest => diag.info(format_args!(
|
||||
"Union element `{first_invalid_element}`, \
|
||||
and {} more union elements, \
|
||||
are not assignable to `{expected_ty_display}`",
|
||||
rest.len(),
|
||||
)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(matching_overload) = matching_overload {
|
||||
if let Some((name_span, parameter_span)) =
|
||||
matching_overload.get(context.db()).and_then(|overload| {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue