diff --git a/crates/ruff_linter/resources/test/fixtures/flake8_bugbear/B006_10.py b/crates/ruff_linter/resources/test/fixtures/flake8_bugbear/B006_10.py index c527182cab..88d6247362 100644 --- a/crates/ruff_linter/resources/test/fixtures/flake8_bugbear/B006_10.py +++ b/crates/ruff_linter/resources/test/fixtures/flake8_bugbear/B006_10.py @@ -3,7 +3,7 @@ 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) +# NOT triggering B006 (correct - PEP 8 specifies that UPPER_CASE variables are considered constants) def func_A(s: set[str] = MY_SET): return s @@ -11,11 +11,11 @@ def func_A(s: set[str] = MY_SET): def func_B(s: set[str] = {"ABC", "DEF"}): return s -# NOT triggering B006 (correct - UPPER_CASE constants are excluded per PEP 8) +# NOT triggering B006 (correct - PEP 8 specifies that UPPER_CASE variables are considered constants) def func_C(items: list[int] = MY_LIST): return items -# NOT triggering B006 (correct - UPPER_CASE constants are excluded per PEP 8) +# NOT triggering B006 (correct - PEP 8 specifies that UPPER_CASE variables are considered constants) def func_D(data: dict[str, str] = MY_DICT): return data diff --git a/crates/ruff_linter/resources/test/fixtures/flake8_bugbear/B006_11.py b/crates/ruff_linter/resources/test/fixtures/flake8_bugbear/B006_11.py new file mode 100644 index 0000000000..dd279030c2 --- /dev/null +++ b/crates/ruff_linter/resources/test/fixtures/flake8_bugbear/B006_11.py @@ -0,0 +1,39 @@ +# Test nested function reading value from outer scope +def outer_function(): + my_list = [1, 2, 3] # Assignment in outer function + + # Should trigger B006 - nested function using mutable from outer scope + def inner_function(items=my_list): + return items + + return inner_function + + +# Test that imports don't trigger B006 (assignment restriction) +from some_module import IMPORTED_LIST + +# Should NOT trigger B006 - imported names are not assignments +def func_with_import(items=IMPORTED_LIST): + return items + + +# Test that function parameters don't trigger B006 (assignment restriction) +def func_with_param(param_list): + # Should NOT trigger B006 - function parameters are not assignments + def nested_func(items=param_list): + return items + + return nested_func + + +# Test module-level assignment that should trigger B006 +module_list = [1, 2, 3] # Module-level assignment + +# Should trigger B006 - module-level assignment used as default +def func_with_module_var(items=module_list): + return items + + +# Test that non-assignment bindings don't trigger B006 +# (This would require a more complex setup to actually test, so we'll focus on the key cases above) + diff --git a/crates/ruff_linter/src/rules/flake8_bugbear/mod.rs b/crates/ruff_linter/src/rules/flake8_bugbear/mod.rs index 46a3bba26c..44af8c2232 100644 --- a/crates/ruff_linter/src/rules/flake8_bugbear/mod.rs +++ b/crates/ruff_linter/src/rules/flake8_bugbear/mod.rs @@ -49,6 +49,7 @@ mod tests { #[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_11.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"))] @@ -95,6 +96,7 @@ mod tests { #[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_11.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<()> { diff --git a/crates/ruff_linter/src/rules/flake8_bugbear/rules/mutable_argument_default.rs b/crates/ruff_linter/src/rules/flake8_bugbear/rules/mutable_argument_default.rs index 01a7fe2314..fb4be84137 100644 --- a/crates/ruff_linter/src/rules/flake8_bugbear/rules/mutable_argument_default.rs +++ b/crates/ruff_linter/src/rules/flake8_bugbear/rules/mutable_argument_default.rs @@ -150,12 +150,15 @@ fn is_guaranteed_mutable_expr(expr: &Expr, semantic: &SemanticModel) -> bool { if str::is_cased_uppercase(&name.id) { return false; } - // Resolve module-level constants that are bound to mutable objects + // Resolve the name in the current scope (module-level, function-level, etc.) + // This can resolve constants, non-constants, and any name in the current scope 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.) + // This restriction ensures we only flag cases where a mutable object is explicitly + // assigned to a name, which is then used as a default argument. if !binding.kind.is_assignment() { return false; } diff --git a/crates/ruff_linter/src/rules/flake8_bugbear/snapshots/ruff_linter__rules__flake8_bugbear__tests__B006_B006_10.py.snap b/crates/ruff_linter/src/rules/flake8_bugbear/snapshots/ruff_linter__rules__flake8_bugbear__tests__B006_B006_10.py.snap index 3282c97ae8..1c9554b69b 100644 --- a/crates/ruff_linter/src/rules/flake8_bugbear/snapshots/ruff_linter__rules__flake8_bugbear__tests__B006_B006_10.py.snap +++ b/crates/ruff_linter/src/rules/flake8_bugbear/snapshots/ruff_linter__rules__flake8_bugbear__tests__B006_B006_10.py.snap @@ -19,5 +19,5 @@ help: Replace with `None`; initialize within function 13 + s = {"ABC", "DEF"} 14 | return s 15 | -16 | # NOT triggering B006 (correct - UPPER_CASE constants are excluded per PEP 8) +16 | # NOT triggering B006 (correct - PEP 8 specifies that UPPER_CASE variables are considered constants) note: This is an unsafe fix and may change runtime behavior diff --git a/crates/ruff_linter/src/rules/flake8_bugbear/snapshots/ruff_linter__rules__flake8_bugbear__tests__B006_B006_11.py.snap b/crates/ruff_linter/src/rules/flake8_bugbear/snapshots/ruff_linter__rules__flake8_bugbear__tests__B006_B006_11.py.snap new file mode 100644 index 0000000000..967e60a4f9 --- /dev/null +++ b/crates/ruff_linter/src/rules/flake8_bugbear/snapshots/ruff_linter__rules__flake8_bugbear__tests__B006_B006_11.py.snap @@ -0,0 +1,4 @@ +--- +source: crates/ruff_linter/src/rules/flake8_bugbear/mod.rs +--- + diff --git a/crates/ruff_linter/src/rules/flake8_bugbear/snapshots/ruff_linter__rules__flake8_bugbear__tests__preview__B006_B006_10.py.snap b/crates/ruff_linter/src/rules/flake8_bugbear/snapshots/ruff_linter__rules__flake8_bugbear__tests__preview__B006_B006_10.py.snap index 3282c97ae8..1c9554b69b 100644 --- a/crates/ruff_linter/src/rules/flake8_bugbear/snapshots/ruff_linter__rules__flake8_bugbear__tests__preview__B006_B006_10.py.snap +++ b/crates/ruff_linter/src/rules/flake8_bugbear/snapshots/ruff_linter__rules__flake8_bugbear__tests__preview__B006_B006_10.py.snap @@ -19,5 +19,5 @@ help: Replace with `None`; initialize within function 13 + s = {"ABC", "DEF"} 14 | return s 15 | -16 | # NOT triggering B006 (correct - UPPER_CASE constants are excluded per PEP 8) +16 | # NOT triggering B006 (correct - PEP 8 specifies that UPPER_CASE variables are considered constants) note: This is an unsafe fix and may change runtime behavior diff --git a/crates/ruff_linter/src/rules/flake8_bugbear/snapshots/ruff_linter__rules__flake8_bugbear__tests__preview__B006_B006_11.py.snap b/crates/ruff_linter/src/rules/flake8_bugbear/snapshots/ruff_linter__rules__flake8_bugbear__tests__preview__B006_B006_11.py.snap new file mode 100644 index 0000000000..2a57e5ad16 --- /dev/null +++ b/crates/ruff_linter/src/rules/flake8_bugbear/snapshots/ruff_linter__rules__flake8_bugbear__tests__preview__B006_B006_11.py.snap @@ -0,0 +1,44 @@ +--- +source: crates/ruff_linter/src/rules/flake8_bugbear/mod.rs +--- +B006 [*] Do not use mutable data structures for argument defaults + --> B006_11.py:6:30 + | +5 | # Should trigger B006 - nested function using mutable from outer scope +6 | def inner_function(items=my_list): + | ^^^^^^^ +7 | return items + | +help: Replace with `None`; initialize within function +3 | my_list = [1, 2, 3] # Assignment in outer function +4 | +5 | # Should trigger B006 - nested function using mutable from outer scope + - def inner_function(items=my_list): +6 + def inner_function(items=None): +7 + if items is None: +8 + items = my_list +9 | return items +10 | +11 | return inner_function +note: This is an unsafe fix and may change runtime behavior + +B006 [*] Do not use mutable data structures for argument defaults + --> B006_11.py:33:32 + | +32 | # Should trigger B006 - module-level assignment used as default +33 | def func_with_module_var(items=module_list): + | ^^^^^^^^^^^ +34 | return items + | +help: Replace with `None`; initialize within function +30 | module_list = [1, 2, 3] # Module-level assignment +31 | +32 | # Should trigger B006 - module-level assignment used as default + - def func_with_module_var(items=module_list): +33 + def func_with_module_var(items=None): +34 + if items is None: +35 + items = module_list +36 | return items +37 | +38 | +note: This is an unsafe fix and may change runtime behavior