mirror of
https://github.com/astral-sh/ruff.git
synced 2025-07-07 21:25:08 +00:00
Fix PLW1508
false positive for default string created via a mult operation (#14841)
This commit is contained in:
parent
85402097fc
commit
8d9e408dbb
4 changed files with 44 additions and 3 deletions
|
@ -234,12 +234,39 @@ impl From<&Expr> for ResolvedPythonType {
|
|||
}
|
||||
_ => {}
|
||||
},
|
||||
// Standard arithmetic operators, which coerce to the "highest" number type.
|
||||
Operator::Mult | Operator::FloorDiv | Operator::Pow => match (
|
||||
Operator::Mult => match (
|
||||
ResolvedPythonType::from(left.as_ref()),
|
||||
ResolvedPythonType::from(right.as_ref()),
|
||||
) {
|
||||
// Ex) `1 - 2`
|
||||
// Ex) `2 * 4`
|
||||
(
|
||||
ResolvedPythonType::Atom(PythonType::Number(left)),
|
||||
ResolvedPythonType::Atom(PythonType::Number(right)),
|
||||
) => {
|
||||
return ResolvedPythonType::Atom(PythonType::Number(
|
||||
left.coerce(right),
|
||||
));
|
||||
}
|
||||
// Ex) `"1" * 2` or `2 * "1"`
|
||||
(
|
||||
ResolvedPythonType::Atom(PythonType::String),
|
||||
ResolvedPythonType::Atom(PythonType::Number(NumberLike::Integer)),
|
||||
)
|
||||
| (
|
||||
ResolvedPythonType::Atom(PythonType::Number(NumberLike::Integer)),
|
||||
ResolvedPythonType::Atom(PythonType::String),
|
||||
) => return ResolvedPythonType::Atom(PythonType::String),
|
||||
(ResolvedPythonType::Atom(_), ResolvedPythonType::Atom(_)) => {
|
||||
return ResolvedPythonType::TypeError;
|
||||
}
|
||||
_ => {}
|
||||
},
|
||||
// Standard arithmetic operators, which coerce to the "highest" number type.
|
||||
Operator::FloorDiv | Operator::Pow => match (
|
||||
ResolvedPythonType::from(left.as_ref()),
|
||||
ResolvedPythonType::from(right.as_ref()),
|
||||
) {
|
||||
// Ex) `2 ** 4`
|
||||
(
|
||||
ResolvedPythonType::Atom(PythonType::Number(left)),
|
||||
ResolvedPythonType::Atom(PythonType::Number(right)),
|
||||
|
@ -476,6 +503,14 @@ mod tests {
|
|||
ResolvedPythonType::from(parse("1.0 * 2j").expr()),
|
||||
ResolvedPythonType::Atom(PythonType::Number(NumberLike::Complex))
|
||||
);
|
||||
assert_eq!(
|
||||
ResolvedPythonType::from(parse("'AA' * 2").expr()),
|
||||
ResolvedPythonType::Atom(PythonType::String)
|
||||
);
|
||||
assert_eq!(
|
||||
ResolvedPythonType::from(parse("4 * 'AA'").expr()),
|
||||
ResolvedPythonType::Atom(PythonType::String)
|
||||
);
|
||||
assert_eq!(
|
||||
ResolvedPythonType::from(parse("1 / True").expr()),
|
||||
ResolvedPythonType::Atom(PythonType::Number(NumberLike::Float))
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue