mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-26 11:59:35 +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
|
@ -13,3 +13,5 @@ os.getenv("B", Z)
|
||||||
os.getenv("AA", "GOOD" if Z else "BAR")
|
os.getenv("AA", "GOOD" if Z else "BAR")
|
||||||
os.getenv("AA", 1 if Z else "BAR") # [invalid-envvar-default]
|
os.getenv("AA", 1 if Z else "BAR") # [invalid-envvar-default]
|
||||||
os.environ.get("TEST", 12) # [invalid-envvar-default]
|
os.environ.get("TEST", 12) # [invalid-envvar-default]
|
||||||
|
os.environ.get("TEST", "AA" * 12)
|
||||||
|
os.environ.get("TEST", 13 * "AA")
|
||||||
|
|
|
@ -49,4 +49,5 @@ invalid_envvar_default.py:14:17: PLW1508 Invalid type for environment variable d
|
||||||
14 | os.getenv("AA", 1 if Z else "BAR") # [invalid-envvar-default]
|
14 | os.getenv("AA", 1 if Z else "BAR") # [invalid-envvar-default]
|
||||||
| ^^^^^^^^^^^^^^^^^ PLW1508
|
| ^^^^^^^^^^^^^^^^^ PLW1508
|
||||||
15 | os.environ.get("TEST", 12) # [invalid-envvar-default]
|
15 | os.environ.get("TEST", 12) # [invalid-envvar-default]
|
||||||
|
16 | os.environ.get("TEST", "AA" * 12)
|
||||||
|
|
|
|
||||||
|
|
|
@ -49,6 +49,7 @@ invalid_envvar_default.py:14:17: PLW1508 Invalid type for environment variable d
|
||||||
14 | os.getenv("AA", 1 if Z else "BAR") # [invalid-envvar-default]
|
14 | os.getenv("AA", 1 if Z else "BAR") # [invalid-envvar-default]
|
||||||
| ^^^^^^^^^^^^^^^^^ PLW1508
|
| ^^^^^^^^^^^^^^^^^ PLW1508
|
||||||
15 | os.environ.get("TEST", 12) # [invalid-envvar-default]
|
15 | os.environ.get("TEST", 12) # [invalid-envvar-default]
|
||||||
|
16 | os.environ.get("TEST", "AA" * 12)
|
||||||
|
|
|
|
||||||
|
|
||||||
invalid_envvar_default.py:15:24: PLW1508 Invalid type for environment variable default; expected `str` or `None`
|
invalid_envvar_default.py:15:24: PLW1508 Invalid type for environment variable default; expected `str` or `None`
|
||||||
|
@ -57,4 +58,6 @@ invalid_envvar_default.py:15:24: PLW1508 Invalid type for environment variable d
|
||||||
14 | os.getenv("AA", 1 if Z else "BAR") # [invalid-envvar-default]
|
14 | os.getenv("AA", 1 if Z else "BAR") # [invalid-envvar-default]
|
||||||
15 | os.environ.get("TEST", 12) # [invalid-envvar-default]
|
15 | os.environ.get("TEST", 12) # [invalid-envvar-default]
|
||||||
| ^^ PLW1508
|
| ^^ PLW1508
|
||||||
|
16 | os.environ.get("TEST", "AA" * 12)
|
||||||
|
17 | os.environ.get("TEST", 13 * "AA")
|
||||||
|
|
|
|
||||||
|
|
|
@ -234,12 +234,39 @@ impl From<&Expr> for ResolvedPythonType {
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
},
|
},
|
||||||
// Standard arithmetic operators, which coerce to the "highest" number type.
|
Operator::Mult => match (
|
||||||
Operator::Mult | Operator::FloorDiv | Operator::Pow => match (
|
|
||||||
ResolvedPythonType::from(left.as_ref()),
|
ResolvedPythonType::from(left.as_ref()),
|
||||||
ResolvedPythonType::from(right.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(left)),
|
||||||
ResolvedPythonType::Atom(PythonType::Number(right)),
|
ResolvedPythonType::Atom(PythonType::Number(right)),
|
||||||
|
@ -476,6 +503,14 @@ mod tests {
|
||||||
ResolvedPythonType::from(parse("1.0 * 2j").expr()),
|
ResolvedPythonType::from(parse("1.0 * 2j").expr()),
|
||||||
ResolvedPythonType::Atom(PythonType::Number(NumberLike::Complex))
|
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!(
|
assert_eq!(
|
||||||
ResolvedPythonType::from(parse("1 / True").expr()),
|
ResolvedPythonType::from(parse("1 / True").expr()),
|
||||||
ResolvedPythonType::Atom(PythonType::Number(NumberLike::Float))
|
ResolvedPythonType::Atom(PythonType::Number(NumberLike::Float))
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue