From 49abc80d0cb36c823792b2f3ca84c7bfc47114a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torsten=20W=C3=B6rtwein?= Date: Mon, 22 Mar 2021 21:18:37 -0400 Subject: [PATCH] STY: enable subset of flake8-bugbear --- .pre-commit-config.yaml | 2 +- environment.yml | 1 + pandas/core/common.py | 2 +- pandas/core/dtypes/common.py | 4 ++-- pandas/core/reshape/pivot.py | 2 +- pandas/tests/frame/methods/test_to_csv.py | 5 +---- pandas/tests/indexes/categorical/test_indexing.py | 4 ++-- pandas/tests/indexes/datetimes/test_indexing.py | 2 +- pandas/tests/io/sas/test_sas7bdat.py | 6 ++++-- pandas/tests/resample/test_datetime_index.py | 2 +- requirements-dev.txt | 1 + setup.cfg | 11 ++++++++++- 12 files changed, 26 insertions(+), 16 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 5bfceec6605c0..b8524a302f4c9 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -36,7 +36,7 @@ repos: rev: 3.9.0 hooks: - id: flake8 - additional_dependencies: [flake8-comprehensions>=3.1.0] + additional_dependencies: [flake8-comprehensions>=3.1.0, flake8-bugbear>=21.3.2] - id: flake8 name: flake8 (cython) types: [cython] diff --git a/environment.yml b/environment.yml index 1259d0dd4ae44..feea3445cb4fe 100644 --- a/environment.yml +++ b/environment.yml @@ -21,6 +21,7 @@ dependencies: - black=20.8b1 - cpplint - flake8 + - flake8-bugbear>=21.3.2 # used by flake8, find likely bugs - flake8-comprehensions>=3.1.0 # used by flake8, linting of unnecessary comprehensions - isort>=5.2.1 # check that imports are in the right order - mypy=0.812 diff --git a/pandas/core/common.py b/pandas/core/common.py index 6790a3e54192a..98606f5d3d240 100644 --- a/pandas/core/common.py +++ b/pandas/core/common.py @@ -336,7 +336,7 @@ def get_callable_name(obj): if isinstance(obj, partial): return get_callable_name(obj.func) # fall back to class name - if hasattr(obj, "__call__"): + if callable(obj): return type(obj).__name__ # everything failed (probably because the argument # wasn't actually callable); we return None diff --git a/pandas/core/dtypes/common.py b/pandas/core/dtypes/common.py index b9e785ff2f887..3d8d189046d8a 100644 --- a/pandas/core/dtypes/common.py +++ b/pandas/core/dtypes/common.py @@ -1576,7 +1576,7 @@ def _is_dtype(arr_or_dtype, condition) -> bool: return False try: dtype = get_dtype(arr_or_dtype) - except (TypeError, ValueError, UnicodeEncodeError): + except (TypeError, ValueError): return False return condition(dtype) @@ -1651,7 +1651,7 @@ def _is_dtype_type(arr_or_dtype, condition) -> bool: try: tipo = pandas_dtype(arr_or_dtype).type - except (TypeError, ValueError, UnicodeEncodeError): + except (TypeError, ValueError): if is_scalar(arr_or_dtype): return condition(type(None)) diff --git a/pandas/core/reshape/pivot.py b/pandas/core/reshape/pivot.py index 0c0b37791f883..fa0e5c422501a 100644 --- a/pandas/core/reshape/pivot.py +++ b/pandas/core/reshape/pivot.py @@ -458,7 +458,7 @@ def _convert_by(by): elif ( is_scalar(by) or isinstance(by, (np.ndarray, Index, ABCSeries, Grouper)) - or hasattr(by, "__call__") + or callable(by) ): by = [by] else: diff --git a/pandas/tests/frame/methods/test_to_csv.py b/pandas/tests/frame/methods/test_to_csv.py index aed784a6e4c3c..3b2668aea001c 100644 --- a/pandas/tests/frame/methods/test_to_csv.py +++ b/pandas/tests/frame/methods/test_to_csv.py @@ -773,10 +773,7 @@ def test_to_csv_dups_cols(self): [df_float, df_int, df_bool, df_object, df_dt], axis=1, ignore_index=True ) - cols = [] - for i in range(5): - cols.extend([0, 1, 2]) - df.columns = cols + df.columns = [0, 1, 2] * 5 with tm.ensure_clean() as filename: df.to_csv(filename) diff --git a/pandas/tests/indexes/categorical/test_indexing.py b/pandas/tests/indexes/categorical/test_indexing.py index 490a68233367a..0e0849fdb8dcf 100644 --- a/pandas/tests/indexes/categorical/test_indexing.py +++ b/pandas/tests/indexes/categorical/test_indexing.py @@ -244,9 +244,9 @@ def test_get_indexer_non_unique(self): for indexer in [idx2, list("abf"), Index(list("abf"))]: msg = "Reindexing only valid with uniquely valued Index objects" with pytest.raises(InvalidIndexError, match=msg): - idx1.get_indexer(idx2) + idx1.get_indexer(indexer) - r1, _ = idx1.get_indexer_non_unique(idx2) + r1, _ = idx1.get_indexer_non_unique(indexer) expected = np.array([0, 1, 2, -1], dtype=np.intp) tm.assert_almost_equal(r1, expected) diff --git a/pandas/tests/indexes/datetimes/test_indexing.py b/pandas/tests/indexes/datetimes/test_indexing.py index d29d4647f4753..3da6414332cb8 100644 --- a/pandas/tests/indexes/datetimes/test_indexing.py +++ b/pandas/tests/indexes/datetimes/test_indexing.py @@ -173,7 +173,7 @@ def test_where_other(self): i = date_range("20130101", periods=3, tz="US/Eastern") for arr in [np.nan, pd.NaT]: - result = i.where(notna(i), other=np.nan) + result = i.where(notna(i), other=arr) expected = i tm.assert_index_equal(result, expected) diff --git a/pandas/tests/io/sas/test_sas7bdat.py b/pandas/tests/io/sas/test_sas7bdat.py index bc9dc8e966e33..e20d78effa931 100644 --- a/pandas/tests/io/sas/test_sas7bdat.py +++ b/pandas/tests/io/sas/test_sas7bdat.py @@ -95,9 +95,11 @@ def test_iterator_loop(self): # github #13654 for j in 0, 1: for k in self.test_ix[j]: - for chunksize in 3, 5, 10, 11: + for chunksize in (3, 5, 10, 11): fname = os.path.join(self.dirpath, f"test{k}.sas7bdat") - with pd.read_sas(fname, chunksize=10, encoding="utf-8") as rdr: + with pd.read_sas( + fname, chunksize=chunksize, encoding="utf-8" + ) as rdr: y = 0 for x in rdr: y += x.shape[0] diff --git a/pandas/tests/resample/test_datetime_index.py b/pandas/tests/resample/test_datetime_index.py index ea8b8fc7aa6a2..71e6aa38d60e5 100644 --- a/pandas/tests/resample/test_datetime_index.py +++ b/pandas/tests/resample/test_datetime_index.py @@ -1342,7 +1342,7 @@ def test_resample_nunique(): assert expected.name == "ID" for t in [r, g]: - result = r.ID.nunique() + result = t.ID.nunique() tm.assert_series_equal(result, expected) result = df.ID.resample("D").nunique() diff --git a/requirements-dev.txt b/requirements-dev.txt index 1817d79f96139..349b176253acb 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -9,6 +9,7 @@ cython>=0.29.21 black==20.8b1 cpplint flake8 +flake8-bugbear>=21.3.2 flake8-comprehensions>=3.1.0 isort>=5.2.1 mypy==0.812 diff --git a/setup.cfg b/setup.cfg index a0b6a0cdfc260..7165fc2275dc0 100644 --- a/setup.cfg +++ b/setup.cfg @@ -76,7 +76,16 @@ ignore = W504, # line break after binary operator E402, # module level import not at top of file E731, # do not assign a lambda expression, use a def - S001 # found modulo formatter (incorrect picks up mod operations) + S001, # found modulo formatter (incorrect picks up mod operations) + B005, # controversial + B006, # controversial + B007, # controversial + B008, # controversial + B009, # setattr is used to side-step mypy + B010, # getattr is used to side-step mypy + B011, # tests use assert False + B015, # tests use comparisons but not their returned value + B301 # false positives exclude = doc/sphinxext/*.py, doc/build/*.py,