Skip to content

Commit 24cfd9a

Browse files
MAINT: Apply ruff/flake8-bugbear rule B009
B009 Do not call `getattr` with a constant attribute value. It is not any safer than normal property access.
1 parent c192c09 commit 24cfd9a

File tree

10 files changed

+19
-19
lines changed

10 files changed

+19
-19
lines changed

benchmarks/benchmarks/bench_ma.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ def time_methods_getitem(self, margs, msize):
213213
mdat = self.nmxs
214214
elif msize == 'big':
215215
mdat = self.nmxl
216-
getattr(mdat, '__getitem__')(margs)
216+
mdat.__getitem__(margs)
217217

218218

219219
class MAMethodSetItem(Benchmark):
@@ -235,7 +235,7 @@ def time_methods_setitem(self, margs, mset, msize):
235235
mdat = self.nmxs
236236
elif msize == 'big':
237237
mdat = self.nmxl
238-
getattr(mdat, '__setitem__')(margs, mset)
238+
mdat.__setitem__(margs, mset)
239239

240240

241241
class Where(Benchmark):

benchmarks/benchmarks/bench_ufunc.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ def time_methods_getitem(self, margs, msize):
258258
mdat = self.xs
259259
elif msize == 'big':
260260
mdat = self.xl
261-
getattr(mdat, '__getitem__')(margs)
261+
mdat.__getitem__(margs)
262262

263263

264264
class NDArraySetItem(Benchmark):

numpy/_core/numeric.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2705,7 +2705,7 @@ def astype(x, dtype, /, *, copy=True, device=None):
27052705

27062706
def extend_all(module):
27072707
existing = set(__all__)
2708-
mall = getattr(module, '__all__')
2708+
mall = module.__all__
27092709
for a in mall:
27102710
if a not in existing:
27112711
__all__.append(a)

numpy/_core/tests/test_deprecations.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,7 @@ class TestMachAr(_DeprecationTestCase):
488488
warning_cls = DeprecationWarning
489489

490490
def test_deprecated_module(self):
491-
self.assert_deprecated(lambda: getattr(np._core, "MachAr"))
491+
self.assert_deprecated(lambda: np._core.MachAr)
492492

493493

494494
class TestQuantileInterpolationDeprecation(_DeprecationTestCase):

numpy/_core/tests/test_simd.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ def test_operators_logical(self):
160160
assert vor == data_or
161161

162162
data_xor = [a ^ b for a, b in zip(data_a, data_b)]
163-
vxor = getattr(self, "xor")(vdata_a, vdata_b)
163+
vxor = self.xor(vdata_a, vdata_b)
164164
assert vxor == data_xor
165165

166166
vnot = getattr(self, "not")(vdata_a)
@@ -171,15 +171,15 @@ def test_operators_logical(self):
171171
return
172172

173173
data_andc = [(a & ~b) & 0xFF for a, b in zip(data_a, data_b)]
174-
vandc = getattr(self, "andc")(vdata_a, vdata_b)
174+
vandc = self.andc(vdata_a, vdata_b)
175175
assert data_andc == vandc
176176

177177
data_orc = [(a | ~b) & 0xFF for a, b in zip(data_a, data_b)]
178-
vorc = getattr(self, "orc")(vdata_a, vdata_b)
178+
vorc = self.orc(vdata_a, vdata_b)
179179
assert data_orc == vorc
180180

181181
data_xnor = [~(a ^ b) & 0xFF for a, b in zip(data_a, data_b)]
182-
vxnor = getattr(self, "xnor")(vdata_a, vdata_b)
182+
vxnor = self.xnor(vdata_a, vdata_b)
183183
assert data_xnor == vxnor
184184

185185
def test_tobits(self):
@@ -1072,7 +1072,7 @@ def test_operators_logical(self):
10721072
if self.sfx not in ("u8"):
10731073
return
10741074
data_andc = [a & ~b for a, b in zip(data_cast_a, data_cast_b)]
1075-
vandc = cast(getattr(self, "andc")(vdata_a, vdata_b))
1075+
vandc = cast(self.andc(vdata_a, vdata_b))
10761076
assert vandc == data_andc
10771077

10781078
@pytest.mark.parametrize("intrin", ["any", "all"])

numpy/_core/tests/test_ufunc.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
UNARY_OBJECT_UFUNCS = [uf for uf in UNARY_UFUNCS if "O->O" in uf.types]
2828

2929
# Remove functions that do not support `floats`
30-
UNARY_OBJECT_UFUNCS.remove(getattr(np, 'bitwise_count'))
30+
UNARY_OBJECT_UFUNCS.remove(np.bitwise_count)
3131

3232

3333
class TestUfuncKwargs:

numpy/_core/tests/test_umath_accuracy.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
UNARY_OBJECT_UFUNCS = [uf for uf in UNARY_UFUNCS if "O->O" in uf.types]
1414

1515
# Remove functions that do not support `floats`
16-
UNARY_OBJECT_UFUNCS.remove(getattr(np, 'invert'))
17-
UNARY_OBJECT_UFUNCS.remove(getattr(np, 'bitwise_count'))
16+
UNARY_OBJECT_UFUNCS.remove(np.invert)
17+
UNARY_OBJECT_UFUNCS.remove(np.bitwise_count)
1818

1919
IS_AVX = __cpu_features__.get('AVX512F', False) or \
2020
(__cpu_features__.get('FMA3', False) and __cpu_features__.get('AVX2', False))

numpy/f2py/tests/test_array_from_pyobj.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,12 +191,12 @@ def _init(self, name):
191191

192192
if self.NAME == 'CHARACTER':
193193
info = c_names_dict[self.NAME]
194-
self.type_num = getattr(wrap, 'NPY_STRING')
194+
self.type_num = wrap.NPY_STRING
195195
self.elsize = 1
196196
self.dtype = np.dtype('c')
197197
elif self.NAME.startswith('STRING'):
198198
info = c_names_dict[self.NAME[:6]]
199-
self.type_num = getattr(wrap, 'NPY_STRING')
199+
self.type_num = wrap.NPY_STRING
200200
self.elsize = int(self.NAME[6:] or 0)
201201
self.dtype = np.dtype(f'S{self.elsize}')
202202
else:

numpy/f2py/tests/test_callback.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ def callback(code):
9494
else:
9595
return 1
9696

97-
f = getattr(self.module, "string_callback")
97+
f = self.module.string_callback
9898
r = f(callback)
9999
assert r == 0
100100

@@ -115,7 +115,7 @@ def callback(cu, lencu):
115115
return 3
116116
return 0
117117

118-
f = getattr(self.module, "string_callback_array")
118+
f = self.module.string_callback_array
119119
for cu in [cu1, cu2, cu3]:
120120
res = f(callback, cu, cu.size)
121121
assert res == 0

numpy/f2py/tests/test_docs.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@ class TestDocAdvanced(util.F2PyTest):
3434
_path('ftype.f')]
3535

3636
def test_asterisk1(self):
37-
foo = getattr(self.module, 'foo1')
37+
foo = self.module.foo1
3838
assert_equal(foo(), b'123456789A12')
3939

4040
def test_asterisk2(self):
41-
foo = getattr(self.module, 'foo2')
41+
foo = self.module.foo2
4242
assert_equal(foo(2), b'12')
4343
assert_equal(foo(12), b'123456789A12')
4444
assert_equal(foo(20), b'123456789A123456789B')

0 commit comments

Comments
 (0)