{"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/98b357f69e380da908740a212288d73b2ee06687/src/_pytest/raises.py#L998-L1034","documentation":"flatten_subgroups=True flattens any nested subgroups in the raised group before matching, so specifying a nested RaisesGroup structure in the expectation would be contradictory — the flattened reality could never match a nested expectation.","triggerScenarios":"Calling RaisesGroup(RaisesGroup(ValueError), flatten_subgroups=True). Hits raises.py:1016 in _parse_excgroup.","commonSituations":"Developer enables flatten_subgroups to tolerate arbitrary nesting but still encodes a nested expectation; misunderstanding the flattening semantics.","solutions":["Remove flatten_subgroups if you genuinely want to assert the nested structure.","Remove the inner RaisesGroup and list the leaf exceptions flatly: RaisesGroup(ValueError, flatten_subgroups=True).","Pick one strategy: either specify full nesting (no flatten) or ignore nesting (flatten + flat expectations)."],"exampleFix":"// before\nRaisesGroup(RaisesGroup(ValueError), flatten_subgroups=True)\n// after\nRaisesGroup(ValueError, flatten_subgroups=True)","handlingStrategy":"validation","validationCode":"has_nested = any(isinstance(e, RaisesGroup) for e in expected)\nif flatten_subgroups and has_nested:\n    raise ValueError('flatten_subgroups conflicts with nested RaisesGroup expectations')\nRaisesGroup(*expected, flatten_subgroups=flatten_subgroups)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Choose one: assert full nesting (no flatten) or ignore nesting (flatten + flat expectations).","flatten_subgroups is for when you don't care about subgroup boundaries — don't encode them.","Audit RaisesGroup constructions whenever you toggle flatten_subgroups."],"tags":["pytest","raises-group","validation"],"analyzedSha":"98b357f69e380da908740a212288d73b2ee06687","analyzedAt":"2026-08-04T20:26:34.442Z","schemaVersion":2}