mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-29 13:25:17 +00:00
Use binary semantics when __iadd__
et al are unbound (#13987)
## Summary I noticed that augmented assignments on floats were yielding "not supported" diagnostics. If the dunder isn't bound at all, we should use binary operator semantics, rather than treating it as not-callable.
This commit is contained in:
parent
71536a43db
commit
262c04f297
3 changed files with 28 additions and 26 deletions
|
@ -1425,26 +1425,32 @@ impl<'db> TypeInferenceBuilder<'db> {
|
|||
};
|
||||
let value_type = self.infer_expression(value);
|
||||
|
||||
// If the target defines, e.g., `__iadd__`, infer the augmented assignment as a call to that
|
||||
// dunder.
|
||||
if let Type::Instance(class) = target_type {
|
||||
let class_member = class.class_member(self.db, op.in_place_dunder());
|
||||
let call = class_member.call(self.db, &[target_type, value_type]);
|
||||
|
||||
return match call.return_ty_result(self.db, AnyNodeRef::StmtAugAssign(assignment), self)
|
||||
{
|
||||
Ok(t) => t,
|
||||
Err(e) => {
|
||||
self.add_diagnostic(
|
||||
assignment.into(),
|
||||
"unsupported-operator",
|
||||
format_args!(
|
||||
"Operator `{op}=` is unsupported between objects of type `{}` and `{}`",
|
||||
target_type.display(self.db),
|
||||
value_type.display(self.db)
|
||||
),
|
||||
);
|
||||
e.return_ty()
|
||||
}
|
||||
};
|
||||
if !class_member.is_unbound() {
|
||||
let call = class_member.call(self.db, &[target_type, value_type]);
|
||||
return match call.return_ty_result(
|
||||
self.db,
|
||||
AnyNodeRef::StmtAugAssign(assignment),
|
||||
self,
|
||||
) {
|
||||
Ok(t) => t,
|
||||
Err(e) => {
|
||||
self.add_diagnostic(
|
||||
assignment.into(),
|
||||
"unsupported-operator",
|
||||
format_args!(
|
||||
"Operator `{op}=` is unsupported between objects of type `{}` and `{}`",
|
||||
target_type.display(self.db),
|
||||
value_type.display(self.db)
|
||||
),
|
||||
);
|
||||
e.return_ty()
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
let left_ty = target_type;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue