()
| 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 | ): |
nothing calls this directly
no test coverage detected