-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pylint: W1508 invalid-envvar-default (#3449)
- Loading branch information
Showing
9 changed files
with
154 additions
and
0 deletions.
There are no files selected for viewing
8 changes: 8 additions & 0 deletions
8
crates/ruff/resources/test/fixtures/pylint/invalid_envvar_default.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import os | ||
|
||
tempVar = os.getenv("TEST", 12) # [invalid-envvar-default] | ||
goodVar = os.getenv("TESTING", None) | ||
dictVarBad = os.getenv("AAA", {"a", 7}) # [invalid-envvar-default] | ||
print(os.getenv("TEST", False)) # [invalid-envvar-default] | ||
os.getenv("AA", "GOOD") | ||
os.getenv("B", Z) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
crates/ruff/src/rules/pylint/rules/invalid_envvar_default.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
use rustpython_parser::ast::{Constant, Expr, ExprKind, Keyword}; | ||
|
||
use ruff_diagnostics::{Diagnostic, Violation}; | ||
use ruff_macros::{derive_message_formats, violation}; | ||
use ruff_python_ast::types::Range; | ||
|
||
use crate::checkers::ast::Checker; | ||
|
||
/// ## What it does | ||
/// Checks for `env.getenv` calls with invalid default values. | ||
/// | ||
/// ## Why is this bad? | ||
/// If an environment variable is set, `env.getenv` will return its value as | ||
/// a string. If the environment variable is _not_ set, `env.getenv` will | ||
/// return `None`, or the default value if one is provided. | ||
/// | ||
/// If the default value is not a string or `None`, then it will be | ||
/// inconsistent with the return type of `env.getenv`, which can lead to | ||
/// confusing behavior. | ||
/// | ||
/// ## Example | ||
/// ```python | ||
/// int(env.getenv("FOO", 1)) | ||
/// ``` | ||
/// | ||
/// Use instead: | ||
/// ```python | ||
/// int(env.getenv("FOO", "1")) | ||
/// ``` | ||
#[violation] | ||
pub struct InvalidEnvvarDefault; | ||
|
||
impl Violation for InvalidEnvvarDefault { | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
format!("Invalid type for environment variable default; expected `str` or `None`") | ||
} | ||
} | ||
|
||
fn is_valid_default(expr: &Expr) -> bool { | ||
// We can't infer the types of these defaults, so assume they're valid. | ||
if matches!( | ||
expr.node, | ||
ExprKind::Name { .. } | ||
| ExprKind::Attribute { .. } | ||
| ExprKind::Subscript { .. } | ||
| ExprKind::Call { .. } | ||
) { | ||
return true; | ||
} | ||
|
||
// Otherwise, the default must be a string or `None`. | ||
matches!( | ||
expr.node, | ||
ExprKind::Constant { | ||
value: Constant::Str { .. } | Constant::None { .. }, | ||
.. | ||
} | ||
) | ||
} | ||
|
||
/// PLW1508 | ||
pub fn invalid_envvar_default( | ||
checker: &mut Checker, | ||
func: &Expr, | ||
args: &[Expr], | ||
keywords: &[Keyword], | ||
) { | ||
if checker | ||
.ctx | ||
.resolve_call_path(func) | ||
.map_or(false, |call_path| call_path.as_slice() == ["os", "getenv"]) | ||
{ | ||
// Find the `default` argument, if it exists. | ||
let Some(expr) = args.get(1).or_else(|| { | ||
keywords | ||
.iter() | ||
.find(|keyword| keyword.node.arg.as_ref().map_or(false, |arg| arg == "default")) | ||
.map(|keyword| &keyword.node.value) | ||
}) else { | ||
return; | ||
}; | ||
|
||
if !is_valid_default(expr) { | ||
checker | ||
.diagnostics | ||
.push(Diagnostic::new(InvalidEnvvarDefault, Range::from(expr))); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
...rules/pylint/snapshots/ruff__rules__pylint__tests__PLW1508_invalid_envvar_default.py.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
--- | ||
source: crates/ruff/src/rules/pylint/mod.rs | ||
expression: diagnostics | ||
--- | ||
- kind: | ||
name: InvalidEnvvarDefault | ||
body: "Invalid type for environment variable default; expected `str` or `None`" | ||
suggestion: ~ | ||
fixable: false | ||
location: | ||
row: 3 | ||
column: 28 | ||
end_location: | ||
row: 3 | ||
column: 30 | ||
fix: ~ | ||
parent: ~ | ||
- kind: | ||
name: InvalidEnvvarDefault | ||
body: "Invalid type for environment variable default; expected `str` or `None`" | ||
suggestion: ~ | ||
fixable: false | ||
location: | ||
row: 5 | ||
column: 30 | ||
end_location: | ||
row: 5 | ||
column: 38 | ||
fix: ~ | ||
parent: ~ | ||
- kind: | ||
name: InvalidEnvvarDefault | ||
body: "Invalid type for environment variable default; expected `str` or `None`" | ||
suggestion: ~ | ||
fixable: false | ||
location: | ||
row: 6 | ||
column: 24 | ||
end_location: | ||
row: 6 | ||
column: 29 | ||
fix: ~ | ||
parent: ~ | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.