Skip to content

Replace bare excepts by explicit excepts in pandas #22877

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

Closed
datapythonista opened this issue Sep 28, 2018 · 6 comments
Closed

Replace bare excepts by explicit excepts in pandas #22877

datapythonista opened this issue Sep 28, 2018 · 6 comments
Labels
CI Continuous Integration good first issue

Comments

@datapythonista
Copy link
Member

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

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/compat/pickle_compat.py:36:13: E722 do not use bare except'
pandas/compat/pickle_compat.py:47:13: E722 do not use bare except'
pandas/compat/pickle_compat.py:185:1: E722 do not use bare except'
pandas/compat/pickle_compat.py:213:5: E722 do not use bare except'
pandas/tseries/holiday.py:297:5: E722 do not use bare except'
pandas/tseries/holiday.py:429:9: E722 do not use bare except'
pandas/tseries/holiday.py:438:9: E722 do not use bare except'
pandas/util/_print_versions.py:24:9: E722 do not use bare except'
pandas/util/_print_versions.py:53:5: E722 do not use bare except'
pandas/util/_print_versions.py:111:9: E722 do not use bare except'
pandas/util/_validators.py:62:9: E722 do not use bare except'

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

@gfyoung
Copy link
Member

gfyoung commented Sep 28, 2018

@datapythonista : I think we can just create a checklist for the different locations instead of having individual issues, given their similarity.

@datapythonista
Copy link
Member Author

I thought creating few tickets with a subset of the try/except to change was a good way to make it easier to first time contributed to work on them. I feel a checklist would be simpler if regular contributors take care of them, but could make it more complex for first timers.

Anyway, let's see how it goes, it shouldn't make a big difference, and hopefully we'll have all them fixed soon anyway.

@gfyoung
Copy link
Member

gfyoung commented Sep 29, 2018

I feel a checklist would be simpler if regular contributors take care of them, but could make it more complex for first timers.

Gotcha. Well, we can agree to disagree on this one then. 🙂

Anyway, let's see how it goes, it shouldn't make a big difference, and hopefully we'll have all them fixed soon anyway.

Yep, hopefully. They shouldn't be too bad.

@datapythonista
Copy link
Member Author

@NeuralFlux did #22907 close this issue, or is still work to do?

@NeuralFlux
Copy link
Contributor

NeuralFlux commented Oct 11, 2018

@NeuralFlux did #22907 close this issue, or is still work to do?

I believe there's no work left to do, #22907 closes this issue. @datapythonista

@datapythonista
Copy link
Member Author

Thanks!

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
Development

No branches or pull requests

3 participants