pandas: [Good first issue] TST: Disallow bare pytest.raises

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:

  • Don’t forget to link this issue in your PR, paste this
https://github.com/pandas-dev/pandas/issues/30999

in your PR.

  • Please comment what you are planning to work on, so we won’t do double the work (no need to mention me, you can just declare what you are planning to work on, just remember to check if something is already taken).

  • If a file/files that should be marked as “done” (as if there is no more work to do), isn’t marked as “done”, please comment letting me know about that (And mentioning me by putting @MomIsBestFriend at the comment’s body, so I’ll know where to look).


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:

  • pandas/tests/arrays/boolean/test_arithmetic.py
  • pandas/tests/computation/test_compat.py
  • pandas/tests/dtypes/test_inference.py
  • pandas/tests/indexes/multi/test_indexing.py
  • pandas/tests/indexes/multi/test_setops.py
  • pandas/tests/indexes/period/test_indexing.py
  • pandas/tests/indexes/test_common.py
  • pandas/tests/indexes/test_numpy_compat.py
  • pandas/tests/indexing/multiindex/test_partial.py
  • pandas/tests/indexing/test_coercion.py
  • pandas/tests/io/test_sql.py
  • pandas/tests/libs/test_hashtable.py
  • pandas/tests/reductions/test_reductions.py
  • pandas/tests/reductions/test_stat_reductions.py
  • pandas/tests/resample/test_resampler_grouper.py
  • pandas/tests/reshape/test_get_dummies.py
  • pandas/tests/reshape/test_union_categoricals.py
  • pandas/tests/series/apply/test_series_apply.py
  • pandas/tests/series/test_ufunc.py
  • pandas/tests/window/moments/test_moments_ewm.py
  • pandas/tests/window/test_apply.py

NOTE:

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


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

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 84 (79 by maintainers)

Commits related to this issue

Most upvoted comments

Thanks, I’ve updated the instructions, no need to change the script

Does it make sense to add this to the .pre-commit-config.yaml and then update the instructions on this thread?

We will add it to .pre-commit-config.yaml once all the errors it raises are fixed, yes

Seems to be related to #37419 perhaps?

No, it’s related to #37379 (which is when we moved this script over to pre-commit, hence it was no longer necessary for it to run on directories)

@MomIsBestFriend this one is done already but isn’t marked as done: pandas/tests/arithmetic/test_numeric.py

UPDATE

@MomIsBestFriend these too: pandas/tests/arithmetic/test_period.py pandas/tests/arrays/test_integer.py pandas/tests/arrays/test_period.py

pandas._testing

#31072 contains the one missing match in stata.py

I can take care of these: @MomIsBestFriend

pandas/tests/io/test_html.py pandas/tests/io/test_parquet.py pandas/tests/io/test_sql.py pandas/tests/io/test_stata.py pandas/tests/plotting/test_backend.py pandas/tests/plotting/test_boxplot_method.py pandas/tests/plotting/test_frame.py pandas/tests/plotting/test_hist_method.py pandas/tests/plotting/test_series.py pandas/tests/reductions/test_reductions.py

@MomIsBestFriend I will help with : pandas/tests/base/test_constructors.py pandas/tests/base/test_ops.py

@gfyoung the list wasn’t generated by grep -r -e "pytest.raises([a-zA-Z]*)" pandas/tests -l in fact, it was generated by the script in #30755 (a validation type called bare_pytest_raises), I will put instructions at the issue body, once it gets merged 😄

@gdex1 I hope this will help you 😃

(The numbers represents line number)

pandas/tests/arithmetic/test_numeric.py:138
pandas/tests/arithmetic/test_numeric.py:141
pandas/tests/arithmetic/test_numeric.py:190
pandas/tests/arithmetic/test_numeric.py:208
pandas/tests/arithmetic/test_numeric.py:210
pandas/tests/arithmetic/test_numeric.py:212
pandas/tests/arithmetic/test_numeric.py:214
pandas/tests/arithmetic/test_numeric.py:232
pandas/tests/arithmetic/test_numeric.py:234
pandas/tests/arithmetic/test_numeric.py:236
pandas/tests/arithmetic/test_numeric.py:238
pandas/tests/arithmetic/test_numeric.py:519
pandas/tests/arithmetic/test_numeric.py:610
pandas/tests/arithmetic/test_numeric.py:615
pandas/tests/arithmetic/test_numeric.py:617
pandas/tests/arithmetic/test_numeric.py:795
pandas/tests/arithmetic/test_numeric.py:798
pandas/tests/arithmetic/test_numeric.py:819
pandas/tests/arithmetic/test_object.py:140
pandas/tests/arithmetic/test_object.py:152
pandas/tests/arithmetic/test_object.py:154
pandas/tests/arithmetic/test_object.py:278
pandas/tests/arithmetic/test_object.py:280
pandas/tests/arithmetic/test_object.py:282
pandas/tests/arithmetic/test_object.py:284
pandas/tests/arithmetic/test_object.py:298
pandas/tests/arithmetic/test_object.py:301
pandas/tests/arithmetic/test_object.py:315
pandas/tests/arithmetic/test_object.py:318
pandas/tests/arithmetic/test_timedelta64.py:51
pandas/tests/arithmetic/test_timedelta64.py:445
pandas/tests/arithmetic/test_timedelta64.py:607
pandas/tests/arithmetic/test_timedelta64.py:609
pandas/tests/arithmetic/test_timedelta64.py:703
pandas/tests/arithmetic/test_timedelta64.py:705
pandas/tests/arithmetic/test_timedelta64.py:707
pandas/tests/arithmetic/test_timedelta64.py:709
pandas/tests/arithmetic/test_timedelta64.py:741
pandas/tests/arithmetic/test_timedelta64.py:743
pandas/tests/arithmetic/test_timedelta64.py:960
pandas/tests/arithmetic/test_timedelta64.py:972
pandas/tests/arithmetic/test_timedelta64.py:1028
pandas/tests/arithmetic/test_timedelta64.py:1037
pandas/tests/arithmetic/test_timedelta64.py:1039
pandas/tests/arithmetic/test_timedelta64.py:1502
pandas/tests/arithmetic/test_timedelta64.py:1505
pandas/tests/arithmetic/test_timedelta64.py:1508
pandas/tests/arithmetic/test_timedelta64.py:1511
pandas/tests/arithmetic/test_timedelta64.py:1536
pandas/tests/arithmetic/test_timedelta64.py:1591
pandas/tests/arithmetic/test_timedelta64.py:1783
pandas/tests/arithmetic/test_timedelta64.py:1785
pandas/tests/arithmetic/test_timedelta64.py:1911
pandas/tests/arithmetic/test_timedelta64.py:1960
pandas/tests/arithmetic/test_timedelta64.py:1962
pandas/tests/arithmetic/test_timedelta64.py:1968
pandas/tests/arrays/interval/test_interval.py:155