Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved error when () (empty tuple) is passed to pytest.raises() or pytest.warns() #8646

Closed
Zac-HD opened this issue May 8, 2021 · 0 comments · Fixed by #9911
Closed

Improved error when () (empty tuple) is passed to pytest.raises() or pytest.warns() #8646

Zac-HD opened this issue May 8, 2021 · 0 comments · Fixed by #9911
Labels
type: enhancement new feature or API change, should be merged into features branch

Comments

@Zac-HD
Copy link
Member

Zac-HD commented May 8, 2021

When using pytest.raises() as a context manager, you can pass an exception type or tuple of exception types; it's then an error if no exception is raised or if the exception raised is not an instance of the expected type(s). The same logic applies to pytest.warns(), which has a near-identical API.

The problem is that if you pass the empty tuple (), this will always result in an error: even if an exception is raised, it can't be an instance of ()! I think we should explicitly check tuple inputs, and raise a more helpful error message if they're empty. For example:

  • "Passing expected_exception=() is an error, because it's impossible to raise an exception which is not an instance of any type. Raising exceptions is already understood as failing the test, so you don't need any special code to say 'this should never raise an exception'."
    (for bonus points, pytest.raises(None) should have the same message, with =None instead of =())
  • The same logic, and same error message, applies to the raises= argument to pytest.mark.xfail().
  • "Passing expected_warning=() is an error, because it's impossible to emit a warning which is not an instance of any type. To assert that no warnings are emitted, use <whatever we come up with for Improve pytest.warns() docs to clarify difference with catch_warnings() #9002>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: enhancement new feature or API change, should be merged into features branch
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant