Skip to content

Commit 759e928

Browse files
Remove bare exceptions from 'pandas/tests' to decrease flake8 E722 warnings
1 parent 14598c6 commit 759e928

File tree

8 files changed

+23
-19
lines changed

8 files changed

+23
-19
lines changed

pandas/tests/indexing/common.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ def get_result(self, obj, method, key, axis):
151151
with catch_warnings(record=True):
152152
try:
153153
xp = getattr(obj, method).__getitem__(_axify(obj, key, axis))
154-
except:
154+
except AttributeError:
155155
xp = getattr(obj, method).__getitem__(key)
156156

157157
return xp
@@ -214,7 +214,7 @@ def _print(result, error=None):
214214

215215
try:
216216
xp = self.get_result(obj, method2, k2, a)
217-
except:
217+
except Exception:
218218
result = 'no comp'
219219
_print(result)
220220
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)
@@ -452,7 +452,7 @@ def test_to_string_repr_unicode(self):
452452
for line in rs[1:]:
453453
try:
454454
line = line.decode(get_option("display.encoding"))
455-
except:
455+
except Exception:
456456
pass
457457
if not line.startswith('dtype:'):
458458
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 Exception:
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 Exception:
121121
pass
122122

123123

@@ -4621,7 +4621,7 @@ def do_copy(f, new_f=None, keys=None,
46214621
safe_close(tstore)
46224622
try:
46234623
os.close(fd)
4624-
except:
4624+
except ValueError:
46254625
pass
46264626
safe_remove(new_f)
46274627

pandas/tests/io/test_sql.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -1787,10 +1787,12 @@ def test_read_procedure(self):
17871787

17881788
connection = self.conn.connect()
17891789
trans = connection.begin()
1790+
1791+
from pymysql.err import Error
17901792
try:
17911793
r1 = connection.execute(proc) # noqa
17921794
trans.commit()
1793-
except:
1795+
except Error:
17941796
trans.rollback()
17951797
raise
17961798

@@ -2370,12 +2372,13 @@ def setup_class(cls):
23702372

23712373
# test connection
23722374
import pymysql
2375+
from pymysql.err import Error
23732376
try:
23742377
# Try Travis defaults.
23752378
# No real user should allow root access with a blank password.
23762379
pymysql.connect(host='localhost', user='root', passwd='',
23772380
db='pandas_nosetest')
2378-
except:
2381+
except Error:
23792382
pass
23802383
else:
23812384
return
@@ -2397,12 +2400,13 @@ def setup_class(cls):
23972400
def setup_method(self, request, datapath):
23982401
_skip_if_no_pymysql()
23992402
import pymysql
2403+
from pymysql.err import Error
24002404
try:
24012405
# Try Travis defaults.
24022406
# No real user should allow root access with a blank password.
24032407
self.conn = pymysql.connect(host='localhost', user='root',
24042408
passwd='', db='pandas_nosetest')
2405-
except:
2409+
except Error:
24062410
pass
24072411
else:
24082412
return

pandas/tests/test_multilevel.py

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

13761376
try:
13771377
df = f()
1378-
except:
1378+
except Exception:
13791379
pass
13801380
assert (df['foo', 'one'] == 0).all()
13811381

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 Exception:
171171
res = res.astype('f8')
172172
try:
173173
targ = targ.astype('c16')
174-
except:
174+
except Exception:
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, KeyError):
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, KeyError):
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
@@ -2660,7 +2660,7 @@ def test_slice(self):
26602660
expected = Series([s[start:stop:step] if not isna(s) else NA
26612661
for s in values])
26622662
tm.assert_series_equal(result, expected)
2663-
except:
2663+
except IndexError:
26642664
print('failed on %s:%s:%s' % (start, stop, step))
26652665
raise
26662666

0 commit comments

Comments
 (0)