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
Initial implementation of RaisesGroup #11671
base: main
Are you sure you want to change the base?
Conversation
Hey @jakkdl thanks for this PR! I like the idea of going with
I would vote to go with just What do you think @bluetech @RonnyPfannschmidt ? |
(I think you meant python-trio/trio#2898 and not python-trio/trio#2891) |
Thanks, edited my post.
|
…as assertions with descriptive outputs for why a match failed
Some polishing of docstrings and the like left, but this should be a decent MVP. With the logic being simpler I also added an I didn't rename |
I think it is important for us to be consistent with However no need to rename the class, let us create a @bluetech this is a new feature, but given it is isolated I think we can release this in |
At this point I think it's better not to add new features to 8.0. But we can have a 8.1 not too long after. |
What about """Check if an exception matches the requirements of this RaisesGroup.
Example::
with pytest.raises(TypeError) as excinfo:
...
assert RaisesGroups(ValueError).matches(excinfo.value.__cause__)
# the above line is equivalent to
myexc = excinfo.value.__cause
assert isinstance(myexc, BaseExceptionGroup)
assert len(myexc.exceptions) == 1
assert isinstance(myexc.exceptions[0], ValueError)
""" |
Definitely, but I believe the idea is to eventually have a |
No that's a different thing, this is for checking a constructed exception that hasn't been raised - something that with pytest.raises_group(TypeError, check=lambda x: "foo" in getattr(x, "__notes__", ())):
e = ExceptionGroup("", (TypeError(),))
e.add_note("foo")
raise e and in the full implementation with with trio.RaisesGroup(trio.Matcher(TypeError, match="bar"), match="foo")):
raise ExceptionGroup("foo", (TypeError("bar"),)) but with pytest.raises(TypeError) as exc_info:
...
myexc = excinfo.value.__cause__
assert isinstance(myexc, BaseExceptionGroup)
assert len(myexc.exceptions) == 1
assert isinstance(myexc.exceptions[0], ValueError) could now be written with pytest.raises(TypeError) as exc_info:
...
assert pytest.raises_group(ValueError).matches(excinfo.value.__cause__) or if using expected_cause = pytest.raises_group(ValueError)
with pytest.raises_group(TypeError, check=lambda x: expected_cause.matches(x.__cause__)):
... Exposing that functionality was trivial, since that's already what was used internally, and cleaned up several instances in the test suite. The equivalent in non-exceptiongroup cases would be: with pytest.raises(TypeError) as exc_info:
...
assert isinstance(exc_info.value.__cause__, ValueError) But I guess if you want to continue with the super minimal public API, I can simply make |
Initially I wanted to keep the API really simple, to avoid committing to something more complex right now, because I was hoping to get this released in 8.0. But given this will no longer land in 8.0x, I think we can have more detailed discussion to include a more complete API regarding matches. Another option is to actually hold deciding on a "matches" API/solution, taking advantage that trio has already released its own API, and see how it fares on the wild. |
@bluetech @RonnyPfannschmidt @The-Compiler @Zac-HD thoughts? |
|
minor note that the parameter is named
I might play around with the "full" implementation in the trio repository to see how to add better messages on failure for nested groups and the like, to see if both
It seems reasonable to expose I do think it's possibly a bit problematic that with pytest.raises(ValueError):
... and with pytest.raises_group(ValueError):
... are so similar, esp when there's tons of parameters and other busy stuff around it, whereas |
This is an alternative implementation to #11656, to fix #11538
Instead of doing
this condenses it to
which also means it doesn't do any modifications to
pytest.raises
orRaisesContext