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
Fix sympy.testing.pytest.raises to behave like pytest.raises #20012
Conversation
✅ Hi, I am the SymPy bot (v160). I'm here to help you write a release notes entry. Please read the guide on how to write release notes. Your release notes are in good order. Here is what the release notes will look like:
This will be added to https://github.com/sympy/sympy/wiki/Release-Notes-for-1.7. Note: This comment will be updated with the latest check if you edit the pull request. You need to reload the page to see it. Click here to see the pull request description that was parsed.
Update The release notes on the wiki have been updated. |
3003f83
to
c559cc5
Compare
c559cc5
to
c5cb5f1
Compare
Code written as `assert raises(...)` would pass in pytest and fail in sympy, because sympy was not returning an appropriate object from `raises`.
c5cb5f1
to
794c985
Compare
CI failure looks like a PyPy network issue. |
Looks like it is working after a restart. |
Codecov Report
@@ Coverage Diff @@
## master #20012 +/- ##
=============================================
- Coverage 75.878% 75.858% -0.020%
=============================================
Files 669 669
Lines 173564 173613 +49
Branches 40969 40971 +2
=============================================
+ Hits 131697 131701 +4
- Misses 36135 36177 +42
- Partials 5732 5735 +3 |
Good to merge then? |
This looks good and should be merged for compatibility between pytest and sympy's test runner. Personally I think that we should just use pytest all the time but that's another issue. I also think that we shouldn't use |
I agree I personally like the lambda form better because it avoids this issue # Test that these all raise ValueError
with raises(ValueError):
f(...)
g(...)
... That can't work. Only the first exception would be tested. It's an easy mistake to make, even if you know about it. So I would prefer the lambda form unless the thing being tested can only be written as a statement. |
I agree that this is a problem. Our contributors are generally not sufficiently experienced to avoid that so it is better to prefer the lambda approach most of the time. Related to that is the
|
Agreed on both points. If you feel strongly about the second, you could make a patch to pytest to make |
FWIW I'm not a huge fan of pytest. But it's unrelated to this PR, so should be discussed elsewhere. I don't know if there's any way to have a code quality test that checks against the context manager problem. I think it's kind of like the problem of forgetting to write |
I think that this is just something we should pick up on review. It could be listed in the dev guide somewhere as well with an explanation. For now though we can clean it up. There is also an example with
I don't see any automated way to pick up on the context manager problem without false positives unless we require only a single statement in the block. You could have something like
If the intention is to test that
That's a more accurate test of the warning but it could lead to less readable tests in some cases. |
Putting this in to unblock #19463. While the discussion is useful and worth continuing, I don't think it's relevant to whether this PR should be merged, and #20012 (comment) seems to be in favor of merging |
I started https://github.com/sympy/sympy/wiki/Writing-tests, with a gotchas section that has these things. I also added some other basic info on writing tests, but more could be added. |
There seems to be a mistaken impression thatXFAIL
is needed for tests withraises
. This should not be true.This was true...
Code written as
assert raises(...)
would pass in pytest and fail in sympy, because sympy was not returning an appropriate object fromraises
.References to other Issues or PRs
Brief description of what is fixed or changed
Other comments
Release Notes
assert sympy.testing.pytest.raises(Exception, func)
no longer always asserts when pytest is not present