mirror of https://github.com/astral-sh/ruff
[`pylint`] Detect invalid default value type for `os.environ.get` (`PLW1508`) (#16674)
## Summary This PR stabilizes the new behavior introduced in https://github.com/astral-sh/ruff/pull/14512 to also detect defalut value arguemnts to `os.environ.get` that have an invalid type (not `str`). There's an upstream issue for this behavior change https://github.com/pylint-dev/pylint/issues/10092 that was accepted and a PR, but it hasn't been merged yet. This behavior change was first shipped with Ruff 0.8.1 (Nov 22). There has only be one PR since the new behavior was introduced but it was unrelated to the scope increase (https://github.com/astral-sh/ruff/pull/14841).
This commit is contained in:
parent
b9ed3e3876
commit
7af5b98606
|
|
@ -445,7 +445,6 @@ mod tests {
|
|||
Rule::RepeatedEqualityComparison,
|
||||
Path::new("repeated_equality_comparison.py")
|
||||
)]
|
||||
#[test_case(Rule::InvalidEnvvarDefault, Path::new("invalid_envvar_default.py"))]
|
||||
#[test_case(Rule::BadStrStripCall, Path::new("bad_str_strip_call.py"))]
|
||||
#[test_case(
|
||||
Rule::BadStaticmethodArgument,
|
||||
|
|
|
|||
|
|
@ -52,14 +52,10 @@ pub(crate) fn invalid_envvar_default(checker: &Checker, call: &ast::ExprCall) {
|
|||
.semantic()
|
||||
.resolve_qualified_name(&call.func)
|
||||
.is_some_and(|qualified_name| {
|
||||
if checker.settings.preview.is_enabled() {
|
||||
matches!(
|
||||
qualified_name.segments(),
|
||||
["os", "getenv"] | ["os", "environ", "get"]
|
||||
)
|
||||
} else {
|
||||
matches!(qualified_name.segments(), ["os", "getenv"])
|
||||
}
|
||||
})
|
||||
{
|
||||
// Find the `default` argument, if it exists.
|
||||
|
|
|
|||
|
|
@ -50,3 +50,13 @@ invalid_envvar_default.py:14:17: PLW1508 Invalid type for environment variable d
|
|||
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`
|
||||
|
|
||||
13 | os.getenv("AA", "GOOD" if Z else "BAR")
|
||||
14 | os.getenv("AA", 1 if Z else "BAR") # [invalid-envvar-default]
|
||||
15 | os.environ.get("TEST", 12) # [invalid-envvar-default]
|
||||
| ^^ PLW1508
|
||||
16 | os.environ.get("TEST", "AA" * 12)
|
||||
17 | os.environ.get("TEST", 13 * "AA")
|
||||
|
|
||||
|
|
|
|||
|
|
@ -1,62 +0,0 @@
|
|||
---
|
||||
source: crates/ruff_linter/src/rules/pylint/mod.rs
|
||||
---
|
||||
invalid_envvar_default.py:3:29: PLW1508 Invalid type for environment variable default; expected `str` or `None`
|
||||
|
|
||||
1 | import os
|
||||
2 |
|
||||
3 | tempVar = os.getenv("TEST", 12) # [invalid-envvar-default]
|
||||
| ^^ PLW1508
|
||||
4 | goodVar = os.getenv("TESTING", None)
|
||||
5 | dictVarBad = os.getenv("AAA", {"a", 7}) # [invalid-envvar-default]
|
||||
|
|
||||
|
||||
invalid_envvar_default.py:5:31: PLW1508 Invalid type for environment variable default; expected `str` or `None`
|
||||
|
|
||||
3 | tempVar = os.getenv("TEST", 12) # [invalid-envvar-default]
|
||||
4 | goodVar = os.getenv("TESTING", None)
|
||||
5 | dictVarBad = os.getenv("AAA", {"a", 7}) # [invalid-envvar-default]
|
||||
| ^^^^^^^^ PLW1508
|
||||
6 | print(os.getenv("TEST", False)) # [invalid-envvar-default]
|
||||
7 | os.getenv("AA", "GOOD")
|
||||
|
|
||||
|
||||
invalid_envvar_default.py:6:25: PLW1508 Invalid type for environment variable default; expected `str` or `None`
|
||||
|
|
||||
4 | goodVar = os.getenv("TESTING", None)
|
||||
5 | dictVarBad = os.getenv("AAA", {"a", 7}) # [invalid-envvar-default]
|
||||
6 | print(os.getenv("TEST", False)) # [invalid-envvar-default]
|
||||
| ^^^^^ PLW1508
|
||||
7 | os.getenv("AA", "GOOD")
|
||||
8 | os.getenv("AA", f"GOOD")
|
||||
|
|
||||
|
||||
invalid_envvar_default.py:10:17: PLW1508 Invalid type for environment variable default; expected `str` or `None`
|
||||
|
|
||||
8 | os.getenv("AA", f"GOOD")
|
||||
9 | os.getenv("AA", "GOOD" + "BAR")
|
||||
10 | os.getenv("AA", "GOOD" + 1)
|
||||
| ^^^^^^^^^^ PLW1508
|
||||
11 | os.getenv("AA", "GOOD %s" % "BAR")
|
||||
12 | os.getenv("B", Z)
|
||||
|
|
||||
|
||||
invalid_envvar_default.py:14:17: PLW1508 Invalid type for environment variable default; expected `str` or `None`
|
||||
|
|
||||
12 | os.getenv("B", Z)
|
||||
13 | os.getenv("AA", "GOOD" if Z else "BAR")
|
||||
14 | os.getenv("AA", 1 if Z else "BAR") # [invalid-envvar-default]
|
||||
| ^^^^^^^^^^^^^^^^^ PLW1508
|
||||
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`
|
||||
|
|
||||
13 | os.getenv("AA", "GOOD" if Z else "BAR")
|
||||
14 | os.getenv("AA", 1 if Z else "BAR") # [invalid-envvar-default]
|
||||
15 | os.environ.get("TEST", 12) # [invalid-envvar-default]
|
||||
| ^^ PLW1508
|
||||
16 | os.environ.get("TEST", "AA" * 12)
|
||||
17 | os.environ.get("TEST", 13 * "AA")
|
||||
|
|
||||
Loading…
Reference in New Issue