Skip to content

Commit 117aeaf

Browse files
authored
B008: Whitelist more immutable function calls (#173)
1 parent 63239e0 commit 117aeaf

File tree

3 files changed

+37
-9
lines changed

3 files changed

+37
-9
lines changed

bugbear.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -694,6 +694,13 @@ def visit(self, node):
694694
"frozenset",
695695
"types.MappingProxyType",
696696
"MappingProxyType",
697+
"re.compile",
698+
"operator.attrgetter",
699+
"operator.itemgetter",
700+
"operator.methodcaller",
701+
"attrgetter",
702+
"itemgetter",
703+
"methodcaller",
697704
}
698705
B009 = Error(
699706
message=(

tests/b006_b008.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import collections
22
import logging
3+
import operator
4+
import re
35
import time
46
import types
7+
from operator import attrgetter, itemgetter, methodcaller
58
from types import MappingProxyType
69

710

@@ -98,3 +101,21 @@ def float_int_is_wrong(value=float(3)):
98101

99102
def float_str_not_inf_or_nan_is_wrong(value=float("3.14")):
100103
pass
104+
105+
106+
def re_compile_ok(value=re.compile("foo")):
107+
pass
108+
109+
110+
def operators_ok(
111+
v=operator.attrgetter("foo"),
112+
v2=operator.itemgetter("foo"),
113+
v3=operator.methodcaller("foo"),
114+
):
115+
pass
116+
117+
118+
def operators_ok_unqualified(
119+
v=attrgetter("foo"), v2=itemgetter("foo"), v3=methodcaller("foo")
120+
):
121+
pass

tests/test_bugbear.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -99,15 +99,15 @@ def test_b006_b008(self):
9999
self.assertEqual(
100100
errors,
101101
self.errors(
102-
B006(26, 24),
103-
B006(30, 29),
104-
B006(34, 19),
105-
B006(38, 19),
106-
B006(42, 31),
107-
B008(51, 38),
108-
B006(67, 32),
109-
B008(95, 29),
110-
B008(99, 44),
102+
B006(29, 24),
103+
B006(33, 29),
104+
B006(37, 19),
105+
B006(41, 19),
106+
B006(45, 31),
107+
B008(54, 38),
108+
B006(70, 32),
109+
B008(98, 29),
110+
B008(102, 44),
111111
),
112112
)
113113

0 commit comments

Comments
 (0)