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/io/ #22875

Closed
datapythonista opened this issue Sep 28, 2018 · 1 comment · Fixed by #23004
Closed

Replace bare excepts by explicit excepts in pandas/io/ #22875

datapythonista opened this issue Sep 28, 2018 · 1 comment · Fixed by #23004
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: #22872, #22873, #22874, #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/io/clipboards.py:45:9: E722 do not use bare except'
pandas/io/packers.py:706:13: E722 do not use bare except'
pandas/io/parsers.py:1811:9: E722 do not use bare except'
pandas/io/parsers.py:3037:13: E722 do not use bare except'
pandas/io/parsers.py:3266:9: E722 do not use bare except'
pandas/io/parsers.py:3287:9: E722 do not use bare except'
pandas/io/parsers.py:3291:9: E722 do not use bare except'
pandas/io/pickle.py:172:13: E722 do not use bare except'
pandas/io/pickle.py:177:5: E722 do not use bare except'
pandas/io/sql.py:385:5: E722 do not use bare except'
pandas/io/sql.py:850:13: E722 do not use bare except'
pandas/io/sql.py:1363:9: E722 do not use bare except'
pandas/io/stata.py:1255:9: E722 do not use bare except'
pandas/io/stata.py:1260:9: E722 do not use bare except'
pandas/io/formats/console.py:103:5: E722 do not use bare except'
pandas/io/formats/console.py:121:5: E722 do not use bare except'
pandas/io/formats/console.py:140:5: E722 do not use bare except'
pandas/io/formats/console.py:152:5: E722 do not use bare except'
pandas/io/formats/terminal.py:81:5: E722 do not use bare except'
pandas/io/formats/terminal.py:111:5: E722 do not use bare except'
pandas/io/formats/terminal.py:123:9: E722 do not use bare except'
pandas/io/formats/terminal.py:132:9: E722 do not use bare except'
pandas/io/formats/terminal.py:138:9: E722 do not use bare except'
pandas/io/sas/sasreader.py:49:9: E722 do not use bare except'
pandas/io/sas/sas_xport.py:249:13: E722 do not use bare except'

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

@JustinZhengBC
Copy link
Contributor

I'll try to fix this one.

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
3 participants