| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
# several expected multi-line strings contain long lines. We don't wanna break them up |
| 4 |
# as that makes it confusing to see where the line breaks are. |
| 5 |
# ruff: noqa: E501 |
| 6 |
from contextlib import AbstractContextManager |
| 7 |
import re |
| 8 |
import sys |
| 9 |
|
| 10 |
from _pytest._code import ExceptionInfo |
| 11 |
from _pytest.outcomes import Failed |
| 12 |
from _pytest.pytester import Pytester |
| 13 |
from _pytest.raises import RaisesExc |
| 14 |
from _pytest.raises import RaisesGroup |
| 15 |
from _pytest.raises import repr_callable |
| 16 |
import pytest |
| 17 |
|
| 18 |
|
| 19 |
if sys.version_info < (3, 11): |
| 20 |
from exceptiongroup import BaseExceptionGroup |
| 21 |
from exceptiongroup import ExceptionGroup |
| 22 |
|
| 23 |
|
| 24 |
def wrap_escape(s: str) -> str: |
| 25 |
return "^" + re.escape(s) + "$" |
| 26 |
|
| 27 |
|
| 28 |
def fails_raises_group(msg: str, add_prefix: bool = True) -> RaisesExc[Failed]: |
| 29 |
assert msg[-1] != "\n", ( |
| 30 |
"developer error, expected string should not end with newline" |
| 31 |
) |
| 32 |
prefix = "Raised exception group did not match: " if add_prefix else "" |
| 33 |
return pytest.raises(Failed, match=wrap_escape(prefix + msg)) |
| 34 |
|
| 35 |
|
| 36 |
def test_raises_group() -> None: |
| 37 |
with pytest.raises( |
| 38 |
TypeError, |
| 39 |
match=wrap_escape("Expected a BaseException type, but got 'int'"), |
| 40 |
): |
| 41 |
RaisesExc(5) # type: ignore[call-overload] |
| 42 |
with pytest.raises( |
| 43 |
ValueError, |
| 44 |
match=wrap_escape("Expected a BaseException type, but got 'int'"), |
| 45 |
): |
| 46 |
RaisesExc(int) # type: ignore[type-var] |
| 47 |
with pytest.raises( |
| 48 |
TypeError, |
| 49 |
match=wrap_escape( |
| 50 |
"Expected a BaseException type, RaisesExc, or RaisesGroup, but got an exception instance: ValueError", |
| 51 |
), |
| 52 |
): |
| 53 |
RaisesGroup(ValueError()) # type: ignore[call-overload] |
| 54 |
with RaisesGroup(ValueError): |
| 55 |
raise ExceptionGroup("foo", (ValueError(),)) |
| 56 |
|
| 57 |
with ( |
| 58 |
fails_raises_group("`SyntaxError()` is not an instance of `ValueError`"), |
| 59 |
RaisesGroup(ValueError), |
| 60 |
): |
| 61 |
raise ExceptionGroup("foo", (SyntaxError(),)) |
| 62 |
|
| 63 |
# multiple exceptions |
| 64 |
with RaisesGroup(ValueError, SyntaxError): |
| 65 |
raise ExceptionGroup("foo", (ValueError(), SyntaxError())) |
| 66 |
|
| 67 |
# order doesn't matter |
| 68 |
with RaisesGroup(SyntaxError, ValueError): |
| 69 |
raise ExceptionGroup("foo", (ValueError(), SyntaxError())) |
| 70 |
|
| 71 |
# nested exceptions |
| 72 |
with RaisesGroup(RaisesGroup(ValueError)): |
| 73 |
raise ExceptionGroup("foo", (ExceptionGroup("bar", (ValueError(),)),)) |
| 74 |
|
| 75 |
with RaisesGroup( |
| 76 |
SyntaxError, |
| 77 |
RaisesGroup(ValueError), |
| 78 |
RaisesGroup(RuntimeError), |
| 79 |
): |
| 80 |
raise ExceptionGroup( |
| 81 |
"foo", |
| 82 |
( |
| 83 |
SyntaxError(), |
| 84 |
ExceptionGroup("bar", (ValueError(),)), |
| 85 |
ExceptionGroup("", (RuntimeError(),)), |
| 86 |
), |
| 87 |
) |
| 88 |
|
| 89 |
|
| 90 |
def test_incorrect_number_exceptions() -> None: |
| 91 |
# We previously gave an error saying the number of exceptions was wrong, |
| 92 |
# but we now instead indicate excess/missing exceptions |
| 93 |
with ( |
| 94 |
fails_raises_group( |
| 95 |
"1 matched exception. Unexpected exception(s): [RuntimeError()]" |
| 96 |
), |
| 97 |
RaisesGroup(ValueError), |
| 98 |
): |
| 99 |
raise ExceptionGroup("", (RuntimeError(), ValueError())) |
| 100 |
|
| 101 |
# will error if there's missing exceptions |
| 102 |
with ( |
| 103 |
fails_raises_group( |
| 104 |
"1 matched exception. Too few exceptions raised, found no match for: [SyntaxError]" |
| 105 |
), |
| 106 |
RaisesGroup(ValueError, SyntaxError), |
| 107 |
): |
| 108 |
raise ExceptionGroup("", (ValueError(),)) |
| 109 |
|
| 110 |
with ( |
| 111 |
fails_raises_group( |
| 112 |
"\n" |
| 113 |
"1 matched exception. \n" |
| 114 |
"Too few exceptions raised!\n" |
| 115 |
"The following expected exceptions did not find a match:\n" |
| 116 |
" ValueError\n" |
| 117 |
" It matches `ValueError()` which was paired with `ValueError`" |
| 118 |
), |
| 119 |
RaisesGroup(ValueError, ValueError), |
| 120 |
): |
| 121 |
raise ExceptionGroup("", (ValueError(),)) |
| 122 |
|
| 123 |
with ( |
| 124 |
fails_raises_group( |
| 125 |
"\n" |
| 126 |
"1 matched exception. \n" |
| 127 |
"Unexpected exception(s)!\n" |
| 128 |
"The following raised exceptions did not find a match\n" |
| 129 |
" ValueError('b'):\n" |
| 130 |
" It matches `ValueError` which was paired with `ValueError('a')`" |
| 131 |
), |
| 132 |
RaisesGroup(ValueError), |
| 133 |
): |
| 134 |
raise ExceptionGroup("", (ValueError("a"), ValueError("b"))) |
| 135 |
|
| 136 |
with ( |
| 137 |
fails_raises_group( |
| 138 |
"\n" |
| 139 |
"1 matched exception. \n" |
| 140 |
"The following expected exceptions did not find a match:\n" |
| 141 |
" ValueError\n" |
| 142 |
" It matches `ValueError()` which was paired with `ValueError`\n" |
| 143 |
"The following raised exceptions did not find a match\n" |
| 144 |
" SyntaxError():\n" |
| 145 |
" `SyntaxError()` is not an instance of `ValueError`" |
| 146 |
), |
| 147 |
RaisesGroup(ValueError, ValueError), |
| 148 |
): |
| 149 |
raise ExceptionGroup("", [ValueError(), SyntaxError()]) |
| 150 |
|
| 151 |
|
| 152 |
def test_flatten_subgroups() -> None: |
| 153 |
# loose semantics, as with expect* |
| 154 |
with RaisesGroup(ValueError, flatten_subgroups=True): |
| 155 |
raise ExceptionGroup("", (ExceptionGroup("", (ValueError(),)),)) |
| 156 |
|
| 157 |
with RaisesGroup(ValueError, TypeError, flatten_subgroups=True): |
| 158 |
raise ExceptionGroup("", (ExceptionGroup("", (ValueError(), TypeError())),)) |
| 159 |
with RaisesGroup(ValueError, TypeError, flatten_subgroups=True): |
| 160 |
raise ExceptionGroup("", [ExceptionGroup("", [ValueError()]), TypeError()]) |
| 161 |
|
| 162 |
# mixed loose is possible if you want it to be at least N deep |
| 163 |
with RaisesGroup(RaisesGroup(ValueError, flatten_subgroups=True)): |
| 164 |
raise ExceptionGroup("", (ExceptionGroup("", (ValueError(),)),)) |
| 165 |
with RaisesGroup(RaisesGroup(ValueError, flatten_subgroups=True)): |
| 166 |
raise ExceptionGroup( |
| 167 |
"", |
| 168 |
(ExceptionGroup("", (ExceptionGroup("", (ValueError(),)),)),), |
| 169 |
) |
| 170 |
|
| 171 |
# but not the other way around |
| 172 |
with pytest.raises( |
| 173 |
ValueError, |
| 174 |
match=r"^You cannot specify a nested structure inside a RaisesGroup with", |
| 175 |
): |
| 176 |
RaisesGroup(RaisesGroup(ValueError), flatten_subgroups=True) # type: ignore[call-overload] |
| 177 |
|
| 178 |
# flatten_subgroups is not sufficient to catch fully unwrapped |
| 179 |
with ( |
| 180 |
fails_raises_group( |
| 181 |
"`ValueError()` is not an exception group, but would match with `allow_unwrapped=True`" |
| 182 |
), |
| 183 |
RaisesGroup(ValueError, flatten_subgroups=True), |
| 184 |
): |
| 185 |
raise ValueError |
| 186 |
with ( |
| 187 |
fails_raises_group( |
| 188 |
"RaisesGroup(ValueError, flatten_subgroups=True): `ValueError()` is not an exception group, but would match with `allow_unwrapped=True`" |
| 189 |
), |
| 190 |
RaisesGroup(RaisesGroup(ValueError, flatten_subgroups=True)), |
| 191 |
): |
| 192 |
raise ExceptionGroup("", (ValueError(),)) |
| 193 |
|
| 194 |
# helpful suggestion if flatten_subgroups would make it pass |
| 195 |
with ( |
| 196 |
fails_raises_group( |
| 197 |
"Raised exception group did not match: \n" |
| 198 |
"The following expected exceptions did not find a match:\n" |
| 199 |
" ValueError\n" |
| 200 |
" TypeError\n" |
| 201 |
"The following raised exceptions did not find a match\n" |
| 202 |
" ExceptionGroup('', [ValueError(), TypeError()]):\n" |
| 203 |
" Unexpected nested `ExceptionGroup()`, expected `ValueError`\n" |
| 204 |
" Unexpected nested `ExceptionGroup()`, expected `TypeError`\n" |
| 205 |
"Did you mean to use `flatten_subgroups=True`?", |
| 206 |
add_prefix=False, |
| 207 |
), |
| 208 |
RaisesGroup(ValueError, TypeError), |
| 209 |
): |
| 210 |
raise ExceptionGroup("", [ExceptionGroup("", [ValueError(), TypeError()])]) |
| 211 |
# but doesn't consider check (otherwise we'd break typing guarantees) |
| 212 |
with ( |
| 213 |
fails_raises_group( |
| 214 |
"Raised exception group did not match: \n" |
| 215 |
"The following expected exceptions did not find a match:\n" |
| 216 |
" ValueError\n" |
| 217 |
" TypeError\n" |
| 218 |
"The following raised exceptions did not find a match\n" |
| 219 |
" ExceptionGroup('', [ValueError(), TypeError()]):\n" |
| 220 |
" Unexpected nested `ExceptionGroup()`, expected `ValueError`\n" |
| 221 |
" Unexpected nested `ExceptionGroup()`, expected `TypeError`\n" |
| 222 |
"Did you mean to use `flatten_subgroups=True`?", |
| 223 |
add_prefix=False, |
| 224 |
), |
| 225 |
RaisesGroup( |
| 226 |
ValueError, |
| 227 |
TypeError, |
| 228 |
check=lambda eg: len(eg.exceptions) == 1, |
| 229 |
), |
| 230 |
): |
| 231 |
raise ExceptionGroup("", [ExceptionGroup("", [ValueError(), TypeError()])]) |
| 232 |
# correct number of exceptions, and flatten_subgroups would make it pass |
| 233 |
# This now doesn't print a repr of the caught exception at all, but that can be found in the traceback |
| 234 |
with ( |
| 235 |
fails_raises_group( |
| 236 |
"Raised exception group did not match: Unexpected nested `ExceptionGroup()`, expected `ValueError`\n" |
| 237 |
" Did you mean to use `flatten_subgroups=True`?", |
| 238 |
add_prefix=False, |
| 239 |
), |
| 240 |
RaisesGroup(ValueError), |
| 241 |
): |
| 242 |
raise ExceptionGroup("", [ExceptionGroup("", [ValueError()])]) |
| 243 |
# correct number of exceptions, but flatten_subgroups wouldn't help, so we don't suggest it |
| 244 |
with ( |
| 245 |
fails_raises_group( |
| 246 |
"Unexpected nested `ExceptionGroup()`, expected `ValueError`" |
| 247 |
), |
| 248 |
RaisesGroup(ValueError), |
| 249 |
): |
| 250 |
raise ExceptionGroup("", [ExceptionGroup("", [TypeError()])]) |
| 251 |
|
| 252 |
# flatten_subgroups can be suggested if nested. This will implicitly ask the user to |
| 253 |
# do `RaisesGroup(RaisesGroup(ValueError, flatten_subgroups=True))` which is unlikely |
| 254 |
# to be what they actually want - but I don't think it's worth trying to special-case |
| 255 |
with ( |
| 256 |
fails_raises_group( |
| 257 |
"RaisesGroup(ValueError): Unexpected nested `ExceptionGroup()`, expected `ValueError`\n" |
| 258 |
" Did you mean to use `flatten_subgroups=True`?", |
| 259 |
), |
| 260 |
RaisesGroup(RaisesGroup(ValueError)), |
| 261 |
): |
| 262 |
raise ExceptionGroup( |
| 263 |
"", |
| 264 |
[ExceptionGroup("", [ExceptionGroup("", [ValueError()])])], |
| 265 |
) |
| 266 |
|
| 267 |
# Don't mention "unexpected nested" if expecting an ExceptionGroup. |
| 268 |
# Although it should perhaps be an error to specify `RaisesGroup(ExceptionGroup)` in |
| 269 |
# favor of doing `RaisesGroup(RaisesGroup(...))`. |
| 270 |
with ( |
| 271 |
fails_raises_group( |
| 272 |
"`BaseExceptionGroup()` is not an instance of `ExceptionGroup`" |
| 273 |
), |
| 274 |
RaisesGroup(ExceptionGroup), |
| 275 |
): |
| 276 |
raise BaseExceptionGroup("", [BaseExceptionGroup("", [KeyboardInterrupt()])]) |
| 277 |
|
| 278 |
|
| 279 |
def test_catch_unwrapped_exceptions() -> None: |
| 280 |
# Catches lone exceptions with strict=False |
| 281 |
# just as except* would |
| 282 |
with RaisesGroup(ValueError, allow_unwrapped=True): |
| 283 |
raise ValueError |
| 284 |
|
| 285 |
# expecting multiple unwrapped exceptions is not possible |
| 286 |
with pytest.raises( |
| 287 |
ValueError, |
| 288 |
match=r"^You cannot specify multiple exceptions with", |
| 289 |
): |
| 290 |
RaisesGroup(SyntaxError, ValueError, allow_unwrapped=True) # type: ignore[call-overload] |
| 291 |
# if users want one of several exception types they need to use a RaisesExc |
| 292 |
# (which the error message suggests) |
| 293 |
with RaisesGroup( |
| 294 |
RaisesExc(check=lambda e: isinstance(e, SyntaxError | ValueError)), |
| 295 |
allow_unwrapped=True, |
| 296 |
): |
| 297 |
raise ValueError |
| 298 |
|
| 299 |
# Unwrapped nested `RaisesGroup` is likely a user error, so we raise an error. |
| 300 |
with pytest.raises(ValueError, match="has no effect when expecting"): |
| 301 |
RaisesGroup(RaisesGroup(ValueError), allow_unwrapped=True) # type: ignore[call-overload] |
| 302 |
|
| 303 |
# But it *can* be used to check for nesting level +- 1 if they move it to |
| 304 |
# the nested RaisesGroup. Users should probably use `RaisesExc`s instead though. |
| 305 |
with RaisesGroup(RaisesGroup(ValueError, allow_unwrapped=True)): |
| 306 |
raise ExceptionGroup("", [ExceptionGroup("", [ValueError()])]) |
| 307 |
with RaisesGroup(RaisesGroup(ValueError, allow_unwrapped=True)): |
| 308 |
raise ExceptionGroup("", [ValueError()]) |
| 309 |
|
| 310 |
# with allow_unwrapped=False (default) it will not be caught |
| 311 |
with ( |
| 312 |
fails_raises_group( |
| 313 |
"`ValueError()` is not an exception group, but would match with `allow_unwrapped=True`" |
| 314 |
), |
| 315 |
RaisesGroup(ValueError), |
| 316 |
): |
| 317 |
raise ValueError("value error text") |
| 318 |
|
| 319 |
# allow_unwrapped on its own won't match against nested groups |
| 320 |
with ( |
| 321 |
fails_raises_group( |
| 322 |
"Unexpected nested `ExceptionGroup()`, expected `ValueError`\n" |
| 323 |
" Did you mean to use `flatten_subgroups=True`?", |
| 324 |
), |
| 325 |
RaisesGroup(ValueError, allow_unwrapped=True), |
| 326 |
): |
| 327 |
raise ExceptionGroup("foo", [ExceptionGroup("bar", [ValueError()])]) |
| 328 |
|
| 329 |
# you need both allow_unwrapped and flatten_subgroups to fully emulate except* |
| 330 |
with RaisesGroup(ValueError, allow_unwrapped=True, flatten_subgroups=True): |
| 331 |
raise ExceptionGroup("", [ExceptionGroup("", [ValueError()])]) |
| 332 |
|
| 333 |
# code coverage |
| 334 |
with ( |
| 335 |
fails_raises_group( |
| 336 |
"Raised exception (group) did not match: `TypeError()` is not an instance of `ValueError`", |
| 337 |
add_prefix=False, |
| 338 |
), |
| 339 |
RaisesGroup(ValueError, allow_unwrapped=True), |
| 340 |
): |
| 341 |
raise TypeError("this text doesn't show up in the error message") |
| 342 |
with ( |
| 343 |
fails_raises_group( |
| 344 |
"Raised exception (group) did not match: RaisesExc(ValueError): `TypeError()` is not an instance of `ValueError`", |
| 345 |
add_prefix=False, |
| 346 |
), |
| 347 |
RaisesGroup(RaisesExc(ValueError), allow_unwrapped=True), |
| 348 |
): |
| 349 |
raise TypeError |
| 350 |
|
| 351 |
# check we don't suggest unwrapping with nested RaisesGroup |
| 352 |
with ( |
| 353 |
fails_raises_group("`ValueError()` is not an exception group"), |
| 354 |
RaisesGroup(RaisesGroup(ValueError)), |
| 355 |
): |
| 356 |
raise ValueError |
| 357 |
|
| 358 |
|
| 359 |
def test_match() -> None: |
| 360 |
# supports match string |
| 361 |
with RaisesGroup(ValueError, match="bar"): |
| 362 |
raise ExceptionGroup("bar", (ValueError(),)) |
| 363 |
|
| 364 |
# now also works with ^$ |
| 365 |
with RaisesGroup(ValueError, match="^bar$"): |
| 366 |
raise ExceptionGroup("bar", (ValueError(),)) |
| 367 |
|
| 368 |
# it also includes notes |
| 369 |
with RaisesGroup(ValueError, match="my note"): |
| 370 |
e = ExceptionGroup("bar", (ValueError(),)) |
| 371 |
e.add_note("my note") |
| 372 |
raise e |
| 373 |
|
| 374 |
# and technically you can match it all with ^$ |
| 375 |
# but you're probably better off using a RaisesExc at that point |
| 376 |
with RaisesGroup(ValueError, match="^bar\nmy note$"): |
| 377 |
e = ExceptionGroup("bar", (ValueError(),)) |
| 378 |
e.add_note("my note") |
| 379 |
raise e |
| 380 |
|
| 381 |
with ( |
| 382 |
fails_raises_group( |
| 383 |
"Regex pattern did not match the `ExceptionGroup()`.\n" |
| 384 |
" Expected regex: 'foo'\n" |
| 385 |
" Actual message: 'bar'" |
| 386 |
), |
| 387 |
RaisesGroup(ValueError, match="foo"), |
| 388 |
): |
| 389 |
raise ExceptionGroup("bar", (ValueError(),)) |
| 390 |
|
| 391 |
# Suggest a fix for easy pitfall of adding match to the RaisesGroup instead of |
| 392 |
# using a RaisesExc. |
| 393 |
# This requires a single expected & raised exception, the expected is a type, |
| 394 |
# and `isinstance(raised, expected_type)`. |
| 395 |
with ( |
| 396 |
fails_raises_group( |
| 397 |
"Regex pattern did not match the `ExceptionGroup()`.\n" |
| 398 |
" Expected regex: 'foo'\n" |
| 399 |
" Actual message: 'bar'\n" |
| 400 |
" but matched the expected `ValueError`.\n" |
| 401 |
" You might want `RaisesGroup(RaisesExc(ValueError, match='foo'))`" |
| 402 |
), |
| 403 |
RaisesGroup(ValueError, match="foo"), |
| 404 |
): |
| 405 |
raise ExceptionGroup("bar", [ValueError("foo")]) |
| 406 |
|
| 407 |
|
| 408 |
def test_check() -> None: |
| 409 |
exc = ExceptionGroup("", (ValueError(),)) |
| 410 |
|
| 411 |
def is_exc(e: ExceptionGroup[ValueError]) -> bool: |
| 412 |
return e is exc |
| 413 |
|
| 414 |
is_exc_repr = repr_callable(is_exc) |
| 415 |
with RaisesGroup(ValueError, check=is_exc): |
| 416 |
raise exc |
| 417 |
|
| 418 |
with ( |
| 419 |
fails_raises_group( |
| 420 |
f"check {is_exc_repr} did not return True on the ExceptionGroup" |
| 421 |
), |
| 422 |
RaisesGroup(ValueError, check=is_exc), |
| 423 |
): |
| 424 |
raise ExceptionGroup("", (ValueError(),)) |
| 425 |
|
| 426 |
def is_value_error(e: BaseException) -> bool: |
| 427 |
return isinstance(e, ValueError) |
| 428 |
|
| 429 |
# helpful suggestion if the user thinks the check is for the sub-exception |
| 430 |
with ( |
| 431 |
fails_raises_group( |
| 432 |
f"check {is_value_error} did not return True on the ExceptionGroup, but did return True for the expected ValueError. You might want RaisesGroup(RaisesExc(ValueError, check=<...>))" |
| 433 |
), |
| 434 |
RaisesGroup(ValueError, check=is_value_error), |
| 435 |
): |
| 436 |
raise ExceptionGroup("", (ValueError(),)) |
| 437 |
|
| 438 |
|
| 439 |
def test_unwrapped_match_check() -> None: |
| 440 |
def my_check(e: object) -> bool: # pragma: no cover |
| 441 |
return True |
| 442 |
|
| 443 |
msg = ( |
| 444 |
"`allow_unwrapped=True` bypasses the `match` and `check` parameters" |
| 445 |
" if the exception is unwrapped. If you intended to match/check the" |
| 446 |
" exception you should use a `RaisesExc` object. If you want to match/check" |
| 447 |
" the exceptiongroup when the exception *is* wrapped you need to" |
| 448 |
" do e.g. `if isinstance(exc.value, ExceptionGroup):" |
| 449 |
" assert RaisesGroup(...).matches(exc.value)` afterwards." |
| 450 |
) |
| 451 |
with pytest.raises(ValueError, match=re.escape(msg)): |
| 452 |
RaisesGroup(ValueError, allow_unwrapped=True, match="foo") # type: ignore[call-overload] |
| 453 |
with pytest.raises(ValueError, match=re.escape(msg)): |
| 454 |
RaisesGroup(ValueError, allow_unwrapped=True, check=my_check) # type: ignore[call-overload] |
| 455 |
|
| 456 |
# Users should instead use a RaisesExc |
| 457 |
rg = RaisesGroup(RaisesExc(ValueError, match="^foo$"), allow_unwrapped=True) |
| 458 |
with rg: |
| 459 |
raise ValueError("foo") |
| 460 |
with rg: |
| 461 |
raise ExceptionGroup("", [ValueError("foo")]) |
| 462 |
|
| 463 |
# or if they wanted to match/check the group, do a conditional `.matches()` |
| 464 |
with RaisesGroup(ValueError, allow_unwrapped=True) as exc: |
| 465 |
raise ExceptionGroup("bar", [ValueError("foo")]) |
| 466 |
if isinstance(exc.value, ExceptionGroup): # pragma: no branch |
| 467 |
assert RaisesGroup(ValueError, match="bar").matches(exc.value) |
| 468 |
|
| 469 |
|
| 470 |
def test_matches() -> None: |
| 471 |
rg = RaisesGroup(ValueError) |
| 472 |
assert not rg.matches(None) |
| 473 |
assert not rg.matches(ValueError()) |
| 474 |
assert rg.matches(ExceptionGroup("", (ValueError(),))) |
| 475 |
|
| 476 |
re = RaisesExc(ValueError) |
| 477 |
assert not re.matches(None) |
| 478 |
assert re.matches(ValueError()) |
| 479 |
|
| 480 |
|
| 481 |
def test_message() -> None: |
| 482 |
def check_message( |
| 483 |
message: str, |
| 484 |
body: RaisesGroup[BaseException], |
| 485 |
) -> None: |
| 486 |
with ( |
| 487 |
pytest.raises( |
| 488 |
Failed, |
| 489 |
match=f"^DID NOT RAISE any exception, expected `{re.escape(message)}`$", |
| 490 |
), |
| 491 |
body, |
| 492 |
): |
| 493 |
... |
| 494 |
|
| 495 |
# basic |
| 496 |
check_message("ExceptionGroup(ValueError)", RaisesGroup(ValueError)) |
| 497 |
# multiple exceptions |
| 498 |
check_message( |
| 499 |
"ExceptionGroup(ValueError, ValueError)", |
| 500 |
RaisesGroup(ValueError, ValueError), |
| 501 |
) |
| 502 |
# nested |
| 503 |
check_message( |
| 504 |
"ExceptionGroup(ExceptionGroup(ValueError))", |
| 505 |
RaisesGroup(RaisesGroup(ValueError)), |
| 506 |
) |
| 507 |
|
| 508 |
# RaisesExc |
| 509 |
check_message( |
| 510 |
"ExceptionGroup(RaisesExc(ValueError, match='my_str'))", |
| 511 |
RaisesGroup(RaisesExc(ValueError, match="my_str")), |
| 512 |
) |
| 513 |
check_message( |
| 514 |
"ExceptionGroup(RaisesExc(match='my_str'))", |
| 515 |
RaisesGroup(RaisesExc(match="my_str")), |
| 516 |
) |
| 517 |
# one-size tuple is printed as not being a tuple |
| 518 |
check_message( |
| 519 |
"ExceptionGroup(RaisesExc(ValueError))", |
| 520 |
RaisesGroup(RaisesExc((ValueError,))), |
| 521 |
) |
| 522 |
check_message( |
| 523 |
"ExceptionGroup(RaisesExc((ValueError, IndexError)))", |
| 524 |
RaisesGroup(RaisesExc((ValueError, IndexError))), |
| 525 |
) |
| 526 |
|
| 527 |
# BaseExceptionGroup |
| 528 |
check_message( |
| 529 |
"BaseExceptionGroup(KeyboardInterrupt)", |
| 530 |
RaisesGroup(KeyboardInterrupt), |
| 531 |
) |
| 532 |
# BaseExceptionGroup with type inside RaisesExc |
| 533 |
check_message( |
| 534 |
"BaseExceptionGroup(RaisesExc(KeyboardInterrupt))", |
| 535 |
RaisesGroup(RaisesExc(KeyboardInterrupt)), |
| 536 |
) |
| 537 |
check_message( |
| 538 |
"BaseExceptionGroup(RaisesExc((ValueError, KeyboardInterrupt)))", |
| 539 |
RaisesGroup(RaisesExc((ValueError, KeyboardInterrupt))), |
| 540 |
) |
| 541 |
# Base-ness transfers to parent containers |
| 542 |
check_message( |
| 543 |
"BaseExceptionGroup(BaseExceptionGroup(KeyboardInterrupt))", |
| 544 |
RaisesGroup(RaisesGroup(KeyboardInterrupt)), |
| 545 |
) |
| 546 |
# but not to child containers |
| 547 |
check_message( |
| 548 |
"BaseExceptionGroup(BaseExceptionGroup(KeyboardInterrupt), ExceptionGroup(ValueError))", |
| 549 |
RaisesGroup(RaisesGroup(KeyboardInterrupt), RaisesGroup(ValueError)), |
| 550 |
) |
| 551 |
|
| 552 |
|
| 553 |
def test_assert_message() -> None: |
| 554 |
# the message does not need to list all parameters to RaisesGroup, nor all exceptions |
| 555 |
# in the exception group, as those are both visible in the traceback. |
| 556 |
# first fails to match |
| 557 |
with ( |
| 558 |
fails_raises_group("`TypeError()` is not an instance of `ValueError`"), |
| 559 |
RaisesGroup(ValueError), |
| 560 |
): |
| 561 |
raise ExceptionGroup("a", [TypeError()]) |
| 562 |
with ( |
| 563 |
fails_raises_group( |
| 564 |
"Raised exception group did not match: \n" |
| 565 |
"The following expected exceptions did not find a match:\n" |
| 566 |
" RaisesGroup(ValueError)\n" |
| 567 |
" RaisesGroup(ValueError, match='a')\n" |
| 568 |
"The following raised exceptions did not find a match\n" |
| 569 |
" ExceptionGroup('', [RuntimeError()]):\n" |
| 570 |
" RaisesGroup(ValueError): `RuntimeError()` is not an instance of `ValueError`\n" |
| 571 |
" RaisesGroup(ValueError, match='a'): Regex pattern did not match the `ExceptionGroup()`.\n" |
| 572 |
" Expected regex: 'a'\n" |
| 573 |
" Actual message: ''\n" |
| 574 |
" RuntimeError():\n" |
| 575 |
" RaisesGroup(ValueError): `RuntimeError()` is not an exception group\n" |
| 576 |
" RaisesGroup(ValueError, match='a'): `RuntimeError()` is not an exception group", |
| 577 |
add_prefix=False, # to see the full structure |
| 578 |
), |
| 579 |
RaisesGroup(RaisesGroup(ValueError), RaisesGroup(ValueError, match="a")), |
| 580 |
): |
| 581 |
raise ExceptionGroup( |
| 582 |
"", |
| 583 |
[ExceptionGroup("", [RuntimeError()]), RuntimeError()], |
| 584 |
) |
| 585 |
|
| 586 |
with ( |
| 587 |
fails_raises_group( |
| 588 |
"Raised exception group did not match: \n" |
| 589 |
"2 matched exceptions. \n" |
| 590 |
"The following expected exceptions did not find a match:\n" |
| 591 |
" RaisesGroup(RuntimeError)\n" |
| 592 |
" RaisesGroup(ValueError)\n" |
| 593 |
"The following raised exceptions did not find a match\n" |
| 594 |
" RuntimeError():\n" |
| 595 |
" RaisesGroup(RuntimeError): `RuntimeError()` is not an exception group, but would match with `allow_unwrapped=True`\n" |
| 596 |
" RaisesGroup(ValueError): `RuntimeError()` is not an exception group\n" |
| 597 |
" ValueError('bar'):\n" |
| 598 |
" It matches `ValueError` which was paired with `ValueError('foo')`\n" |
| 599 |
" RaisesGroup(RuntimeError): `ValueError()` is not an exception group\n" |
| 600 |
" RaisesGroup(ValueError): `ValueError()` is not an exception group, but would match with `allow_unwrapped=True`", |
| 601 |
add_prefix=False, # to see the full structure |
| 602 |
), |
| 603 |
RaisesGroup( |
| 604 |
ValueError, |
| 605 |
RaisesExc(TypeError), |
| 606 |
RaisesGroup(RuntimeError), |
| 607 |
RaisesGroup(ValueError), |
| 608 |
), |
| 609 |
): |
| 610 |
raise ExceptionGroup( |
| 611 |
"a", |
| 612 |
[RuntimeError(), TypeError(), ValueError("foo"), ValueError("bar")], |
| 613 |
) |
| 614 |
|
| 615 |
with ( |
| 616 |
fails_raises_group( |
| 617 |
"1 matched exception. `AssertionError()` is not an instance of `TypeError`" |
| 618 |
), |
| 619 |
RaisesGroup(ValueError, TypeError), |
| 620 |
): |
| 621 |
raise ExceptionGroup("a", [ValueError(), AssertionError()]) |
| 622 |
|
| 623 |
with ( |
| 624 |
fails_raises_group( |
| 625 |
"RaisesExc(ValueError): `TypeError()` is not an instance of `ValueError`" |
| 626 |
), |
| 627 |
RaisesGroup(RaisesExc(ValueError)), |
| 628 |
): |
| 629 |
raise ExceptionGroup("a", [TypeError()]) |
| 630 |
|
| 631 |
# suggest escaping |
| 632 |
with ( |
| 633 |
fails_raises_group( |
| 634 |
# TODO: did not match Exceptiongroup('h(ell)o', ...) ? |
| 635 |
"Raised exception group did not match: Regex pattern did not match the `ExceptionGroup()`.\n" |
| 636 |
" Expected regex: 'h(ell)o'\n" |
| 637 |
" Actual message: 'h(ell)o'\n" |
| 638 |
" Did you mean to `re.escape()` the regex?", |
| 639 |
add_prefix=False, # to see the full structure |
| 640 |
), |
| 641 |
RaisesGroup(ValueError, match="h(ell)o"), |
| 642 |
): |
| 643 |
raise ExceptionGroup("h(ell)o", [ValueError()]) |
| 644 |
with ( |
| 645 |
fails_raises_group( |
| 646 |
"RaisesExc(match='h(ell)o'): Regex pattern did not match.\n" |
| 647 |
" Expected regex: 'h(ell)o'\n" |
| 648 |
" Actual message: 'h(ell)o'\n" |
| 649 |
" Did you mean to `re.escape()` the regex?", |
| 650 |
), |
| 651 |
RaisesGroup(RaisesExc(match="h(ell)o")), |
| 652 |
): |
| 653 |
raise ExceptionGroup("", [ValueError("h(ell)o")]) |
| 654 |
|
| 655 |
with ( |
| 656 |
fails_raises_group( |
| 657 |
"Raised exception group did not match: \n" |
| 658 |
"The following expected exceptions did not find a match:\n" |
| 659 |
" ValueError\n" |
| 660 |
" ValueError\n" |
| 661 |
" ValueError\n" |
| 662 |
" ValueError\n" |
| 663 |
"The following raised exceptions did not find a match\n" |
| 664 |
" ExceptionGroup('', [ValueError(), TypeError()]):\n" |
| 665 |
" Unexpected nested `ExceptionGroup()`, expected `ValueError`\n" |
| 666 |
" Unexpected nested `ExceptionGroup()`, expected `ValueError`\n" |
| 667 |
" Unexpected nested `ExceptionGroup()`, expected `ValueError`\n" |
| 668 |
" Unexpected nested `ExceptionGroup()`, expected `ValueError`", |
| 669 |
add_prefix=False, # to see the full structure |
| 670 |
), |
| 671 |
RaisesGroup(ValueError, ValueError, ValueError, ValueError), |
| 672 |
): |
| 673 |
raise ExceptionGroup("", [ExceptionGroup("", [ValueError(), TypeError()])]) |
| 674 |
|
| 675 |
|
| 676 |
def test_message_indent() -> None: |
| 677 |
with ( |
| 678 |
fails_raises_group( |
| 679 |
"Raised exception group did not match: \n" |
| 680 |
"The following expected exceptions did not find a match:\n" |
| 681 |
" RaisesGroup(ValueError, ValueError)\n" |
| 682 |
" ValueError\n" |
| 683 |
"The following raised exceptions did not find a match\n" |
| 684 |
" ExceptionGroup('', [TypeError(), RuntimeError()]):\n" |
| 685 |
" RaisesGroup(ValueError, ValueError): \n" |
| 686 |
" The following expected exceptions did not find a match:\n" |
| 687 |
" ValueError\n" |
| 688 |
" ValueError\n" |
| 689 |
" The following raised exceptions did not find a match\n" |
| 690 |
" TypeError():\n" |
| 691 |
" `TypeError()` is not an instance of `ValueError`\n" |
| 692 |
" `TypeError()` is not an instance of `ValueError`\n" |
| 693 |
" RuntimeError():\n" |
| 694 |
" `RuntimeError()` is not an instance of `ValueError`\n" |
| 695 |
" `RuntimeError()` is not an instance of `ValueError`\n" |
| 696 |
# TODO: this line is not great, should maybe follow the same format as the other and say |
| 697 |
# ValueError: Unexpected nested `ExceptionGroup()` (?) |
| 698 |
" Unexpected nested `ExceptionGroup()`, expected `ValueError`\n" |
| 699 |
" TypeError():\n" |
| 700 |
" RaisesGroup(ValueError, ValueError): `TypeError()` is not an exception group\n" |
| 701 |
" `TypeError()` is not an instance of `ValueError`", |
| 702 |
add_prefix=False, |
| 703 |
), |
| 704 |
RaisesGroup( |
| 705 |
RaisesGroup(ValueError, ValueError), |
| 706 |
ValueError, |
| 707 |
), |
| 708 |
): |
| 709 |
raise ExceptionGroup( |
| 710 |
"", |
| 711 |
[ |
| 712 |
ExceptionGroup("", [TypeError(), RuntimeError()]), |
| 713 |
TypeError(), |
| 714 |
], |
| 715 |
) |
| 716 |
with ( |
| 717 |
fails_raises_group( |
| 718 |
"Raised exception group did not match: \n" |
| 719 |
"RaisesGroup(ValueError, ValueError): \n" |
| 720 |
" The following expected exceptions did not find a match:\n" |
| 721 |
" ValueError\n" |
| 722 |
" ValueError\n" |
| 723 |
" The following raised exceptions did not find a match\n" |
| 724 |
" TypeError():\n" |
| 725 |
" `TypeError()` is not an instance of `ValueError`\n" |
| 726 |
" `TypeError()` is not an instance of `ValueError`\n" |
| 727 |
" RuntimeError():\n" |
| 728 |
" `RuntimeError()` is not an instance of `ValueError`\n" |
| 729 |
" `RuntimeError()` is not an instance of `ValueError`", |
| 730 |
add_prefix=False, |
| 731 |
), |
| 732 |
RaisesGroup( |
| 733 |
RaisesGroup(ValueError, ValueError), |
| 734 |
), |
| 735 |
): |
| 736 |
raise ExceptionGroup( |
| 737 |
"", |
| 738 |
[ |
| 739 |
ExceptionGroup("", [TypeError(), RuntimeError()]), |
| 740 |
], |
| 741 |
) |
| 742 |
|
| 743 |
|
| 744 |
def test_suggestion_on_nested_and_brief_error() -> None: |
| 745 |
# Make sure "Did you mean" suggestion gets indented iff it follows a single-line error |
| 746 |
with ( |
| 747 |
fails_raises_group( |
| 748 |
"\n" |
| 749 |
"The following expected exceptions did not find a match:\n" |
| 750 |
" RaisesGroup(ValueError)\n" |
| 751 |
" ValueError\n" |
| 752 |
"The following raised exceptions did not find a match\n" |
| 753 |
" ExceptionGroup('', [ExceptionGroup('', [ValueError()])]):\n" |
| 754 |
" RaisesGroup(ValueError): Unexpected nested `ExceptionGroup()`, expected `ValueError`\n" |
| 755 |
" Did you mean to use `flatten_subgroups=True`?\n" |
| 756 |
" Unexpected nested `ExceptionGroup()`, expected `ValueError`", |
| 757 |
), |
| 758 |
RaisesGroup(RaisesGroup(ValueError), ValueError), |
| 759 |
): |
| 760 |
raise ExceptionGroup( |
| 761 |
"", |
| 762 |
[ExceptionGroup("", [ExceptionGroup("", [ValueError()])])], |
| 763 |
) |
| 764 |
# if indented here it would look like another raised exception |
| 765 |
with ( |
| 766 |
fails_raises_group( |
| 767 |
"\n" |
| 768 |
"The following expected exceptions did not find a match:\n" |
| 769 |
" RaisesGroup(ValueError, ValueError)\n" |
| 770 |
" ValueError\n" |
| 771 |
"The following raised exceptions did not find a match\n" |
| 772 |
" ExceptionGroup('', [ValueError(), ExceptionGroup('', [ValueError()])]):\n" |
| 773 |
" RaisesGroup(ValueError, ValueError): \n" |
| 774 |
" 1 matched exception. \n" |
| 775 |
" The following expected exceptions did not find a match:\n" |
| 776 |
" ValueError\n" |
| 777 |
" It matches `ValueError()` which was paired with `ValueError`\n" |
| 778 |
" The following raised exceptions did not find a match\n" |
| 779 |
" ExceptionGroup('', [ValueError()]):\n" |
| 780 |
" Unexpected nested `ExceptionGroup()`, expected `ValueError`\n" |
| 781 |
" Did you mean to use `flatten_subgroups=True`?\n" |
| 782 |
" Unexpected nested `ExceptionGroup()`, expected `ValueError`" |
| 783 |
), |
| 784 |
RaisesGroup(RaisesGroup(ValueError, ValueError), ValueError), |
| 785 |
): |
| 786 |
raise ExceptionGroup( |
| 787 |
"", |
| 788 |
[ExceptionGroup("", [ValueError(), ExceptionGroup("", [ValueError()])])], |
| 789 |
) |
| 790 |
|
| 791 |
# re.escape always comes after single-line errors |
| 792 |
with ( |
| 793 |
fails_raises_group( |
| 794 |
"\n" |
| 795 |
"The following expected exceptions did not find a match:\n" |
| 796 |
" RaisesGroup(Exception, match='^hello')\n" |
| 797 |
" ValueError\n" |
| 798 |
"The following raised exceptions did not find a match\n" |
| 799 |
" ExceptionGroup('^hello', [Exception()]):\n" |
| 800 |
" RaisesGroup(Exception, match='^hello'): Regex pattern did not match the `ExceptionGroup()`.\n" |
| 801 |
" Expected regex: '^hello'\n" |
| 802 |
" Actual message: '^hello'\n" |
| 803 |
" Did you mean to `re.escape()` the regex?\n" |
| 804 |
" Unexpected nested `ExceptionGroup()`, expected `ValueError`" |
| 805 |
), |
| 806 |
RaisesGroup(RaisesGroup(Exception, match="^hello"), ValueError), |
| 807 |
): |
| 808 |
raise ExceptionGroup("", [ExceptionGroup("^hello", [Exception()])]) |
| 809 |
|
| 810 |
|
| 811 |
def test_assert_message_nested() -> None: |
| 812 |
# we only get one instance of aaaaaaaaaa... and bbbbbb..., but we do get multiple instances of ccccc... and dddddd.. |
| 813 |
# but I think this now only prints the full repr when that is necessary to disambiguate exceptions |
| 814 |
with ( |
| 815 |
fails_raises_group( |
| 816 |
"Raised exception group did not match: \n" |
| 817 |
"The following expected exceptions did not find a match:\n" |
| 818 |
" RaisesGroup(ValueError)\n" |
| 819 |
" RaisesGroup(RaisesGroup(ValueError))\n" |
| 820 |
" RaisesGroup(RaisesExc(TypeError, match='foo'))\n" |
| 821 |
" RaisesGroup(TypeError, ValueError)\n" |
| 822 |
"The following raised exceptions did not find a match\n" |
| 823 |
" TypeError('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'):\n" |
| 824 |
" RaisesGroup(ValueError): `TypeError()` is not an exception group\n" |
| 825 |
" RaisesGroup(RaisesGroup(ValueError)): `TypeError()` is not an exception group\n" |
| 826 |
" RaisesGroup(RaisesExc(TypeError, match='foo')): `TypeError()` is not an exception group\n" |
| 827 |
" RaisesGroup(TypeError, ValueError): `TypeError()` is not an exception group\n" |
| 828 |
" ExceptionGroup('Exceptions from Trio nursery', [TypeError('bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb')]):\n" |
| 829 |
" RaisesGroup(ValueError): `TypeError()` is not an instance of `ValueError`\n" |
| 830 |
" RaisesGroup(RaisesGroup(ValueError)): RaisesGroup(ValueError): `TypeError()` is not an exception group\n" |
| 831 |
" RaisesGroup(RaisesExc(TypeError, match='foo')): RaisesExc(TypeError, match='foo'): Regex pattern did not match.\n" |
| 832 |
" Expected regex: 'foo'\n" |
| 833 |
" Actual message: 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb'\n" |
| 834 |
" RaisesGroup(TypeError, ValueError): 1 matched exception. Too few exceptions raised, found no match for: [ValueError]\n" |
| 835 |
" ExceptionGroup('Exceptions from Trio nursery', [TypeError('cccccccccccccccccccccccccccccc'), TypeError('dddddddddddddddddddddddddddddd')]):\n" |
| 836 |
" RaisesGroup(ValueError): \n" |
| 837 |
" The following expected exceptions did not find a match:\n" |
| 838 |
" ValueError\n" |
| 839 |
" The following raised exceptions did not find a match\n" |
| 840 |
" TypeError('cccccccccccccccccccccccccccccc'):\n" |
| 841 |
" `TypeError()` is not an instance of `ValueError`\n" |
| 842 |
" TypeError('dddddddddddddddddddddddddddddd'):\n" |
| 843 |
" `TypeError()` is not an instance of `ValueError`\n" |
| 844 |
" RaisesGroup(RaisesGroup(ValueError)): \n" |
| 845 |
" The following expected exceptions did not find a match:\n" |
| 846 |
" RaisesGroup(ValueError)\n" |
| 847 |
" The following raised exceptions did not find a match\n" |
| 848 |
" TypeError('cccccccccccccccccccccccccccccc'):\n" |
| 849 |
" RaisesGroup(ValueError): `TypeError()` is not an exception group\n" |
| 850 |
" TypeError('dddddddddddddddddddddddddddddd'):\n" |
| 851 |
" RaisesGroup(ValueError): `TypeError()` is not an exception group\n" |
| 852 |
" RaisesGroup(RaisesExc(TypeError, match='foo')): \n" |
| 853 |
" The following expected exceptions did not find a match:\n" |
| 854 |
" RaisesExc(TypeError, match='foo')\n" |
| 855 |
" The following raised exceptions did not find a match\n" |
| 856 |
" TypeError('cccccccccccccccccccccccccccccc'):\n" |
| 857 |
" RaisesExc(TypeError, match='foo'): Regex pattern did not match.\n" |
| 858 |
" Expected regex: 'foo'\n" |
| 859 |
" Actual message: 'cccccccccccccccccccccccccccccc'\n" |
| 860 |
" TypeError('dddddddddddddddddddddddddddddd'):\n" |
| 861 |
" RaisesExc(TypeError, match='foo'): Regex pattern did not match.\n" |
| 862 |
" Expected regex: 'foo'\n" |
| 863 |
" Actual message: 'dddddddddddddddddddddddddddddd'\n" |
| 864 |
" RaisesGroup(TypeError, ValueError): \n" |
| 865 |
" 1 matched exception. \n" |
| 866 |
" The following expected exceptions did not find a match:\n" |
| 867 |
" ValueError\n" |
| 868 |
" The following raised exceptions did not find a match\n" |
| 869 |
" TypeError('dddddddddddddddddddddddddddddd'):\n" |
| 870 |
" It matches `TypeError` which was paired with `TypeError('cccccccccccccccccccccccccccccc')`\n" |
| 871 |
" `TypeError()` is not an instance of `ValueError`", |
| 872 |
add_prefix=False, # to see the full structure |
| 873 |
), |
| 874 |
RaisesGroup( |
| 875 |
RaisesGroup(ValueError), |
| 876 |
RaisesGroup(RaisesGroup(ValueError)), |
| 877 |
RaisesGroup(RaisesExc(TypeError, match="foo")), |
| 878 |
RaisesGroup(TypeError, ValueError), |
| 879 |
), |
| 880 |
): |
| 881 |
raise ExceptionGroup( |
| 882 |
"", |
| 883 |
[ |
| 884 |
TypeError("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"), |
| 885 |
ExceptionGroup( |
| 886 |
"Exceptions from Trio nursery", |
| 887 |
[TypeError("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb")], |
| 888 |
), |
| 889 |
ExceptionGroup( |
| 890 |
"Exceptions from Trio nursery", |
| 891 |
[ |
| 892 |
TypeError("cccccccccccccccccccccccccccccc"), |
| 893 |
TypeError("dddddddddddddddddddddddddddddd"), |
| 894 |
], |
| 895 |
), |
| 896 |
], |
| 897 |
) |
| 898 |
|
| 899 |
|
| 900 |
# CI always runs with hypothesis, but this is not a critical test - it overlaps |
| 901 |
# with several others |
| 902 |
@pytest.mark.skipif( |
| 903 |
"hypothesis" in sys.modules, |
| 904 |
reason="hypothesis may have monkeypatched _check_repr", |
| 905 |
) |
| 906 |
def test_check_no_patched_repr() -> None: # pragma: no cover |
| 907 |
# We make `_check_repr` monkeypatchable to avoid this very ugly and verbose |
| 908 |
# repr. The other tests that use `check` make use of `_check_repr` so they'll |
| 909 |
# continue passing in case it is patched - but we have this one test that |
| 910 |
# demonstrates just how nasty it gets otherwise. |
| 911 |
match_str = ( |
| 912 |
r"^Raised exception group did not match: \n" |
| 913 |
r"The following expected exceptions did not find a match:\n" |
| 914 |
r" RaisesExc\(check=<function test_check_no_patched_repr.<locals>.<lambda> at .*>\)\n" |
| 915 |
r" TypeError\n" |
| 916 |
r"The following raised exceptions did not find a match\n" |
| 917 |
r" ValueError\('foo'\):\n" |
| 918 |
r" RaisesExc\(check=<function test_check_no_patched_repr.<locals>.<lambda> at .*>\): check did not return True\n" |
| 919 |
r" `ValueError\(\)` is not an instance of `TypeError`\n" |
| 920 |
r" ValueError\('bar'\):\n" |
| 921 |
r" RaisesExc\(check=<function test_check_no_patched_repr.<locals>.<lambda> at .*>\): check did not return True\n" |
| 922 |
r" `ValueError\(\)` is not an instance of `TypeError`$" |
| 923 |
) |
| 924 |
with ( |
| 925 |
pytest.raises(Failed, match=match_str), |
| 926 |
RaisesGroup(RaisesExc(check=lambda x: False), TypeError), |
| 927 |
): |
| 928 |
raise ExceptionGroup("", [ValueError("foo"), ValueError("bar")]) |
| 929 |
|
| 930 |
|
| 931 |
def test_misordering_example() -> None: |
| 932 |
with ( |
| 933 |
fails_raises_group( |
| 934 |
"\n" |
| 935 |
"3 matched exceptions. \n" |
| 936 |
"The following expected exceptions did not find a match:\n" |
| 937 |
" RaisesExc(ValueError, match='foo')\n" |
| 938 |
" It matches `ValueError('foo')` which was paired with `ValueError`\n" |
| 939 |
" It matches `ValueError('foo')` which was paired with `ValueError`\n" |
| 940 |
" It matches `ValueError('foo')` which was paired with `ValueError`\n" |
| 941 |
"The following raised exceptions did not find a match\n" |
| 942 |
" ValueError('bar'):\n" |
| 943 |
" It matches `ValueError` which was paired with `ValueError('foo')`\n" |
| 944 |
" It matches `ValueError` which was paired with `ValueError('foo')`\n" |
| 945 |
" It matches `ValueError` which was paired with `ValueError('foo')`\n" |
| 946 |
" RaisesExc(ValueError, match='foo'): Regex pattern did not match.\n" |
| 947 |
" Expected regex: 'foo'\n" |
| 948 |
" Actual message: 'bar'\n" |
| 949 |
"There exist a possible match when attempting an exhaustive check, but RaisesGroup uses a greedy algorithm. Please make your expected exceptions more stringent with `RaisesExc` etc so the greedy algorithm can function." |
| 950 |
), |
| 951 |
RaisesGroup( |
| 952 |
ValueError, ValueError, ValueError, RaisesExc(ValueError, match="foo") |
| 953 |
), |
| 954 |
): |
| 955 |
raise ExceptionGroup( |
| 956 |
"", |
| 957 |
[ |
| 958 |
ValueError("foo"), |
| 959 |
ValueError("foo"), |
| 960 |
ValueError("foo"), |
| 961 |
ValueError("bar"), |
| 962 |
], |
| 963 |
) |
| 964 |
|
| 965 |
|
| 966 |
def test_brief_error_on_one_fail() -> None: |
| 967 |
"""If only one raised and one expected fail to match up, we print a full table iff |
| 968 |
the raised exception would match one of the expected that previously got matched""" |
| 969 |
# no also-matched |
| 970 |
with ( |
| 971 |
fails_raises_group( |
| 972 |
"1 matched exception. `TypeError()` is not an instance of `RuntimeError`" |
| 973 |
), |
| 974 |
RaisesGroup(ValueError, RuntimeError), |
| 975 |
): |
| 976 |
raise ExceptionGroup("", [ValueError(), TypeError()]) |
| 977 |
|
| 978 |
# raised would match an expected |
| 979 |
with ( |
| 980 |
fails_raises_group( |
| 981 |
"\n" |
| 982 |
"1 matched exception. \n" |
| 983 |
"The following expected exceptions did not find a match:\n" |
| 984 |
" RuntimeError\n" |
| 985 |
"The following raised exceptions did not find a match\n" |
| 986 |
" TypeError():\n" |
| 987 |
" It matches `Exception` which was paired with `ValueError()`\n" |
| 988 |
" `TypeError()` is not an instance of `RuntimeError`" |
| 989 |
), |
| 990 |
RaisesGroup(Exception, RuntimeError), |
| 991 |
): |
| 992 |
raise ExceptionGroup("", [ValueError(), TypeError()]) |
| 993 |
|
| 994 |
# expected would match a raised |
| 995 |
with ( |
| 996 |
fails_raises_group( |
| 997 |
"\n" |
| 998 |
"1 matched exception. \n" |
| 999 |
"The following expected exceptions did not find a match:\n" |
| 1000 |
" ValueError\n" |
| 1001 |
" It matches `ValueError()` which was paired with `ValueError`\n" |
| 1002 |
"The following raised exceptions did not find a match\n" |
| 1003 |
" TypeError():\n" |
| 1004 |
" `TypeError()` is not an instance of `ValueError`" |
| 1005 |
), |
| 1006 |
RaisesGroup(ValueError, ValueError), |
| 1007 |
): |
| 1008 |
raise ExceptionGroup("", [ValueError(), TypeError()]) |
| 1009 |
|
| 1010 |
|
| 1011 |
def test_identity_oopsies() -> None: |
| 1012 |
# it's both possible to have several instances of the same exception in the same group |
| 1013 |
# and to expect multiple of the same type |
| 1014 |
# this previously messed up the logic |
| 1015 |
|
| 1016 |
with ( |
| 1017 |
fails_raises_group( |
| 1018 |
"3 matched exceptions. `RuntimeError()` is not an instance of `TypeError`" |
| 1019 |
), |
| 1020 |
RaisesGroup(ValueError, ValueError, ValueError, TypeError), |
| 1021 |
): |
| 1022 |
raise ExceptionGroup( |
| 1023 |
"", [ValueError(), ValueError(), ValueError(), RuntimeError()] |
| 1024 |
) |
| 1025 |
|
| 1026 |
e = ValueError("foo") |
| 1027 |
m = RaisesExc(match="bar") |
| 1028 |
with ( |
| 1029 |
fails_raises_group( |
| 1030 |
"\n" |
| 1031 |
"The following expected exceptions did not find a match:\n" |
| 1032 |
" RaisesExc(match='bar')\n" |
| 1033 |
" RaisesExc(match='bar')\n" |
| 1034 |
" RaisesExc(match='bar')\n" |
| 1035 |
"The following raised exceptions did not find a match\n" |
| 1036 |
" ValueError('foo'):\n" |
| 1037 |
" RaisesExc(match='bar'): Regex pattern did not match.\n" |
| 1038 |
" Expected regex: 'bar'\n" |
| 1039 |
" Actual message: 'foo'\n" |
| 1040 |
" RaisesExc(match='bar'): Regex pattern did not match.\n" |
| 1041 |
" Expected regex: 'bar'\n" |
| 1042 |
" Actual message: 'foo'\n" |
| 1043 |
" RaisesExc(match='bar'): Regex pattern did not match.\n" |
| 1044 |
" Expected regex: 'bar'\n" |
| 1045 |
" Actual message: 'foo'\n" |
| 1046 |
" ValueError('foo'):\n" |
| 1047 |
" RaisesExc(match='bar'): Regex pattern did not match.\n" |
| 1048 |
" Expected regex: 'bar'\n" |
| 1049 |
" Actual message: 'foo'\n" |
| 1050 |
" RaisesExc(match='bar'): Regex pattern did not match.\n" |
| 1051 |
" Expected regex: 'bar'\n" |
| 1052 |
" Actual message: 'foo'\n" |
| 1053 |
" RaisesExc(match='bar'): Regex pattern did not match.\n" |
| 1054 |
" Expected regex: 'bar'\n" |
| 1055 |
" Actual message: 'foo'\n" |
| 1056 |
" ValueError('foo'):\n" |
| 1057 |
" RaisesExc(match='bar'): Regex pattern did not match.\n" |
| 1058 |
" Expected regex: 'bar'\n" |
| 1059 |
" Actual message: 'foo'\n" |
| 1060 |
" RaisesExc(match='bar'): Regex pattern did not match.\n" |
| 1061 |
" Expected regex: 'bar'\n" |
| 1062 |
" Actual message: 'foo'\n" |
| 1063 |
" RaisesExc(match='bar'): Regex pattern did not match.\n" |
| 1064 |
" Expected regex: 'bar'\n" |
| 1065 |
" Actual message: 'foo'" |
| 1066 |
), |
| 1067 |
RaisesGroup(m, m, m), |
| 1068 |
): |
| 1069 |
raise ExceptionGroup("", [e, e, e]) |
| 1070 |
|
| 1071 |
|
| 1072 |
def test_raisesexc() -> None: |
| 1073 |
with pytest.raises( |
| 1074 |
ValueError, |
| 1075 |
match=r"^You must specify at least one parameter to match on.$", |
| 1076 |
): |
| 1077 |
RaisesExc() # type: ignore[call-overload] |
| 1078 |
with pytest.raises( |
| 1079 |
ValueError, |
| 1080 |
match=wrap_escape("Expected a BaseException type, but got 'object'"), |
| 1081 |
): |
| 1082 |
RaisesExc(object) # type: ignore[type-var] |
| 1083 |
|
| 1084 |
with RaisesGroup(RaisesExc(ValueError)): |
| 1085 |
raise ExceptionGroup("", (ValueError(),)) |
| 1086 |
with ( |
| 1087 |
fails_raises_group( |
| 1088 |
"RaisesExc(TypeError): `ValueError()` is not an instance of `TypeError`" |
| 1089 |
), |
| 1090 |
RaisesGroup(RaisesExc(TypeError)), |
| 1091 |
): |
| 1092 |
raise ExceptionGroup("", (ValueError(),)) |
| 1093 |
|
| 1094 |
with RaisesExc(ValueError): |
| 1095 |
raise ValueError |
| 1096 |
|
| 1097 |
with pytest.raises(Failed, match=wrap_escape("DID NOT RAISE ValueError")): |
| 1098 |
with RaisesExc(ValueError): |
| 1099 |
... |
| 1100 |
|
| 1101 |
with pytest.raises(Failed, match=wrap_escape("DID NOT RAISE any exception")): |
| 1102 |
with RaisesExc(match="foo"): |
| 1103 |
... |
| 1104 |
|
| 1105 |
with pytest.raises( |
| 1106 |
Failed, |
| 1107 |
match=wrap_escape("DID NOT RAISE any of (ValueError, TypeError)"), |
| 1108 |
): |
| 1109 |
with RaisesExc((ValueError, TypeError)): |
| 1110 |
... |
| 1111 |
|
| 1112 |
# currently RaisesGroup says "Raised exception did not match" but RaisesExc doesn't... |
| 1113 |
with pytest.raises( |
| 1114 |
AssertionError, |
| 1115 |
match=wrap_escape( |
| 1116 |
"Regex pattern did not match.\n Expected regex: 'foo'\n Actual message: 'bar'" |
| 1117 |
), |
| 1118 |
): |
| 1119 |
with RaisesExc(TypeError, match="foo"): |
| 1120 |
raise TypeError("bar") |
| 1121 |
|
| 1122 |
|
| 1123 |
def test_raisesexc_match() -> None: |
| 1124 |
with RaisesGroup(RaisesExc(ValueError, match="foo")): |
| 1125 |
raise ExceptionGroup("", (ValueError("foo"),)) |
| 1126 |
with ( |
| 1127 |
fails_raises_group( |
| 1128 |
"RaisesExc(ValueError, match='foo'): Regex pattern did not match.\n" |
| 1129 |
" Expected regex: 'foo'\n" |
| 1130 |
" Actual message: 'bar'" |
| 1131 |
), |
| 1132 |
RaisesGroup(RaisesExc(ValueError, match="foo")), |
| 1133 |
): |
| 1134 |
raise ExceptionGroup("", (ValueError("bar"),)) |
| 1135 |
|
| 1136 |
# Can be used without specifying the type |
| 1137 |
with RaisesGroup(RaisesExc(match="foo")): |
| 1138 |
raise ExceptionGroup("", (ValueError("foo"),)) |
| 1139 |
with ( |
| 1140 |
fails_raises_group( |
| 1141 |
"RaisesExc(match='foo'): Regex pattern did not match.\n" |
| 1142 |
" Expected regex: 'foo'\n" |
| 1143 |
" Actual message: 'bar'" |
| 1144 |
), |
| 1145 |
RaisesGroup(RaisesExc(match="foo")), |
| 1146 |
): |
| 1147 |
raise ExceptionGroup("", (ValueError("bar"),)) |
| 1148 |
|
| 1149 |
# check ^$ |
| 1150 |
with RaisesGroup(RaisesExc(ValueError, match="^bar$")): |
| 1151 |
raise ExceptionGroup("", [ValueError("bar")]) |
| 1152 |
with ( |
| 1153 |
fails_raises_group( |
| 1154 |
"\nRaisesExc(ValueError, match='^bar$'): \n - barr\n ? -\n + bar" |
| 1155 |
), |
| 1156 |
RaisesGroup(RaisesExc(ValueError, match="^bar$")), |
| 1157 |
): |
| 1158 |
raise ExceptionGroup("", [ValueError("barr")]) |
| 1159 |
|
| 1160 |
|
| 1161 |
def test_RaisesExc_check() -> None: |
| 1162 |
def check_oserror_and_errno_is_5(e: BaseException) -> bool: |
| 1163 |
return isinstance(e, OSError) and e.errno == 5 |
| 1164 |
|
| 1165 |
with RaisesGroup(RaisesExc(check=check_oserror_and_errno_is_5)): |
| 1166 |
raise ExceptionGroup("", (OSError(5, ""),)) |
| 1167 |
|
| 1168 |
# specifying exception_type narrows the parameter type to the callable |
| 1169 |
def check_errno_is_5(e: OSError) -> bool: |
| 1170 |
return e.errno == 5 |
| 1171 |
|
| 1172 |
with RaisesGroup(RaisesExc(OSError, check=check_errno_is_5)): |
| 1173 |
raise ExceptionGroup("", (OSError(5, ""),)) |
| 1174 |
|
| 1175 |
# avoid printing overly verbose repr multiple times |
| 1176 |
with ( |
| 1177 |
fails_raises_group( |
| 1178 |
f"RaisesExc(OSError, check={check_errno_is_5!r}): check did not return True" |
| 1179 |
), |
| 1180 |
RaisesGroup(RaisesExc(OSError, check=check_errno_is_5)), |
| 1181 |
): |
| 1182 |
raise ExceptionGroup("", (OSError(6, ""),)) |
| 1183 |
|
| 1184 |
# in nested cases you still get it multiple times though |
| 1185 |
# to address this you'd need logic in RaisesExc.__repr__ and RaisesGroup.__repr__ |
| 1186 |
with ( |
| 1187 |
fails_raises_group( |
| 1188 |
f"RaisesGroup(RaisesExc(OSError, check={check_errno_is_5!r})): RaisesExc(OSError, check={check_errno_is_5!r}): check did not return True" |
| 1189 |
), |
| 1190 |
RaisesGroup(RaisesGroup(RaisesExc(OSError, check=check_errno_is_5))), |
| 1191 |
): |
| 1192 |
raise ExceptionGroup("", [ExceptionGroup("", [OSError(6, "")])]) |
| 1193 |
|
| 1194 |
|
| 1195 |
def test_raisesexc_tostring() -> None: |
| 1196 |
assert str(RaisesExc(ValueError)) == "RaisesExc(ValueError)" |
| 1197 |
assert str(RaisesExc(match="[a-z]")) == "RaisesExc(match='[a-z]')" |
| 1198 |
pattern_no_flags = re.compile(r"noflag", 0) |
| 1199 |
assert str(RaisesExc(match=pattern_no_flags)) == "RaisesExc(match='noflag')" |
| 1200 |
pattern_flags = re.compile(r"noflag", re.IGNORECASE) |
| 1201 |
assert str(RaisesExc(match=pattern_flags)) == f"RaisesExc(match={pattern_flags!r})" |
| 1202 |
assert ( |
| 1203 |
str(RaisesExc(ValueError, match="re", check=bool)) |
| 1204 |
== f"RaisesExc(ValueError, match='re', check={bool!r})" |
| 1205 |
) |
| 1206 |
|
| 1207 |
|
| 1208 |
def test_raisesgroup_tostring() -> None: |
| 1209 |
def check_str_and_repr(s: str) -> None: |
| 1210 |
evaled = eval(s) |
| 1211 |
assert s == str(evaled) == repr(evaled) |
| 1212 |
|
| 1213 |
check_str_and_repr("RaisesGroup(ValueError)") |
| 1214 |
check_str_and_repr("RaisesGroup(RaisesGroup(ValueError))") |
| 1215 |
check_str_and_repr("RaisesGroup(RaisesExc(ValueError))") |
| 1216 |
check_str_and_repr("RaisesGroup(ValueError, allow_unwrapped=True)") |
| 1217 |
check_str_and_repr("RaisesGroup(ValueError, match='aoeu')") |
| 1218 |
|
| 1219 |
assert ( |
| 1220 |
str(RaisesGroup(ValueError, match="[a-z]", check=bool)) |
| 1221 |
== f"RaisesGroup(ValueError, match='[a-z]', check={bool!r})" |
| 1222 |
) |
| 1223 |
|
| 1224 |
|
| 1225 |
def test_assert_matches() -> None: |
| 1226 |
e = ValueError() |
| 1227 |
|
| 1228 |
# it's easy to do this |
| 1229 |
assert RaisesExc(ValueError).matches(e) |
| 1230 |
|
| 1231 |
# but you don't get a helpful error |
| 1232 |
with pytest.raises(AssertionError, match=r"assert False\n \+ where False = .*"): |
| 1233 |
assert RaisesExc(TypeError).matches(e) |
| 1234 |
|
| 1235 |
with pytest.raises( |
| 1236 |
AssertionError, |
| 1237 |
match=wrap_escape( |
| 1238 |
"`ValueError()` is not an instance of `TypeError`\n" |
| 1239 |
"assert False\n" |
| 1240 |
" + where False = matches(ValueError())\n" |
| 1241 |
" + where matches = RaisesExc(TypeError).matches" |
| 1242 |
), |
| 1243 |
): |
| 1244 |
# you'd need to do this arcane incantation |
| 1245 |
assert (m := RaisesExc(TypeError)).matches(e), m.fail_reason |
| 1246 |
|
| 1247 |
# but even if we add assert_matches, will people remember to use it? |
| 1248 |
# other than writing a linter rule, I don't think we can catch `assert RaisesExc(...).matches` |
| 1249 |
# ... no wait pytest catches other asserts ... so we probably can?? |
| 1250 |
|
| 1251 |
|
| 1252 |
# https://github.com/pytest-dev/pytest/issues/12504 |
| 1253 |
def test_xfail_raisesgroup(pytester: Pytester) -> None: |
| 1254 |
pytester.makepyfile( |
| 1255 |
""" |
| 1256 |
import sys |
| 1257 |
import pytest |
| 1258 |
if sys.version_info < (3, 11): |
| 1259 |
from exceptiongroup import ExceptionGroup |
| 1260 |
@pytest.mark.xfail(raises=pytest.RaisesGroup(ValueError)) |
| 1261 |
def test_foo() -> None: |
| 1262 |
raise ExceptionGroup("foo", [ValueError()]) |
| 1263 |
""" |
| 1264 |
) |
| 1265 |
result = pytester.runpytest() |
| 1266 |
result.assert_outcomes(xfailed=1) |
| 1267 |
|
| 1268 |
|
| 1269 |
def test_xfail_RaisesExc(pytester: Pytester) -> None: |
| 1270 |
pytester.makepyfile( |
| 1271 |
""" |
| 1272 |
import pytest |
| 1273 |
@pytest.mark.xfail(raises=pytest.RaisesExc(ValueError)) |
| 1274 |
def test_foo() -> None: |
| 1275 |
raise ValueError |
| 1276 |
""" |
| 1277 |
) |
| 1278 |
result = pytester.runpytest() |
| 1279 |
result.assert_outcomes(xfailed=1) |
| 1280 |
|
| 1281 |
|
| 1282 |
@pytest.mark.parametrize( |
| 1283 |
"wrap_in_group,handler", |
| 1284 |
[ |
| 1285 |
(False, pytest.raises(ValueError)), |
| 1286 |
(True, RaisesGroup(ValueError)), |
| 1287 |
], |
| 1288 |
) |
| 1289 |
def test_parametrizing_conditional_raisesgroup( |
| 1290 |
wrap_in_group: bool, handler: AbstractContextManager[ExceptionInfo[BaseException]] |
| 1291 |
) -> None: |
| 1292 |
with handler: |
| 1293 |
if wrap_in_group: |
| 1294 |
raise ExceptionGroup("", [ValueError()]) |
| 1295 |
raise ValueError() |
| 1296 |
|
| 1297 |
|
| 1298 |
def test_annotated_group() -> None: |
| 1299 |
# repr depends on if exceptiongroup backport is being used or not |
| 1300 |
t = repr(ExceptionGroup[ValueError]) |
| 1301 |
msg = "Only `ExceptionGroup[Exception]` or `BaseExceptionGroup[BaseException]` are accepted as generic types but got `{}`. As `raises` will catch all instances of the specified group regardless of the generic argument specific nested exceptions has to be checked with `RaisesGroup`." |
| 1302 |
|
| 1303 |
fail_msg = wrap_escape(msg.format(t)) |
| 1304 |
with pytest.raises(ValueError, match=fail_msg): |
| 1305 |
RaisesGroup(ExceptionGroup[ValueError]) |
| 1306 |
with pytest.raises(ValueError, match=fail_msg): |
| 1307 |
RaisesExc(ExceptionGroup[ValueError]) |
| 1308 |
with pytest.raises( |
| 1309 |
ValueError, |
| 1310 |
match=wrap_escape(msg.format(repr(BaseExceptionGroup[KeyboardInterrupt]))), |
| 1311 |
): |
| 1312 |
with RaisesExc(BaseExceptionGroup[KeyboardInterrupt]): |
| 1313 |
raise BaseExceptionGroup("", [KeyboardInterrupt()]) |
| 1314 |
|
| 1315 |
with RaisesGroup(ExceptionGroup[Exception]): |
| 1316 |
raise ExceptionGroup( |
| 1317 |
"", [ExceptionGroup("", [ValueError(), ValueError(), ValueError()])] |
| 1318 |
) |
| 1319 |
with RaisesExc(BaseExceptionGroup[BaseException]): |
| 1320 |
raise BaseExceptionGroup("", [KeyboardInterrupt()]) |
| 1321 |
|
| 1322 |
# assure AbstractRaises.is_baseexception is set properly |
| 1323 |
assert ( |
| 1324 |
RaisesGroup(ExceptionGroup[Exception]).expected_type() |
| 1325 |
== "ExceptionGroup(ExceptionGroup)" |
| 1326 |
) |
| 1327 |
assert ( |
| 1328 |
RaisesGroup(BaseExceptionGroup[BaseException]).expected_type() |
| 1329 |
== "BaseExceptionGroup(BaseExceptionGroup)" |
| 1330 |
) |
| 1331 |
|
| 1332 |
|
| 1333 |
def test_tuples() -> None: |
| 1334 |
# raises has historically supported one of several exceptions being raised |
| 1335 |
with pytest.raises((ValueError, IndexError)): |
| 1336 |
raise ValueError |
| 1337 |
# so now RaisesExc also does |
| 1338 |
with RaisesExc((ValueError, IndexError)): |
| 1339 |
raise IndexError |
| 1340 |
# but RaisesGroup currently doesn't. There's an argument it shouldn't because |
| 1341 |
# it can be confusing - RaisesGroup((ValueError, TypeError)) looks a lot like |
| 1342 |
# RaisesGroup(ValueError, TypeError), and the former might be interpreted as the latter. |
| 1343 |
with pytest.raises( |
| 1344 |
TypeError, |
| 1345 |
match=wrap_escape( |
| 1346 |
"Expected a BaseException type, RaisesExc, or RaisesGroup, but got 'tuple'.\n" |
| 1347 |
"RaisesGroup does not support tuples of exception types when expecting one of " |
| 1348 |
"several possible exception types like RaisesExc.\n" |
| 1349 |
"If you meant to expect a group with multiple exceptions, list them as separate arguments." |
| 1350 |
), |
| 1351 |
): |
| 1352 |
RaisesGroup((ValueError, IndexError)) # type: ignore[call-overload] |
| 1353 |
|
| 1354 |
|
| 1355 |
def test_expected_matching_only_on_matching() -> None: |
| 1356 |
"""Regression test for #14220, logic error which caused the "which was |
| 1357 |
paired with" message to appear for wrong pairs.""" |
| 1358 |
with ( |
| 1359 |
fails_raises_group( |
| 1360 |
"\n" |
| 1361 |
"1 matched exception. \n" |
| 1362 |
"Too few exceptions raised!\n" |
| 1363 |
"The following expected exceptions did not find a match:\n" |
| 1364 |
" ValueError\n" |
| 1365 |
" TypeError\n" |
| 1366 |
" It matches `TypeError()` which was paired with `TypeError`", |
| 1367 |
), |
| 1368 |
RaisesGroup(TypeError, ValueError, TypeError), |
| 1369 |
): |
| 1370 |
raise ExceptionGroup("", [TypeError()]) |