question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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.

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
MarcoGorellicommented, Dec 19, 2020

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

1reaction
MarcoGorellicommented, Nov 20, 2020

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)

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to use pytest to check that Error is NOT raised
Doing it this way allows you to test that it did not raise any exception. ... pytest will fail anyway as soon as...
Read more >
Asserting Exceptions with Pytest - DEV Community ‍ ‍
The problem is that when function does not raise exception, test_param() gets fail with the following error. Failed: DID NOT RAISE It works...
Read more >
How to write and report assertions in tests - Pytest
Using pytest.raises() is likely to be better for cases where you are testing exceptions your own code is deliberately raising, whereas using @pytest....
Read more >
Chapter03 Introduction to Pytest - Pragmatic AI Labs
The most simple test possible #. Pytest allows you to write test functions, which is a major feature if you are getting started...
Read more >
pytest Documentation - Read the Docs
pytest Documentation, Release 0.1 ... The first test passed and the second failed. ... with pytest.raises(RuntimeError) as excinfo: def f():.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found