Skip to content

Commit f3cd65a

Browse files
STYLE: Specify bare exceptions in pandas/tests (pandas-dev#23370)
1 parent cd7fd0f commit f3cd65a

File tree

11 files changed

+23
-24
lines changed

11 files changed

+23
-24
lines changed

.pep8speaks.yml

-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ pycodestyle:
1313
- W503, # line break before binary operator
1414
- W504, # line break after binary operator
1515
- E402, # module level import not at top of file
16-
- E722, # do not use bare except
1716
- E731, # do not assign a lambda expression, use a def
1817
- C406, # Unnecessary list literal - rewrite as a dict literal.
1918
- C408, # Unnecessary dict call - rewrite as a literal.

doc/source/conf.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -586,7 +586,7 @@ def linkcode_resolve(domain, info):
586586
for part in fullname.split('.'):
587587
try:
588588
obj = getattr(obj, part)
589-
except:
589+
except AttributeError:
590590
return None
591591

592592
try:
@@ -595,14 +595,14 @@ def linkcode_resolve(domain, info):
595595
fn = inspect.getsourcefile(inspect.unwrap(obj))
596596
else:
597597
fn = inspect.getsourcefile(obj)
598-
except:
598+
except TypeError:
599599
fn = None
600600
if not fn:
601601
return None
602602

603603
try:
604604
source, lineno = inspect.getsourcelines(obj)
605-
except:
605+
except OSError:
606606
lineno = None
607607

608608
if lineno:

pandas/tests/indexing/common.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ def get_result(self, obj, method, key, axis):
156156
with catch_warnings(record=True):
157157
try:
158158
xp = getattr(obj, method).__getitem__(_axify(obj, key, axis))
159-
except:
159+
except AttributeError:
160160
xp = getattr(obj, method).__getitem__(key)
161161

162162
return xp
@@ -219,7 +219,7 @@ def _print(result, error=None):
219219

220220
try:
221221
xp = self.get_result(obj, method2, k2, a)
222-
except:
222+
except Exception:
223223
result = 'no comp'
224224
_print(result)
225225
return

pandas/tests/io/formats/test_format.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ def has_horizontally_truncated_repr(df):
7070
try: # Check header row
7171
fst_line = np.array(repr(df).splitlines()[0].split())
7272
cand_col = np.where(fst_line == '...')[0][0]
73-
except:
73+
except IndexError:
7474
return False
7575
# Make sure each row has this ... in the same place
7676
r = repr(df)
@@ -459,7 +459,7 @@ def test_to_string_repr_unicode(self):
459459
for line in rs[1:]:
460460
try:
461461
line = line.decode(get_option("display.encoding"))
462-
except:
462+
except AttributeError:
463463
pass
464464
if not line.startswith('dtype:'):
465465
assert len(line) == line_len

pandas/tests/io/test_pytables.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,15 @@ def safe_remove(path):
5151
if path is not None:
5252
try:
5353
os.remove(path)
54-
except:
54+
except OSError:
5555
pass
5656

5757

5858
def safe_close(store):
5959
try:
6060
if store is not None:
6161
store.close()
62-
except:
62+
except IOError:
6363
pass
6464

6565

@@ -117,7 +117,7 @@ def _maybe_remove(store, key):
117117
no content from previous tests using the same table name."""
118118
try:
119119
store.remove(key)
120-
except:
120+
except (ValueError, KeyError):
121121
pass
122122

123123

@@ -4601,7 +4601,7 @@ def do_copy(f, new_f=None, keys=None,
46014601
safe_close(tstore)
46024602
try:
46034603
os.close(fd)
4604-
except:
4604+
except (OSError, ValueError):
46054605
pass
46064606
safe_remove(new_f)
46074607

pandas/tests/io/test_sql.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -1815,6 +1815,7 @@ def test_default_type_conversion(self):
18151815
assert issubclass(df.BoolColWithNull.dtype.type, np.floating)
18161816

18171817
def test_read_procedure(self):
1818+
import pymysql
18181819
# see GH7324. Although it is more an api test, it is added to the
18191820
# mysql tests as sqlite does not have stored procedures
18201821
df = DataFrame({'a': [1, 2, 3], 'b': [0.1, 0.2, 0.3]})
@@ -1833,7 +1834,7 @@ def test_read_procedure(self):
18331834
try:
18341835
r1 = connection.execute(proc) # noqa
18351836
trans.commit()
1836-
except:
1837+
except pymysql.Error:
18371838
trans.rollback()
18381839
raise
18391840

@@ -2418,7 +2419,7 @@ def setup_class(cls):
24182419
# No real user should allow root access with a blank password.
24192420
pymysql.connect(host='localhost', user='root', passwd='',
24202421
db='pandas_nosetest')
2421-
except:
2422+
except pymysql.Error:
24222423
pass
24232424
else:
24242425
return
@@ -2445,7 +2446,7 @@ def setup_method(self, request, datapath):
24452446
# No real user should allow root access with a blank password.
24462447
self.conn = pymysql.connect(host='localhost', user='root',
24472448
passwd='', db='pandas_nosetest')
2448-
except:
2449+
except pymysql.Error:
24492450
pass
24502451
else:
24512452
return

pandas/tests/test_multilevel.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1376,7 +1376,7 @@ def f():
13761376

13771377
try:
13781378
df = f()
1379-
except:
1379+
except ValueError:
13801380
pass
13811381
assert (df['foo', 'one'] == 0).all()
13821382

pandas/tests/test_nanops.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -141,12 +141,12 @@ def _coerce_tds(targ, res):
141141
if axis != 0 and hasattr(
142142
targ, 'shape') and targ.ndim and targ.shape != res.shape:
143143
res = np.split(res, [targ.shape[0]], axis=0)[0]
144-
except:
144+
except (ValueError, IndexError):
145145
targ, res = _coerce_tds(targ, res)
146146

147147
try:
148148
tm.assert_almost_equal(targ, res, check_dtype=check_dtype)
149-
except:
149+
except AssertionError:
150150

151151
# handle timedelta dtypes
152152
if hasattr(targ, 'dtype') and targ.dtype == 'm8[ns]':
@@ -167,11 +167,11 @@ def _coerce_tds(targ, res):
167167
else:
168168
try:
169169
res = res.astype('c16')
170-
except:
170+
except RuntimeError:
171171
res = res.astype('f8')
172172
try:
173173
targ = targ.astype('c16')
174-
except:
174+
except RuntimeError:
175175
targ = targ.astype('f8')
176176
# there should never be a case where numpy returns an object
177177
# but nanops doesn't, so make that an exception

pandas/tests/test_panel.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -335,13 +335,13 @@ def check_op(op, name):
335335
for op in ops:
336336
try:
337337
check_op(getattr(operator, op), op)
338-
except:
338+
except AttributeError:
339339
pprint_thing("Failing operation: %r" % op)
340340
raise
341341
if compat.PY3:
342342
try:
343343
check_op(operator.truediv, 'div')
344-
except:
344+
except AttributeError:
345345
pprint_thing("Failing operation: %r" % 'div')
346346
raise
347347

pandas/tests/test_strings.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2637,7 +2637,7 @@ def test_slice(self):
26372637
expected = Series([s[start:stop:step] if not isna(s) else NA
26382638
for s in values])
26392639
tm.assert_series_equal(result, expected)
2640-
except:
2640+
except IndexError:
26412641
print('failed on %s:%s:%s' % (start, stop, step))
26422642
raise
26432643

setup.cfg

-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ ignore =
1717
W503, # line break before binary operator
1818
W504, # line break after binary operator
1919
E402, # module level import not at top of file
20-
E722, # do not use bare except
2120
E731, # do not assign a lambda expression, use a def
2221
C406, # Unnecessary list literal - rewrite as a dict literal.
2322
C408, # Unnecessary dict call - rewrite as a literal.

0 commit comments

Comments
 (0)