Skip to content

Commit 82357df

Browse files
authored
feat(b038): Change B038 to B909 and make it optional (#456)
B038 lead to some false positives that stem from methods defined in the standard library that have the same name as mutating functions for container types like lists and dicts. Thus we decided to make this rule optional. See #455 for the related discussion.
1 parent 5c3f0bd commit 82357df

File tree

3 files changed

+39
-29
lines changed

3 files changed

+39
-29
lines changed

bugbear.py

+21-11
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,7 @@ def visit_For(self, node):
524524
self.check_for_b020(node)
525525
self.check_for_b023(node)
526526
self.check_for_b031(node)
527-
self.check_for_b038(node)
527+
self.check_for_b909(node)
528528
self.generic_visit(node)
529529

530530
def visit_AsyncFor(self, node):
@@ -1574,17 +1574,17 @@ def check(num_args, param_name):
15741574
elif node.func.attr == "split":
15751575
check(2, "maxsplit")
15761576

1577-
def check_for_b038(self, node: ast.For):
1577+
def check_for_b909(self, node: ast.For):
15781578
if isinstance(node.iter, ast.Name):
15791579
name = _to_name_str(node.iter)
15801580
elif isinstance(node.iter, ast.Attribute):
15811581
name = _to_name_str(node.iter)
15821582
else:
15831583
return
1584-
checker = B038Checker(name)
1584+
checker = B909Checker(name)
15851585
checker.visit(node.body)
15861586
for mutation in checker.mutations:
1587-
self.errors.append(B038(mutation.lineno, mutation.col_offset))
1587+
self.errors.append(B909(mutation.lineno, mutation.col_offset))
15881588

15891589

15901590
def compose_call_path(node):
@@ -1597,7 +1597,7 @@ def compose_call_path(node):
15971597
yield node.id
15981598

15991599

1600-
class B038Checker(ast.NodeVisitor):
1600+
class B909Checker(ast.NodeVisitor):
16011601
# https://docs.python.org/3/library/stdtypes.html#mutable-sequence-types
16021602
MUTATING_FUNCTIONS = (
16031603
"append",
@@ -2146,12 +2146,22 @@ def visit_Lambda(self, node):
21462146
" statement."
21472147
)
21482148
)
2149-
2150-
B950 = Error(message="B950 line too long ({} > {} characters)")
2151-
2152-
B038 = Error(
2149+
B909 = Error(
21532150
message=(
2154-
"B038 editing a loop's mutable iterable often leads to unexpected results/bugs"
2151+
"B909 editing a loop's mutable iterable often leads to unexpected results/bugs"
21552152
)
21562153
)
2157-
disabled_by_default = ["B901", "B902", "B903", "B904", "B905", "B906", "B908", "B950"]
2154+
B950 = Error(message="B950 line too long ({} > {} characters)")
2155+
2156+
2157+
disabled_by_default = [
2158+
"B901",
2159+
"B902",
2160+
"B903",
2161+
"B904",
2162+
"B905",
2163+
"B906",
2164+
"B908",
2165+
"B909",
2166+
"B950",
2167+
]

tests/b038.py tests/b909.py

File renamed without changes.

tests/test_bugbear.py

+18-18
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@
4646
B035,
4747
B036,
4848
B037,
49-
B038,
5049
B901,
5150
B902,
5251
B903,
@@ -55,6 +54,7 @@
5554
B906,
5655
B907,
5756
B908,
57+
B909,
5858
B950,
5959
BugBearChecker,
6060
BugBearVisitor,
@@ -969,27 +969,27 @@ def test_selfclean_test_bugbear(self):
969969
self.assertEqual(proc.stdout, b"")
970970
self.assertEqual(proc.stderr, b"")
971971

972-
def test_b038(self):
973-
filename = Path(__file__).absolute().parent / "b038.py"
974-
mock_options = Namespace(select=[], extend_select=["B038"])
972+
def test_b909(self):
973+
filename = Path(__file__).absolute().parent / "b909.py"
974+
mock_options = Namespace(select=[], extend_select=["B909"])
975975
bbc = BugBearChecker(filename=str(filename), options=mock_options)
976976
errors = list(bbc.run())
977977
print(errors)
978978
expected = [
979-
B038(11, 8),
980-
B038(26, 8),
981-
B038(27, 8),
982-
B038(41, 8),
983-
B038(47, 8),
984-
B038(56, 8),
985-
B038(57, 8),
986-
B038(58, 8),
987-
B038(59, 8),
988-
B038(60, 8),
989-
B038(61, 8),
990-
B038(62, 8),
991-
B038(63, 8),
992-
B038(74, 8),
979+
B909(11, 8),
980+
B909(26, 8),
981+
B909(27, 8),
982+
B909(41, 8),
983+
B909(47, 8),
984+
B909(56, 8),
985+
B909(57, 8),
986+
B909(58, 8),
987+
B909(59, 8),
988+
B909(60, 8),
989+
B909(61, 8),
990+
B909(62, 8),
991+
B909(63, 8),
992+
B909(74, 8),
993993
]
994994
self.assertEqual(errors, self.errors(*expected))
995995

0 commit comments

Comments
 (0)