Skip to content

Commit d561912

Browse files
committed
[pylint] Detect invalid default value type for os.environ.get (PLW1508)
1 parent 0ced1cb commit d561912

4 files changed

Lines changed: 14 additions & 71 deletions

File tree

crates/ruff_linter/src/rules/pylint/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,6 @@ mod tests {
445445
Rule::RepeatedEqualityComparison,
446446
Path::new("repeated_equality_comparison.py")
447447
)]
448-
#[test_case(Rule::InvalidEnvvarDefault, Path::new("invalid_envvar_default.py"))]
449448
#[test_case(Rule::BadStrStripCall, Path::new("bad_str_strip_call.py"))]
450449
#[test_case(
451450
Rule::BadStaticmethodArgument,

crates/ruff_linter/src/rules/pylint/rules/invalid_envvar_default.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,10 @@ pub(crate) fn invalid_envvar_default(checker: &Checker, call: &ast::ExprCall) {
5252
.semantic()
5353
.resolve_qualified_name(&call.func)
5454
.is_some_and(|qualified_name| {
55-
if checker.settings.preview.is_enabled() {
56-
matches!(
57-
qualified_name.segments(),
58-
["os", "getenv"] | ["os", "environ", "get"]
59-
)
60-
} else {
61-
matches!(qualified_name.segments(), ["os", "getenv"])
62-
}
55+
matches!(
56+
qualified_name.segments(),
57+
["os", "getenv"] | ["os", "environ", "get"]
58+
)
6359
})
6460
{
6561
// Find the `default` argument, if it exists.

crates/ruff_linter/src/rules/pylint/snapshots/ruff_linter__rules__pylint__tests__PLW1508_invalid_envvar_default.py.snap

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,13 @@ invalid_envvar_default.py:14:17: PLW1508 Invalid type for environment variable d
5050
15 | os.environ.get("TEST", 12) # [invalid-envvar-default]
5151
16 | os.environ.get("TEST", "AA" * 12)
5252
|
53+
54+
invalid_envvar_default.py:15:24: PLW1508 Invalid type for environment variable default; expected `str` or `None`
55+
|
56+
13 | os.getenv("AA", "GOOD" if Z else "BAR")
57+
14 | os.getenv("AA", 1 if Z else "BAR") # [invalid-envvar-default]
58+
15 | os.environ.get("TEST", 12) # [invalid-envvar-default]
59+
| ^^ PLW1508
60+
16 | os.environ.get("TEST", "AA" * 12)
61+
17 | os.environ.get("TEST", 13 * "AA")
62+
|

crates/ruff_linter/src/rules/pylint/snapshots/ruff_linter__rules__pylint__tests__preview__PLW1508_invalid_envvar_default.py.snap

Lines changed: 0 additions & 62 deletions
This file was deleted.

0 commit comments

Comments
 (0)