pandas-dev/pandas (original) (raw)

End users rely on error messages for their debugging purposes. Thus, it is important that we make sure that the correct error messages are surfaced depending on the error triggered.

The core idea is to convert this:

with pytest.raises(klass): # Some code that raise an error

To this:

with pytest.raises(klass, match=msg): # Some code that raise an error

You can read more about pytest.raises here.


Side note:

In case that the raised error message is an external error message (meaning that's not pandas specific), you should use the external_error_raised instead of pytest.raises.

the usage of external_error_raised is exactly like pytest.raises the only difference is that you don't pass in the match argument.

For example:

import pandas._testing as tm

def test_foo(): with tm.external_error_raised(ValueError): raise ValueError("foo")


Keynotes:

https://github.com/pandas-dev/pandas/issues/30999

in your PR.


To generate the full list yourself, you can add:

-   id: unwanted-patterns-bare-pytest-raises
    name: Check for use of bare use of pytest raises
    language: python
    entry: python scripts/validate_unwanted_patterns.py --validation-type="bare_pytest_raises"
    types: [python]
    files: ^pandas/tests/
    exclude: ^pandas/tests/extension

to .pre-commit-config.yaml in the - repo: local section, and then run

pre-commit run unwanted-patterns-bare-pytest-raises --all-files.

The current list is:


NOTE:

The list may change as files are moved/renamed constantly.


Took pretty much everything from #23922, that was originally opened by @gfyoung.