Skip to content
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

Replace bare excepts by explicit excepts in pandas/tests/ #22872

Closed
datapythonista opened this issue Sep 28, 2018 · 4 comments · Fixed by #23370
Closed

Replace bare excepts by explicit excepts in pandas/tests/ #22872

datapythonista opened this issue Sep 28, 2018 · 4 comments · Fixed by #23370
Labels
CI Continuous Integration good first issue
Milestone

Comments

@datapythonista
Copy link
Member

datapythonista commented Sep 28, 2018

(superseeds #18419, same task for different files: #22873, #22874, #22875, #22877)

In several parts of the code, we have bare excepts in the form of:

try:
    my_value = my_dict[my_key]
except:  # we are capturing all the exceptions, when we want to capture only KeyError
    my_value = None

This is a bad practice, and we want to avoid having this in the code. What we want instead is:

try:
    my_value = my_dict[my_key]
except KeyError:
    my_value = None

Of course in every case, the exception can be different (not always KeyError), and some research is needed to see which is the right exception (or exceptions, it can be a tuple) that needs to be captured. In some cases, every exception needs to be captured and we'll use except Exception:, but this should be avoided unless we really need to capture all exceptions.

This issue is to fix all the bare excepts in testing files pandas/tests/. At the moment they are (note that the list can change as code evolves):

pandas/tests/test_multilevel.py:1378:9: E722 do not use bare except'
pandas/tests/test_nanops.py:144:9: E722 do not use bare except'
pandas/tests/test_nanops.py:149:9: E722 do not use bare except'
pandas/tests/test_nanops.py:170:21: E722 do not use bare except'
pandas/tests/test_nanops.py:174:21: E722 do not use bare except'
pandas/tests/test_panel.py:338:13: E722 do not use bare except'
pandas/tests/test_panel.py:344:13: E722 do not use bare except'
pandas/tests/test_strings.py:2663:13: E722 do not use bare except'
pandas/tests/indexing/common.py:154:13: E722 do not use bare except'
pandas/tests/indexing/common.py:217:17: E722 do not use bare except'
pandas/tests/io/test_pytables.py:54:9: E722 do not use bare except'
pandas/tests/io/test_pytables.py:62:5: E722 do not use bare except'
pandas/tests/io/test_pytables.py:120:5: E722 do not use bare except'
pandas/tests/io/test_pytables.py:4624:21: E722 do not use bare except'
pandas/tests/io/test_sql.py:1793:9: E722 do not use bare except'
pandas/tests/io/test_sql.py:2378:9: E722 do not use bare except'
pandas/tests/io/test_sql.py:2405:9: E722 do not use bare except'
pandas/tests/io/formats/test_format.py:73:5: E722 do not use bare except'
pandas/tests/io/formats/test_format.py:455:13: E722 do not use bare except'

An updated list can be obtained by running: flake8 --select=E722 --config=none pandas/tests/

@HaraldNordgren
Copy link

On it!

@alexander-ponomaroff
Copy link
Contributor

This issue seems not fixed after some time. It has been a month and I'm not sure if Harald will be finishing this as he didn't respond if he will be fixing the requested changes on his pull request. Could I take over and try to work on this? @datapythonista @jreback

@jreback
Copy link
Contributor

jreback commented Oct 27, 2018

we would accept multiple PRs for this

@alexander-ponomaroff
Copy link
Contributor

This is the updated list. I will start working on this as soon as possible.

pandas/tests/test_panel.py:338:13: E722 do not use bare 'except'
pandas/tests/test_panel.py:344:13: E722 do not use bare 'except'
pandas/tests/test_nanops.py:144:9: E722 do not use bare 'except'
pandas/tests/test_nanops.py:149:9: E722 do not use bare 'except'
pandas/tests/test_nanops.py:170:21: E722 do not use bare 'except'
pandas/tests/test_nanops.py:174:21: E722 do not use bare 'except'
pandas/tests/test_multilevel.py:1381:9: E722 do not use bare 'except'
pandas/tests/test_strings.py:2632:13: E722 do not use bare 'except'
pandas/tests/io/test_pytables.py:54:9: E722 do not use bare 'except'
pandas/tests/io/test_pytables.py:62:5: E722 do not use bare 'except'
pandas/tests/io/test_pytables.py:120:5: E722 do not use bare 'except'
pandas/tests/io/test_pytables.py:4624:21: E722 do not use bare 'except'
pandas/tests/io/test_sql.py:1793:9: E722 do not use bare 'except'
pandas/tests/io/test_sql.py:2378:9: E722 do not use bare 'except'
pandas/tests/io/test_sql.py:2405:9: E722 do not use bare 'except'
pandas/tests/io/formats/test_format.py:73:5: E722 do not use bare 'except'
pandas/tests/io/formats/test_format.py:455:13: E722 do not use bare 'except'
pandas/tests/indexing/common.py:154:13: E722 do not use bare 'except'
pandas/tests/indexing/common.py:217:17: E722 do not use bare 'except'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CI Continuous Integration good first issue
Projects
None yet
4 participants