| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
from collections.abc import Callable |
| 4 |
import sys |
| 5 |
|
| 6 |
from typing_extensions import assert_type |
| 7 |
|
| 8 |
from _pytest.main import Failed as main_Failed |
| 9 |
from _pytest.outcomes import Failed |
| 10 |
from pytest import raises |
| 11 |
from pytest import RaisesExc |
| 12 |
from pytest import RaisesGroup |
| 13 |
|
| 14 |
|
| 15 |
# does not work |
| 16 |
assert_type(raises.Exception, Failed) # type: ignore[assert-type, attr-defined] |
| 17 |
|
| 18 |
# FIXME: these are different for some reason(?) |
| 19 |
assert Failed is not main_Failed # type: ignore[comparison-overlap] |
| 20 |
|
| 21 |
if sys.version_info < (3, 11): |
| 22 |
from exceptiongroup import BaseExceptionGroup |
| 23 |
from exceptiongroup import ExceptionGroup |
| 24 |
|
| 25 |
# split into functions to isolate the different scopes |
| 26 |
|
| 27 |
|
| 28 |
def check_raisesexc_typevar_default(e: RaisesExc) -> None: |
| 29 |
assert e.expected_exceptions is not None |
| 30 |
_exc: type[BaseException] | tuple[type[BaseException], ...] = e.expected_exceptions |
| 31 |
# this would previously pass, as the type would be `Any` |
| 32 |
e.exception_type().blah() # type: ignore |
| 33 |
|
| 34 |
|
| 35 |
def check_basic_contextmanager() -> None: |
| 36 |
with RaisesGroup(ValueError) as e: |
| 37 |
raise ExceptionGroup("foo", (ValueError(),)) |
| 38 |
assert_type(e.value, ExceptionGroup[ValueError]) |
| 39 |
|
| 40 |
|
| 41 |
def check_basic_matches() -> None: |
| 42 |
# check that matches gets rid of the naked ValueError in the union |
| 43 |
exc: ExceptionGroup[ValueError] | ValueError = ExceptionGroup("", (ValueError(),)) |
| 44 |
if RaisesGroup(ValueError).matches(exc): |
| 45 |
assert_type(exc, ExceptionGroup[ValueError]) |
| 46 |
|
| 47 |
# also check that BaseExceptionGroup shows up for BaseExceptions |
| 48 |
if RaisesGroup(KeyboardInterrupt).matches(exc): |
| 49 |
assert_type(exc, BaseExceptionGroup[KeyboardInterrupt]) |
| 50 |
|
| 51 |
|
| 52 |
def check_matches_with_different_exception_type() -> None: |
| 53 |
e: BaseExceptionGroup[KeyboardInterrupt] = BaseExceptionGroup( |
| 54 |
"", |
| 55 |
(KeyboardInterrupt(),), |
| 56 |
) |
| 57 |
|
| 58 |
# note: it might be tempting to have this warn. |
| 59 |
# however, that isn't possible with current typing |
| 60 |
if RaisesGroup(ValueError).matches(e): |
| 61 |
assert_type(e, ExceptionGroup[ValueError]) |
| 62 |
|
| 63 |
|
| 64 |
def check_raisesexc_init() -> None: |
| 65 |
def check_exc(exc: BaseException) -> bool: |
| 66 |
return isinstance(exc, ValueError) |
| 67 |
|
| 68 |
# Check various combinations of constructor signatures. |
| 69 |
# At least 1 arg must be provided. |
| 70 |
RaisesExc() # type: ignore |
| 71 |
RaisesExc(ValueError) |
| 72 |
RaisesExc(ValueError, match="regex") |
| 73 |
RaisesExc(ValueError, match="regex", check=check_exc) |
| 74 |
RaisesExc(match="regex") |
| 75 |
RaisesExc(check=check_exc) |
| 76 |
RaisesExc(ValueError, match="regex") |
| 77 |
RaisesExc(match="regex", check=check_exc) |
| 78 |
|
| 79 |
def check_filenotfound(exc: FileNotFoundError) -> bool: |
| 80 |
return not exc.filename.endswith(".tmp") |
| 81 |
|
| 82 |
# If exception_type is provided, that narrows the `check` method's argument. |
| 83 |
RaisesExc(FileNotFoundError, check=check_filenotfound) |
| 84 |
RaisesExc(ValueError, check=check_filenotfound) # type: ignore |
| 85 |
RaisesExc(check=check_filenotfound) # type: ignore |
| 86 |
RaisesExc(FileNotFoundError, match="regex", check=check_filenotfound) |
| 87 |
|
| 88 |
# exceptions are pos-only |
| 89 |
RaisesExc(expected_exception=ValueError) # type: ignore |
| 90 |
# match and check are kw-only |
| 91 |
RaisesExc(ValueError, "regex") # type: ignore |
| 92 |
|
| 93 |
|
| 94 |
def raisesgroup_check_type_narrowing() -> None: |
| 95 |
"""Check type narrowing on the `check` argument to `RaisesGroup`. |
| 96 |
All `type: ignore`s are correctly pointing out type errors. |
| 97 |
""" |
| 98 |
|
| 99 |
def handle_exc(e: BaseExceptionGroup[BaseException]) -> bool: |
| 100 |
return True |
| 101 |
|
| 102 |
def handle_kbi(e: BaseExceptionGroup[KeyboardInterrupt]) -> bool: |
| 103 |
return True |
| 104 |
|
| 105 |
def handle_value(e: BaseExceptionGroup[ValueError]) -> bool: |
| 106 |
return True |
| 107 |
|
| 108 |
RaisesGroup(BaseException, check=handle_exc) |
| 109 |
RaisesGroup(BaseException, check=handle_kbi) # type: ignore |
| 110 |
|
| 111 |
RaisesGroup(Exception, check=handle_exc) |
| 112 |
RaisesGroup(Exception, check=handle_value) # type: ignore |
| 113 |
|
| 114 |
RaisesGroup(KeyboardInterrupt, check=handle_exc) |
| 115 |
RaisesGroup(KeyboardInterrupt, check=handle_kbi) |
| 116 |
RaisesGroup(KeyboardInterrupt, check=handle_value) # type: ignore |
| 117 |
|
| 118 |
RaisesGroup(ValueError, check=handle_exc) |
| 119 |
RaisesGroup(ValueError, check=handle_kbi) # type: ignore |
| 120 |
RaisesGroup(ValueError, check=handle_value) |
| 121 |
|
| 122 |
RaisesGroup(ValueError, KeyboardInterrupt, check=handle_exc) |
| 123 |
RaisesGroup(ValueError, KeyboardInterrupt, check=handle_kbi) # type: ignore |
| 124 |
RaisesGroup(ValueError, KeyboardInterrupt, check=handle_value) # type: ignore |
| 125 |
|
| 126 |
|
| 127 |
def raisesgroup_narrow_baseexceptiongroup() -> None: |
| 128 |
"""Check type narrowing specifically for the container exceptiongroup.""" |
| 129 |
|
| 130 |
def handle_group(e: ExceptionGroup[Exception]) -> bool: |
| 131 |
return True |
| 132 |
|
| 133 |
def handle_group_value(e: ExceptionGroup[ValueError]) -> bool: |
| 134 |
return True |
| 135 |
|
| 136 |
RaisesGroup(ValueError, check=handle_group_value) |
| 137 |
|
| 138 |
RaisesGroup(Exception, check=handle_group) |
| 139 |
|
| 140 |
|
| 141 |
def check_raisesexc_transparent() -> None: |
| 142 |
with RaisesGroup(RaisesExc(ValueError)) as e: |
| 143 |
... |
| 144 |
_: BaseExceptionGroup[ValueError] = e.value |
| 145 |
assert_type(e.value, ExceptionGroup[ValueError]) |
| 146 |
|
| 147 |
|
| 148 |
def check_nested_raisesgroups_contextmanager() -> None: |
| 149 |
with RaisesGroup(RaisesGroup(ValueError)) as excinfo: |
| 150 |
raise ExceptionGroup("foo", (ValueError(),)) |
| 151 |
|
| 152 |
_: BaseExceptionGroup[BaseExceptionGroup[ValueError]] = excinfo.value |
| 153 |
|
| 154 |
assert_type( |
| 155 |
excinfo.value, |
| 156 |
ExceptionGroup[ExceptionGroup[ValueError]], |
| 157 |
) |
| 158 |
|
| 159 |
assert_type( |
| 160 |
excinfo.value.exceptions[0], |
| 161 |
# this union is because of how typeshed defines .exceptions |
| 162 |
ExceptionGroup[ValueError] | ExceptionGroup[ExceptionGroup[ValueError]], |
| 163 |
) |
| 164 |
|
| 165 |
|
| 166 |
def check_nested_raisesgroups_matches() -> None: |
| 167 |
"""Check nested RaisesGroup with .matches""" |
| 168 |
exc: ExceptionGroup[ExceptionGroup[ValueError]] = ExceptionGroup( |
| 169 |
"", |
| 170 |
(ExceptionGroup("", (ValueError(),)),), |
| 171 |
) |
| 172 |
|
| 173 |
if RaisesGroup(RaisesGroup(ValueError)).matches(exc): |
| 174 |
assert_type(exc, ExceptionGroup[ExceptionGroup[ValueError]]) |
| 175 |
|
| 176 |
|
| 177 |
def check_multiple_exceptions_1() -> None: |
| 178 |
a = RaisesGroup(ValueError, ValueError) |
| 179 |
b = RaisesGroup(RaisesExc(ValueError), RaisesExc(ValueError)) |
| 180 |
c = RaisesGroup(ValueError, RaisesExc(ValueError)) |
| 181 |
|
| 182 |
d: RaisesGroup[ValueError] |
| 183 |
d = a |
| 184 |
d = b |
| 185 |
d = c |
| 186 |
assert d |
| 187 |
|
| 188 |
|
| 189 |
def check_multiple_exceptions_2() -> None: |
| 190 |
# This previously failed due to lack of covariance in the TypeVar |
| 191 |
a = RaisesGroup(RaisesExc(ValueError), RaisesExc(TypeError)) |
| 192 |
b = RaisesGroup(RaisesExc(ValueError), TypeError) |
| 193 |
c = RaisesGroup(ValueError, TypeError) |
| 194 |
|
| 195 |
d: RaisesGroup[Exception] |
| 196 |
d = a |
| 197 |
d = b |
| 198 |
d = c |
| 199 |
assert d |
| 200 |
|
| 201 |
|
| 202 |
def check_raisesgroup_overloads() -> None: |
| 203 |
# allow_unwrapped=True does not allow: |
| 204 |
# multiple exceptions |
| 205 |
RaisesGroup(ValueError, TypeError, allow_unwrapped=True) # type: ignore |
| 206 |
# nested RaisesGroup |
| 207 |
RaisesGroup(RaisesGroup(ValueError), allow_unwrapped=True) # type: ignore |
| 208 |
# specifying match |
| 209 |
RaisesGroup(ValueError, match="foo", allow_unwrapped=True) # type: ignore |
| 210 |
# specifying check |
| 211 |
RaisesGroup(ValueError, check=bool, allow_unwrapped=True) # type: ignore |
| 212 |
# allowed variants |
| 213 |
RaisesGroup(ValueError, allow_unwrapped=True) |
| 214 |
RaisesGroup(ValueError, allow_unwrapped=True, flatten_subgroups=True) |
| 215 |
RaisesGroup(RaisesExc(ValueError), allow_unwrapped=True) |
| 216 |
|
| 217 |
# flatten_subgroups=True does not allow nested RaisesGroup |
| 218 |
RaisesGroup(RaisesGroup(ValueError), flatten_subgroups=True) # type: ignore |
| 219 |
# but rest is plenty fine |
| 220 |
RaisesGroup(ValueError, TypeError, flatten_subgroups=True) |
| 221 |
RaisesGroup(ValueError, match="foo", flatten_subgroups=True) |
| 222 |
RaisesGroup(ValueError, check=bool, flatten_subgroups=True) |
| 223 |
RaisesGroup(ValueError, flatten_subgroups=True) |
| 224 |
RaisesGroup(RaisesExc(ValueError), flatten_subgroups=True) |
| 225 |
|
| 226 |
# if they're both false we can of course specify nested raisesgroup |
| 227 |
RaisesGroup(RaisesGroup(ValueError)) |
| 228 |
|
| 229 |
|
| 230 |
def check_triple_nested_raisesgroup() -> None: |
| 231 |
with RaisesGroup(RaisesGroup(RaisesGroup(ValueError))) as e: |
| 232 |
assert_type(e.value, ExceptionGroup[ExceptionGroup[ExceptionGroup[ValueError]]]) |
| 233 |
|
| 234 |
|
| 235 |
def check_check_typing() -> None: |
| 236 |
# `BaseExceptiongroup` should perhaps be `ExceptionGroup`, but close enough |
| 237 |
assert_type( |
| 238 |
RaisesGroup(ValueError).check, |
| 239 |
Callable[[BaseExceptionGroup[ValueError]], bool] | None, |
| 240 |
) |