From f28e2a8b9542f5ef271ee2c853c832e8980d4ce4 Mon Sep 17 00:00:00 2001 From: harupy Date: Thu, 21 Nov 2024 20:23:02 +0900 Subject: [PATCH 1/6] Fix PLW1508 to flag os.environ.get --- .../test/fixtures/pylint/invalid_envvar_default.py | 1 + .../src/rules/pylint/rules/invalid_envvar_default.rs | 7 ++++++- ...lint__tests__PLW1508_invalid_envvar_default.py.snap | 10 +++++++++- 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/crates/ruff_linter/resources/test/fixtures/pylint/invalid_envvar_default.py b/crates/ruff_linter/resources/test/fixtures/pylint/invalid_envvar_default.py index ed1065ce0e233..9a7e1f0efafcb 100644 --- a/crates/ruff_linter/resources/test/fixtures/pylint/invalid_envvar_default.py +++ b/crates/ruff_linter/resources/test/fixtures/pylint/invalid_envvar_default.py @@ -12,3 +12,4 @@ os.getenv("B", Z) os.getenv("AA", "GOOD" if Z else "BAR") os.getenv("AA", 1 if Z else "BAR") # [invalid-envvar-default] +os.environ.get("TEST", 12) # [invalid-envvar-default] diff --git a/crates/ruff_linter/src/rules/pylint/rules/invalid_envvar_default.rs b/crates/ruff_linter/src/rules/pylint/rules/invalid_envvar_default.rs index fba62cf5e7c2d..cbd5f86470d1f 100644 --- a/crates/ruff_linter/src/rules/pylint/rules/invalid_envvar_default.rs +++ b/crates/ruff_linter/src/rules/pylint/rules/invalid_envvar_default.rs @@ -51,7 +51,12 @@ pub(crate) fn invalid_envvar_default(checker: &mut Checker, call: &ast::ExprCall if checker .semantic() .resolve_qualified_name(&call.func) - .is_some_and(|qualified_name| matches!(qualified_name.segments(), ["os", "getenv"])) + .is_some_and(|qualified_name| { + matches!( + qualified_name.segments(), + ["os", "getenv"] | ["os", "environ", "get"] + ) + }) { // Find the `default` argument, if it exists. let Some(expr) = call.arguments.find_argument("default", 1) else { diff --git a/crates/ruff_linter/src/rules/pylint/snapshots/ruff_linter__rules__pylint__tests__PLW1508_invalid_envvar_default.py.snap b/crates/ruff_linter/src/rules/pylint/snapshots/ruff_linter__rules__pylint__tests__PLW1508_invalid_envvar_default.py.snap index cccf6cc2909cd..36f84fa2c8ba7 100644 --- a/crates/ruff_linter/src/rules/pylint/snapshots/ruff_linter__rules__pylint__tests__PLW1508_invalid_envvar_default.py.snap +++ b/crates/ruff_linter/src/rules/pylint/snapshots/ruff_linter__rules__pylint__tests__PLW1508_invalid_envvar_default.py.snap @@ -1,6 +1,5 @@ --- source: crates/ruff_linter/src/rules/pylint/mod.rs -snapshot_kind: text --- invalid_envvar_default.py:3:29: PLW1508 Invalid type for environment variable default; expected `str` or `None` | @@ -48,4 +47,13 @@ invalid_envvar_default.py:14:17: PLW1508 Invalid type for environment variable d 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] + | + +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 | From 059c600c2ddf1fec7e17648599829bd6cc1735b1 Mon Sep 17 00:00:00 2001 From: harupy Date: Thu, 21 Nov 2024 22:06:55 +0900 Subject: [PATCH 2/6] Check os.environ.get if preview --- .../src/rules/pylint/rules/invalid_envvar_default.rs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/crates/ruff_linter/src/rules/pylint/rules/invalid_envvar_default.rs b/crates/ruff_linter/src/rules/pylint/rules/invalid_envvar_default.rs index cbd5f86470d1f..6ec7435279c6a 100644 --- a/crates/ruff_linter/src/rules/pylint/rules/invalid_envvar_default.rs +++ b/crates/ruff_linter/src/rules/pylint/rules/invalid_envvar_default.rs @@ -52,10 +52,14 @@ pub(crate) fn invalid_envvar_default(checker: &mut Checker, call: &ast::ExprCall .semantic() .resolve_qualified_name(&call.func) .is_some_and(|qualified_name| { - matches!( - qualified_name.segments(), - ["os", "getenv"] | ["os", "environ", "get"] - ) + 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. From 26d311caffd2c3f0433d16270dbd73763aac54c1 Mon Sep 17 00:00:00 2001 From: harupy Date: Thu, 21 Nov 2024 22:08:21 +0900 Subject: [PATCH 3/6] Add test for preview --- crates/ruff_linter/src/rules/pylint/mod.rs | 1 + ...ew__PLW1508_invalid_envvar_default.py.snap | 59 +++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 crates/ruff_linter/src/rules/pylint/snapshots/ruff_linter__rules__pylint__tests__preview__PLW1508_invalid_envvar_default.py.snap diff --git a/crates/ruff_linter/src/rules/pylint/mod.rs b/crates/ruff_linter/src/rules/pylint/mod.rs index b17e530757a5d..68b10132c0686 100644 --- a/crates/ruff_linter/src/rules/pylint/mod.rs +++ b/crates/ruff_linter/src/rules/pylint/mod.rs @@ -410,6 +410,7 @@ mod tests { Rule::RepeatedEqualityComparison, Path::new("repeated_equality_comparison.py") )] + #[test_case(Rule::InvalidEnvvarDefault, Path::new("invalid_envvar_default.py"))] fn preview_rules(rule_code: Rule, path: &Path) -> Result<()> { let snapshot = format!( "preview__{}_{}", diff --git a/crates/ruff_linter/src/rules/pylint/snapshots/ruff_linter__rules__pylint__tests__preview__PLW1508_invalid_envvar_default.py.snap b/crates/ruff_linter/src/rules/pylint/snapshots/ruff_linter__rules__pylint__tests__preview__PLW1508_invalid_envvar_default.py.snap new file mode 100644 index 0000000000000..36f84fa2c8ba7 --- /dev/null +++ b/crates/ruff_linter/src/rules/pylint/snapshots/ruff_linter__rules__pylint__tests__preview__PLW1508_invalid_envvar_default.py.snap @@ -0,0 +1,59 @@ +--- +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] + | + +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 + | From 3f9b632bb0f15d7be576e13f41b85a0bf698dbd7 Mon Sep 17 00:00:00 2001 From: harupy Date: Thu, 21 Nov 2024 22:09:36 +0900 Subject: [PATCH 4/6] Fix failed test --- ..._pylint__tests__PLW1508_invalid_envvar_default.py.snap | 8 -------- 1 file changed, 8 deletions(-) diff --git a/crates/ruff_linter/src/rules/pylint/snapshots/ruff_linter__rules__pylint__tests__PLW1508_invalid_envvar_default.py.snap b/crates/ruff_linter/src/rules/pylint/snapshots/ruff_linter__rules__pylint__tests__PLW1508_invalid_envvar_default.py.snap index 36f84fa2c8ba7..79f57bfc32936 100644 --- a/crates/ruff_linter/src/rules/pylint/snapshots/ruff_linter__rules__pylint__tests__PLW1508_invalid_envvar_default.py.snap +++ b/crates/ruff_linter/src/rules/pylint/snapshots/ruff_linter__rules__pylint__tests__PLW1508_invalid_envvar_default.py.snap @@ -49,11 +49,3 @@ invalid_envvar_default.py:14:17: PLW1508 Invalid type for environment variable d | ^^^^^^^^^^^^^^^^^ PLW1508 15 | os.environ.get("TEST", 12) # [invalid-envvar-default] | - -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 - | From 8970a09119a79f9e802cea17d448a011997c81e6 Mon Sep 17 00:00:00 2001 From: Harutaka Kawamura Date: Fri, 22 Nov 2024 23:40:59 +0900 Subject: [PATCH 5/6] Update crates/ruff_linter/src/rules/pylint/snapshots/ruff_linter__rules__pylint__tests__PLW1508_invalid_envvar_default.py.snap --- ..._rules__pylint__tests__PLW1508_invalid_envvar_default.py.snap | 1 + 1 file changed, 1 insertion(+) diff --git a/crates/ruff_linter/src/rules/pylint/snapshots/ruff_linter__rules__pylint__tests__PLW1508_invalid_envvar_default.py.snap b/crates/ruff_linter/src/rules/pylint/snapshots/ruff_linter__rules__pylint__tests__PLW1508_invalid_envvar_default.py.snap index 79f57bfc32936..6104e1f5c37be 100644 --- a/crates/ruff_linter/src/rules/pylint/snapshots/ruff_linter__rules__pylint__tests__PLW1508_invalid_envvar_default.py.snap +++ b/crates/ruff_linter/src/rules/pylint/snapshots/ruff_linter__rules__pylint__tests__PLW1508_invalid_envvar_default.py.snap @@ -1,5 +1,6 @@ --- source: crates/ruff_linter/src/rules/pylint/mod.rs +snapshot_kind: text --- invalid_envvar_default.py:3:29: PLW1508 Invalid type for environment variable default; expected `str` or `None` | From 7e6558be248a41a1af6b7649217d33b82dd05e0e Mon Sep 17 00:00:00 2001 From: Harutaka Kawamura Date: Fri, 22 Nov 2024 23:41:33 +0900 Subject: [PATCH 6/6] Update crates/ruff_linter/src/rules/pylint/snapshots/ruff_linter__rules__pylint__tests__preview__PLW1508_invalid_envvar_default.py.snap --- ...ylint__tests__preview__PLW1508_invalid_envvar_default.py.snap | 1 + 1 file changed, 1 insertion(+) diff --git a/crates/ruff_linter/src/rules/pylint/snapshots/ruff_linter__rules__pylint__tests__preview__PLW1508_invalid_envvar_default.py.snap b/crates/ruff_linter/src/rules/pylint/snapshots/ruff_linter__rules__pylint__tests__preview__PLW1508_invalid_envvar_default.py.snap index 36f84fa2c8ba7..ed7dd8e7bedfb 100644 --- a/crates/ruff_linter/src/rules/pylint/snapshots/ruff_linter__rules__pylint__tests__preview__PLW1508_invalid_envvar_default.py.snap +++ b/crates/ruff_linter/src/rules/pylint/snapshots/ruff_linter__rules__pylint__tests__preview__PLW1508_invalid_envvar_default.py.snap @@ -1,5 +1,6 @@ --- source: crates/ruff_linter/src/rules/pylint/mod.rs +snapshot_kind: text --- invalid_envvar_default.py:3:29: PLW1508 Invalid type for environment variable default; expected `str` or `None` |