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/core/ #22873

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

Replace bare excepts by explicit excepts in pandas/core/ #22873

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

Comments

@datapythonista
Copy link
Member

datapythonista commented Sep 28, 2018

(superseeds #18419, same task for different files: #22872, #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/core/frame.py:3263:13: E722 do not use bare except'
pandas/core/frame.py:7706:9: E722 do not use bare except'
pandas/core/indexing.py:2149:9: E722 do not use bare except'
pandas/core/indexing.py:2708:13: E722 do not use bare except'
pandas/core/indexing.py:2714:9: E722 do not use bare except'
pandas/core/nanops.py:506:13: E722 do not use bare except'
pandas/core/nanops.py:818:13: E722 do not use bare except'
pandas/core/ops.py:1548:17: E722 do not use bare except'
pandas/core/window.py:2507:5: E722 do not use bare except'
pandas/core/computation/pytables.py:414:9: E722 do not use bare except'
pandas/core/dtypes/common.py:470:5: E722 do not use bare except'
pandas/core/dtypes/dtypes.py:361:9: E722 do not use bare except'
pandas/core/dtypes/dtypes.py:522:13: E722 do not use bare except'
pandas/core/indexes/frozen.py:142:9: E722 do not use bare except'
pandas/core/indexes/multi.py:1005:17: E722 do not use bare except'
pandas/core/indexes/multi.py:1010:17: E722 do not use bare except'
pandas/core/indexes/multi.py:1689:9: E722 do not use bare except'
pandas/core/indexes/multi.py:2318:17: E722 do not use bare except'
pandas/core/indexes/multi.py:2821:17: E722 do not use bare except'
pandas/core/internals/blocks.py:669:9: E722 do not use bare except'
pandas/core/internals/blocks.py:1145:9: E722 do not use bare except'
pandas/core/internals/blocks.py:1160:9: E722 do not use bare except'
pandas/core/internals/blocks.py:2441:13: E722 do not use bare except'
pandas/core/internals/blocks.py:3175:9: E722 do not use bare except'
pandas/core/sparse/array.py:309:9: E722 do not use bare except'
pandas/core/tools/datetimes.py:244:17: E722 do not use bare except'
pandas/core/tools/datetimes.py:334:9: E722 do not use bare except'
pandas/core/tools/datetimes.py:731:5: E722 do not use bare except'
pandas/core/tools/datetimes.py:738:5: E722 do not use bare except'
pandas/core/tools/datetimes.py:745:5: E722 do not use bare except'

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

@pambot
Copy link
Contributor

pambot commented Sep 29, 2018

I'd like to give this a try. I started on a few, but it's definitely not PR-ready.

@datapythonista
Copy link
Member Author

Looking great, let us know if you need any help.

And feel free to open a PR for part of them only, if there are too many in this issue. We can create a separate ticket for the remaining.

@datapythonista
Copy link
Member Author

@pambot did #22901 close this issue, or are there still more excepts to fix?

@datapythonista
Copy link
Member Author

Closed via #22901

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

2 participants