-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
Remove bare exceptions from 'pandas/tests' to decrease flake8 E722 warnings #22882
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Remove bare exceptions from 'pandas/tests' to decrease flake8 E722 warnings #22882
Conversation
Hello @HaraldNordgren! Thanks for submitting the PR.
|
4f7a7e6
to
37c6d0b
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The point of this is to be explicit about which exceptions need to be caught, so except Exception
isn't what we'd be looking for. Can you review those in more detail and see if we can choose a more specific type of exception?
pandas/tests/test_nanops.py
Outdated
@@ -141,12 +141,12 @@ def _coerce_tds(targ, res): | |||
if axis != 0 and hasattr( | |||
targ, 'shape') and targ.ndim and targ.shape != res.shape: | |||
res = np.split(res, [targ.shape[0]], axis=0)[0] | |||
except: | |||
except (np.ValueError, IndexError): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is np.ValueError different from ValueError?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, my mistake. numpy.ValueError
does not exists. Changed it to ValueError
.
91d922a
to
a2d30d5
Compare
Codecov Report
@@ Coverage Diff @@
## master #22882 +/- ##
=======================================
Coverage 92.18% 92.18%
=======================================
Files 169 169
Lines 50830 50830
=======================================
Hits 46860 46860
Misses 3970 3970
Continue to review full report at Codecov.
|
bb6f71c
to
b3a4dcc
Compare
b3a4dcc
to
759e928
Compare
@WillAyd Added some more specific errors. Do you think it looks better or would you like to go deeper? In cases like https://github.com/pandas-dev/pandas/pull/22882/files#diff-45436d9cd11edcc7297e0dc3d699a328R217 I'm not sure which exceptions to catch? E.g. When changing it to |
@HaraldNordgren I still think it's worthwhile to go deeper. What failures are you getting in your aforementioned example? Are the errors not clueing you in further on what exceptions to catch? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pls be specific on the exceptions rather than using Exception. IOW only if we have a long list of exceptions to catch will we use Exception (so it should be rare)
@@ -214,7 +214,7 @@ def _print(result, error=None): | |||
|
|||
try: | |||
xp = self.get_result(obj, method2, k2, a) | |||
except: | |||
except Exception: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you be more specific here
@@ -452,7 +452,7 @@ def test_to_string_repr_unicode(self): | |||
for line in rs[1:]: | |||
try: | |||
line = line.decode(get_option("display.encoding")) | |||
except: | |||
except Exception: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same
pass | ||
|
||
|
||
def safe_close(store): | ||
try: | ||
if store is not None: | ||
store.close() | ||
except: | ||
except Exception: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same
@HaraldNordgren can you make the requested fixes? |
can you merge master and update |
I think this is fixing the same problems than #23370, which is ready to be merged. Closing as duplicate. |
Closes #22872
In my experience, the main reson to not catch bare exception is that thet progam has no way of using distinguisging keyboard exception (
crtl + C
) from regular exception. So I opted forin cases where it's not clear what exception may happen or function wrapped in a try-except is so large that basically any exception can in different situations happen.
git diff upstream/master -u -- "*.py" | flake8 --diff