This commit is contained in:
Dan Parizher 2025-11-16 12:38:01 -05:00 committed by GitHub
commit 9538be2d38
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 94 additions and 1 deletions

View file

@ -0,0 +1,21 @@
# Module-level mutable "constant"
MY_SET = {"ABC", "DEF"} # plain set
MY_LIST = [1, 2, 3] # plain list
MY_DICT = {"key": "value"} # plain dict
# NOT triggering B006 (correct - UPPER_CASE constants are excluded per PEP 8)
def func_A(s: set[str] = MY_SET):
return s
# Triggering B006 (correct)
def func_B(s: set[str] = {"ABC", "DEF"}):
return s
# NOT triggering B006 (correct - UPPER_CASE constants are excluded per PEP 8)
def func_C(items: list[int] = MY_LIST):
return items
# NOT triggering B006 (correct - UPPER_CASE constants are excluded per PEP 8)
def func_D(data: dict[str, str] = MY_DICT):
return data

View file

@ -48,6 +48,7 @@ mod tests {
#[test_case(Rule::MutableArgumentDefault, Path::new("B006_7.py"))]
#[test_case(Rule::MutableArgumentDefault, Path::new("B006_8.py"))]
#[test_case(Rule::MutableArgumentDefault, Path::new("B006_9.py"))]
#[test_case(Rule::MutableArgumentDefault, Path::new("B006_10.py"))]
#[test_case(Rule::MutableArgumentDefault, Path::new("B006_B008.py"))]
#[test_case(Rule::MutableArgumentDefault, Path::new("B006_1.pyi"))]
#[test_case(Rule::NoExplicitStacklevel, Path::new("B028.py"))]
@ -93,6 +94,7 @@ mod tests {
#[test_case(Rule::MutableArgumentDefault, Path::new("B006_7.py"))]
#[test_case(Rule::MutableArgumentDefault, Path::new("B006_8.py"))]
#[test_case(Rule::MutableArgumentDefault, Path::new("B006_9.py"))]
#[test_case(Rule::MutableArgumentDefault, Path::new("B006_10.py"))]
#[test_case(Rule::MutableArgumentDefault, Path::new("B006_B008.py"))]
#[test_case(Rule::MutableArgumentDefault, Path::new("B006_1.pyi"))]
fn preview_rules(rule_code: Rule, path: &Path) -> Result<()> {

View file

@ -7,7 +7,10 @@ use ruff_python_ast::parenthesize::parenthesized_range;
use ruff_python_ast::{self as ast, Expr, ParameterWithDefault};
use ruff_python_semantic::SemanticModel;
use ruff_python_semantic::analyze::function_type::is_stub;
use ruff_python_semantic::analyze::typing::{is_immutable_annotation, is_mutable_expr};
use ruff_python_semantic::analyze::typing::{
find_binding_value, is_immutable_annotation, is_mutable_expr,
};
use ruff_python_stdlib::str;
use ruff_python_trivia::{indentation_at_offset, textwrap};
use ruff_source_file::LineRanges;
use ruff_text_size::Ranged;
@ -142,6 +145,27 @@ fn is_guaranteed_mutable_expr(expr: &Expr, semantic: &SemanticModel) -> bool {
elts.iter().any(|e| is_guaranteed_mutable_expr(e, semantic))
}
Expr::Named(ast::ExprNamed { value, .. }) => is_guaranteed_mutable_expr(value, semantic),
Expr::Name(name) => {
// Exclude UPPER_CASE constants (PEP 8 convention - meant to be read-only)
if str::is_cased_uppercase(&name.id) {
return false;
}
// Resolve module-level constants that are bound to mutable objects
let Some(binding_id) = semantic.only_binding(name) else {
return false;
};
let binding = semantic.binding(binding_id);
// Only check assignments (not imports, function parameters, etc.)
if !binding.kind.is_assignment() {
return false;
}
// Get the assigned value and check if it's mutable
if let Some(value) = find_binding_value(binding, semantic) {
is_guaranteed_mutable_expr(value, semantic)
} else {
false
}
}
_ => is_mutable_expr(expr, semantic),
}
}

View file

@ -0,0 +1,23 @@
---
source: crates/ruff_linter/src/rules/flake8_bugbear/mod.rs
---
B006 [*] Do not use mutable data structures for argument defaults
--> B006_10.py:11:26
|
10 | # Triggering B006 (correct)
11 | def func_B(s: set[str] = {"ABC", "DEF"}):
| ^^^^^^^^^^^^^^
12 | return s
|
help: Replace with `None`; initialize within function
8 | return s
9 |
10 | # Triggering B006 (correct)
- def func_B(s: set[str] = {"ABC", "DEF"}):
11 + def func_B(s: set[str] = None):
12 + if s is None:
13 + s = {"ABC", "DEF"}
14 | return s
15 |
16 | # NOT triggering B006 (correct - UPPER_CASE constants are excluded per PEP 8)
note: This is an unsafe fix and may change runtime behavior

View file

@ -0,0 +1,23 @@
---
source: crates/ruff_linter/src/rules/flake8_bugbear/mod.rs
---
B006 [*] Do not use mutable data structures for argument defaults
--> B006_10.py:11:26
|
10 | # Triggering B006 (correct)
11 | def func_B(s: set[str] = {"ABC", "DEF"}):
| ^^^^^^^^^^^^^^
12 | return s
|
help: Replace with `None`; initialize within function
8 | return s
9 |
10 | # Triggering B006 (correct)
- def func_B(s: set[str] = {"ABC", "DEF"}):
11 + def func_B(s: set[str] = None):
12 + if s is None:
13 + s = {"ABC", "DEF"}
14 | return s
15 |
16 | # NOT triggering B006 (correct - UPPER_CASE constants are excluded per PEP 8)
note: This is an unsafe fix and may change runtime behavior