Skip to content

Commit 86c2668

Browse files
authored
Enable get-attr-with-constant (B009) (#60862)
* Enable get-attr-with-constant (B009) * Enable get-attr-with-constant (B009)
1 parent 70edaa0 commit 86c2668

File tree

8 files changed

+10
-14
lines changed

8 files changed

+10
-14
lines changed

pandas/core/arraylike.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ def array_ufunc(self, ufunc: np.ufunc, method: str, *inputs: Any, **kwargs: Any)
329329
reconstruct_axes = dict(zip(self._AXIS_ORDERS, self.axes))
330330

331331
if self.ndim == 1:
332-
names = {getattr(x, "name") for x in inputs if hasattr(x, "name")}
332+
names = {x.name for x in inputs if hasattr(x, "name")}
333333
name = names.pop() if len(names) == 1 else None
334334
reconstruct_kwargs = {"name": name}
335335
else:

pandas/core/common.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ def is_full_slice(obj, line: int) -> bool:
359359
def get_callable_name(obj):
360360
# typical case has name
361361
if hasattr(obj, "__name__"):
362-
return getattr(obj, "__name__")
362+
return obj.__name__
363363
# some objects don't; could recurse
364364
if isinstance(obj, partial):
365365
return get_callable_name(obj.func)

pandas/io/formats/style.py

+4-6
Original file line numberDiff line numberDiff line change
@@ -2021,7 +2021,7 @@ def apply(
20212021
more details.
20222022
"""
20232023
self._todo.append(
2024-
(lambda instance: getattr(instance, "_apply"), (func, axis, subset), kwargs)
2024+
(lambda instance: instance._apply, (func, axis, subset), kwargs)
20252025
)
20262026
return self
20272027

@@ -2128,7 +2128,7 @@ def apply_index(
21282128
"""
21292129
self._todo.append(
21302130
(
2131-
lambda instance: getattr(instance, "_apply_index"),
2131+
lambda instance: instance._apply_index,
21322132
(func, axis, level, "apply"),
21332133
kwargs,
21342134
)
@@ -2157,7 +2157,7 @@ def map_index(
21572157
) -> Styler:
21582158
self._todo.append(
21592159
(
2160-
lambda instance: getattr(instance, "_apply_index"),
2160+
lambda instance: instance._apply_index,
21612161
(func, axis, level, "map"),
21622162
kwargs,
21632163
)
@@ -2230,9 +2230,7 @@ def map(self, func: Callable, subset: Subset | None = None, **kwargs) -> Styler:
22302230
See `Table Visualization <../../user_guide/style.ipynb>`_ user guide for
22312231
more details.
22322232
"""
2233-
self._todo.append(
2234-
(lambda instance: getattr(instance, "_map"), (func, subset), kwargs)
2235-
)
2233+
self._todo.append((lambda instance: instance._map, (func, subset), kwargs))
22362234
return self
22372235

22382236
def set_table_attributes(self, attributes: str) -> Styler:

pandas/tests/generic/test_finalize.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -644,7 +644,7 @@ def test_timedelta_methods(method):
644644
operator.methodcaller("add_categories", ["c"]),
645645
operator.methodcaller("as_ordered"),
646646
operator.methodcaller("as_unordered"),
647-
lambda x: getattr(x, "codes"),
647+
lambda x: x.codes,
648648
operator.methodcaller("remove_categories", "a"),
649649
operator.methodcaller("remove_unused_categories"),
650650
operator.methodcaller("rename_categories", {"a": "A", "b": "B"}),

pandas/tests/groupby/test_apply.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1390,7 +1390,7 @@ def test_empty_df(method, op):
13901390
# GH 47985
13911391
empty_df = DataFrame({"a": [], "b": []})
13921392
gb = empty_df.groupby("a", group_keys=True)
1393-
group = getattr(gb, "b")
1393+
group = gb.b
13941394

13951395
result = getattr(group, method)(op)
13961396
expected = Series(

pandas/tests/groupby/test_groupby.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ def test_attr_wrapper(ts):
264264
# make sure raises error
265265
msg = "'SeriesGroupBy' object has no attribute 'foo'"
266266
with pytest.raises(AttributeError, match=msg):
267-
getattr(grouped, "foo")
267+
grouped.foo
268268

269269

270270
def test_frame_groupby(tsframe):

pandas/tests/io/pytables/test_store.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ def test_getattr(setup_path):
311311
# test attribute access
312312
result = store.a
313313
tm.assert_series_equal(result, s)
314-
result = getattr(store, "a")
314+
result = store.a
315315
tm.assert_series_equal(result, s)
316316

317317
df = DataFrame(

pyproject.toml

-2
Original file line numberDiff line numberDiff line change
@@ -272,8 +272,6 @@ ignore = [
272272
"B007",
273273
# controversial
274274
"B008",
275-
# setattr is used to side-step mypy
276-
"B009",
277275
# getattr is used to side-step mypy
278276
"B010",
279277
# tests use comparisons but not their returned value

0 commit comments

Comments
 (0)