{"record":{"id":"24d74f250b0f0949","repo":"pytorch/pytorch","slug":"brackets-are-not-balanced","errorCode":null,"errorMessage":"Brackets are not balanced","messagePattern":"Brackets are not balanced","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"functorch/einops/_parsing.py","lineNumber":146,"sourceCode":"                    self.composition.append([axis_name])\n                else:\n                    bracket_group.append(axis_name)\n\n        current_identifier = None\n        for char in expression:\n            if char in \"() \":\n                if current_identifier is not None:\n                    add_axis_name(current_identifier)\n                current_identifier = None\n                if char == \"(\":\n                    if bracket_group is not None:\n                        raise ValueError(\n                            \"Axis composition is one-level (brackets inside brackets not allowed)\"\n                        )\n                    bracket_group = []\n                elif char == \")\":\n                    if bracket_group is None:\n                        raise ValueError(\"Brackets are not balanced\")\n                    self.composition.append(bracket_group)\n                    bracket_group = None\n            elif str.isalnum(char) or char in [\"_\", _ellipsis]:\n                if current_identifier is None:\n                    current_identifier = char\n                else:\n                    current_identifier += char\n            else:\n                raise ValueError(f\"Unknown character '{char}'\")\n\n        if bracket_group is not None:\n            raise ValueError(f\"Imbalanced parentheses in expression: '{expression}'\")\n        if current_identifier is not None:\n            add_axis_name(current_identifier)\n\n    @staticmethod\n    def check_axis_name_return_reason(\n        name: str, allow_underscore: bool = False","sourceCodeStart":128,"sourceCodeEnd":164,"githubUrl":"https://github.com/pytorch/pytorch/blob/dcd2ecae775af66439b7ede4e7a82540b058c59c/functorch/einops/_parsing.py#L128-L164","documentation":"Thrown by the character loop in `ParsedExpression.__init__` (functorch/einops `_parsing.py`). A closing `)` is encountered while no bracket group is open (`bracket_group is None`), i.e. an unmatched close parenthesis.","triggerScenarios":"Patterns like `'a) b -> a b'`, `'(h w)) -> h w'`, or a stray `)` pasted into the pattern.","commonSituations":"Typos; unbalanced parentheses when patterns are built by f-string concatenation; editing a pattern and deleting an opening `(`.","solutions":["Balance the parentheses in that side of the pattern","Lint the pattern string before use: `expr.count('(') == expr.count(')')`","Simplify the pattern into multiple chained rearrange calls if it is getting hard to read"],"exampleFix":"# before\ny = rearrange(x, 'h w) -> (h w)')\n\n# after\ny = rearrange(x, 'h w -> (h w)')","handlingStrategy":"validation","validationCode":"def balanced_parens(expr: str) -> bool:\n    depth = 0\n    for ch in expr:\n        if ch == \"(\":\n            depth += 1\n        elif ch == \")\":\n            depth -= 1\n            if depth < 0:\n                return False\n    return depth == 0","typeGuard":null,"tryCatchPattern":"try:\n    y = rearrange(x, pattern)\nexcept ValueError as e:\n    if \"not balanced\" in str(e):\n        raise ValueError(f\"unbalanced ')' in {pattern!r}\") from e\n    raise","preventionTips":["Lint every pattern with a paren-balance check before use","Build patterns from tested fragments rather than by string surgery"],"tags":["einops","parsing","pattern","unbalanced-parens"],"backgroundTag":null,"analyzedSha":"dcd2ecae775af66439b7ede4e7a82540b058c59c","analyzedAt":"2026-08-14T19:21:26.615Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}