Skip to content

Commit 1367cac

Browse files
authored
STY: enable subset of flake8-bugbear (#40743)
1 parent dc207e2 commit 1367cac

File tree

12 files changed

+26
-16
lines changed

12 files changed

+26
-16
lines changed

.pre-commit-config.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ repos:
3636
rev: 3.9.0
3737
hooks:
3838
- id: flake8
39-
additional_dependencies: [flake8-comprehensions>=3.1.0]
39+
additional_dependencies: [flake8-comprehensions>=3.1.0, flake8-bugbear>=21.3.2]
4040
- id: flake8
4141
name: flake8 (cython)
4242
types: [cython]

environment.yml

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ dependencies:
2121
- black=20.8b1
2222
- cpplint
2323
- flake8
24+
- flake8-bugbear>=21.3.2 # used by flake8, find likely bugs
2425
- flake8-comprehensions>=3.1.0 # used by flake8, linting of unnecessary comprehensions
2526
- isort>=5.2.1 # check that imports are in the right order
2627
- mypy=0.812

pandas/core/common.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ def get_callable_name(obj):
336336
if isinstance(obj, partial):
337337
return get_callable_name(obj.func)
338338
# fall back to class name
339-
if hasattr(obj, "__call__"):
339+
if callable(obj):
340340
return type(obj).__name__
341341
# everything failed (probably because the argument
342342
# wasn't actually callable); we return None

pandas/core/dtypes/common.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1576,7 +1576,7 @@ def _is_dtype(arr_or_dtype, condition) -> bool:
15761576
return False
15771577
try:
15781578
dtype = get_dtype(arr_or_dtype)
1579-
except (TypeError, ValueError, UnicodeEncodeError):
1579+
except (TypeError, ValueError):
15801580
return False
15811581
return condition(dtype)
15821582

@@ -1651,7 +1651,7 @@ def _is_dtype_type(arr_or_dtype, condition) -> bool:
16511651

16521652
try:
16531653
tipo = pandas_dtype(arr_or_dtype).type
1654-
except (TypeError, ValueError, UnicodeEncodeError):
1654+
except (TypeError, ValueError):
16551655
if is_scalar(arr_or_dtype):
16561656
return condition(type(None))
16571657

pandas/core/reshape/pivot.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,7 @@ def _convert_by(by):
458458
elif (
459459
is_scalar(by)
460460
or isinstance(by, (np.ndarray, Index, ABCSeries, Grouper))
461-
or hasattr(by, "__call__")
461+
or callable(by)
462462
):
463463
by = [by]
464464
else:

pandas/tests/frame/methods/test_to_csv.py

+1-4
Original file line numberDiff line numberDiff line change
@@ -773,10 +773,7 @@ def test_to_csv_dups_cols(self):
773773
[df_float, df_int, df_bool, df_object, df_dt], axis=1, ignore_index=True
774774
)
775775

776-
cols = []
777-
for i in range(5):
778-
cols.extend([0, 1, 2])
779-
df.columns = cols
776+
df.columns = [0, 1, 2] * 5
780777

781778
with tm.ensure_clean() as filename:
782779
df.to_csv(filename)

pandas/tests/indexes/categorical/test_indexing.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -244,9 +244,9 @@ def test_get_indexer_non_unique(self):
244244
for indexer in [idx2, list("abf"), Index(list("abf"))]:
245245
msg = "Reindexing only valid with uniquely valued Index objects"
246246
with pytest.raises(InvalidIndexError, match=msg):
247-
idx1.get_indexer(idx2)
247+
idx1.get_indexer(indexer)
248248

249-
r1, _ = idx1.get_indexer_non_unique(idx2)
249+
r1, _ = idx1.get_indexer_non_unique(indexer)
250250
expected = np.array([0, 1, 2, -1], dtype=np.intp)
251251
tm.assert_almost_equal(r1, expected)
252252

pandas/tests/indexes/datetimes/test_indexing.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ def test_where_other(self):
173173
i = date_range("20130101", periods=3, tz="US/Eastern")
174174

175175
for arr in [np.nan, pd.NaT]:
176-
result = i.where(notna(i), other=np.nan)
176+
result = i.where(notna(i), other=arr)
177177
expected = i
178178
tm.assert_index_equal(result, expected)
179179

pandas/tests/io/sas/test_sas7bdat.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,11 @@ def test_iterator_loop(self):
9595
# github #13654
9696
for j in 0, 1:
9797
for k in self.test_ix[j]:
98-
for chunksize in 3, 5, 10, 11:
98+
for chunksize in (3, 5, 10, 11):
9999
fname = os.path.join(self.dirpath, f"test{k}.sas7bdat")
100-
with pd.read_sas(fname, chunksize=10, encoding="utf-8") as rdr:
100+
with pd.read_sas(
101+
fname, chunksize=chunksize, encoding="utf-8"
102+
) as rdr:
101103
y = 0
102104
for x in rdr:
103105
y += x.shape[0]

pandas/tests/resample/test_datetime_index.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1342,7 +1342,7 @@ def test_resample_nunique():
13421342
assert expected.name == "ID"
13431343

13441344
for t in [r, g]:
1345-
result = r.ID.nunique()
1345+
result = t.ID.nunique()
13461346
tm.assert_series_equal(result, expected)
13471347

13481348
result = df.ID.resample("D").nunique()

requirements-dev.txt

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ cython>=0.29.21
99
black==20.8b1
1010
cpplint
1111
flake8
12+
flake8-bugbear>=21.3.2
1213
flake8-comprehensions>=3.1.0
1314
isort>=5.2.1
1415
mypy==0.812

setup.cfg

+10-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,16 @@ ignore =
7676
W504, # line break after binary operator
7777
E402, # module level import not at top of file
7878
E731, # do not assign a lambda expression, use a def
79-
S001 # found modulo formatter (incorrect picks up mod operations)
79+
S001, # found modulo formatter (incorrect picks up mod operations)
80+
B005, # controversial
81+
B006, # controversial
82+
B007, # controversial
83+
B008, # controversial
84+
B009, # setattr is used to side-step mypy
85+
B010, # getattr is used to side-step mypy
86+
B011, # tests use assert False
87+
B015, # tests use comparisons but not their returned value
88+
B301 # false positives
8089
exclude =
8190
doc/sphinxext/*.py,
8291
doc/build/*.py,

0 commit comments

Comments
 (0)