{"record":{"id":"75f1c80768af13d4","repo":"pytest-dev/pytest","slug":"you-cannot-specify-a-nested-structure-inside-a-rai","errorCode":null,"errorMessage":"You cannot specify a nested structure inside a RaisesGroup with `flatten_subgroups=True`. The parameter will flatten subgroups in the raised exceptiongroup before matching, which would never match a nested structure.","messagePattern":"You cannot specify a nested structure inside a RaisesGroup with `flatten_subgroups=True`\\. The parameter will flatten subgroups in the raised exceptiongroup before matching, which would never match a nested structure\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/_pytest/raises.py","lineNumber":1016,"sourceCode":"                expected_exception,\n                *other_exceptions,\n            )\n        )\n\n    def _parse_excgroup(\n        self,\n        exc: (\n            type[BaseExcT_co]\n            | types.GenericAlias\n            | RaisesExc[BaseExcT_1]\n            | RaisesGroup[BaseExcT_2]\n        ),\n        expected: str,\n    ) -> type[BaseExcT_co] | RaisesExc[BaseExcT_1] | RaisesGroup[BaseExcT_2]:\n        # verify exception type and set `self.is_baseexception`\n        match exc:\n            case RaisesGroup() if self.flatten_subgroups:\n                raise ValueError(\n                    \"You cannot specify a nested structure inside a RaisesGroup with\"\n                    \" `flatten_subgroups=True`. The parameter will flatten subgroups\"\n                    \" in the raised exceptiongroup before matching, which would never\"\n                    \" match a nested structure.\",\n                )\n            case RaisesGroup() | RaisesExc():\n                self.is_baseexception |= exc.is_baseexception\n                exc._nested = True\n                return exc\n            case tuple():\n                raise TypeError(\n                    f\"Expected {expected}, but got {type(exc).__name__!r}.\\n\"\n                    \"RaisesGroup does not support tuples of exception types when expecting one of \"\n                    \"several possible exception types like RaisesExc.\\n\"\n                    \"If you meant to expect a group with multiple exceptions, list them as separate arguments.\"\n                )\n            case _:\n                return super()._parse_exc(exc, expected)","sourceCodeStart":998,"sourceCodeEnd":1034,"githubUrl":"https://github.com/pytest-dev/pytest/blob/0d6fbdeffa57c796123f62f81f7dd370d9b7ecdc/src/_pytest/raises.py#L998-L1034","documentation":"Raised by RaisesGroup._parse_excgroup when a nested RaisesGroup appears among expected exceptions while flatten_subgroups=True is set on the outer RaisesGroup. flatten_subgroups flattens subgroups in the raised group before matching, so any nested structure in the expected matcher could never align with the flattened shape — the assertion would be unsatisfiable. pytest rejects this contradiction early.","triggerScenarios":"RaisesGroup(RaisesGroup(ValueError), flatten_subgroups=True); any nested RaisesGroup argument combined with flatten_subgroups=True on the parent. The `case RaisesGroup() if self.flatten_subgroups` arm fires.","commonSituations":"Enabling flatten_subgroups to tolerate subgroup nesting, then also specifying a nested RaisesGroup expectation; refactoring that adds flatten_subgroups without removing nested matchers.","solutions":["Remove flatten_subgroups=True so the nested RaisesGroup matcher can compare structure-for-structure.","Keep flatten_subgroups=True and flatten the expected matcher too — list leaf exceptions directly: RaisesGroup(ValueError) instead of RaisesGroup(RaisesGroup(ValueError)).","Use RaisesExc leaves only and let flatten_subgroups treat the whole group as a flat list."],"exampleFix":"// before\nwith pytest.raises(RaisesGroup(RaisesGroup(ValueError), flatten_subgroups=True)):\n    ...\n// after (flatten both sides):\nwith pytest.raises(RaisesGroup(ValueError, flatten_subgroups=True)):\n    ...\n// or keep structure on both sides:\nwith pytest.raises(RaisesGroup(RaisesGroup(ValueError))):\n    ...","handlingStrategy":"validation","validationCode":"from _pytest.raises import RaisesGroup\nif flatten_subgroups:\n    exs = expected_exception if isinstance(expected_exception, tuple) else (expected_exception,)\n    assert not any(isinstance(e, RaisesGroup) for e in exs), 'flatten_subgroups conflicts with nested RaisesGroup'","typeGuard":"def flatten_subgroups_no_nested(expected_exception, flatten_subgroups) -> bool:\n    from _pytest.raises import RaisesGroup\n    if not flatten_subgroups:\n        return True\n    exs = expected_exception if isinstance(expected_exception, tuple) else (expected_exception,)\n    return not any(isinstance(e, RaisesGroup) for e in exs)","tryCatchPattern":null,"preventionTips":["Decide upfront: either match structure (drop flatten_subgroups) or match flat (drop nested RaisesGroup).","Use RaisesExc leaves with flatten_subgroups=True for a flat-membership assertion.","Document the chosen strategy in test helpers."],"tags":["pytest","raises","raisesgroup","flatten-subgroups","nested-group","validation"],"backgroundTag":null,"analyzedSha":"0d6fbdeffa57c796123f62f81f7dd370d9b7ecdc","analyzedAt":"2026-08-11T20:52:36.969Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}