Skip to content

Commit

Permalink
Allow string percent formatting in os.getenv (#3518)
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Mar 14, 2023
1 parent 1b738f8 commit c50d6da
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
os.getenv("AA", f"GOOD")
os.getenv("AA", "GOOD" + "BAD")
os.getenv("AA", "GOOD" + 1)
os.getenv("AA", "GOOD %s" % "BAD")
os.getenv("B", Z)

11 changes: 11 additions & 0 deletions crates/ruff/src/rules/pylint/rules/invalid_envvar_default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ fn is_valid_default(expr: &Expr) -> bool {
return true;
}

// Allow string concatenation.
if let ExprKind::BinOp {
left,
right,
Expand All @@ -58,6 +59,16 @@ fn is_valid_default(expr: &Expr) -> bool {
return is_valid_default(left) && is_valid_default(right);
}

// Allow string formatting.
if let ExprKind::BinOp {
left,
op: Operator::Mod,
..
} = &expr.node
{
return is_valid_default(left);
}

// Otherwise, the default must be a string or `None`.
matches!(
expr.node,
Expand Down
11 changes: 11 additions & 0 deletions crates/ruff/src/rules/pylint/rules/invalid_envvar_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ fn is_valid_key(expr: &Expr) -> bool {
return true;
}

// Allow string concatenation.
if let ExprKind::BinOp {
left,
right,
Expand All @@ -55,6 +56,16 @@ fn is_valid_key(expr: &Expr) -> bool {
return is_valid_key(left) && is_valid_key(right);
}

// Allow string formatting.
if let ExprKind::BinOp {
left,
op: Operator::Mod,
..
} = &expr.node
{
return is_valid_key(left);
}

// Otherwise, the default must be a string.
matches!(
expr.node,
Expand Down

0 comments on commit c50d6da

Please sign in to comment.