Skip to content

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

Closed
@ShaharNaveh

Description

@ShaharNaveh

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Code StyleCode style, linting, code_checksTestingpandas testing functions or related to the test suitegood first issue

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions