Skip to content

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

Closed
@datapythonista

Description

@datapythonista

(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/

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions