{"record":{"id":"01b307097102bae2","repo":"python/cpython","slug":"invalid-format-format-r","errorCode":null,"errorMessage":"Invalid format: {format!r}","messagePattern":"Invalid format: (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"Lib/annotationlib.py","lineNumber":848,"sourceCode":"            if isinstance(result, ForwardRef):\n                return result.evaluate(format=Format.FORWARDREF)\n            else:\n                return result\n        else:\n            return {\n                key: (\n                    val.evaluate(format=Format.FORWARDREF)\n                    if isinstance(val, ForwardRef)\n                    else val\n                )\n                for key, val in result.items()\n            }\n    elif format == Format.VALUE:\n        # Should be impossible because __annotate__ functions must not raise\n        # NotImplementedError for this format.\n        raise RuntimeError(\"annotate function does not support VALUE format\")\n    else:\n        raise ValueError(f\"Invalid format: {format!r}\")\n\n\ndef _build_closure(annotate, owner, is_class, stringifier_dict, *, allow_evaluation):\n    if not annotate.__closure__:\n        return None, None\n    new_closure = []\n    cell_dict = {}\n    for name, cell in zip(annotate.__code__.co_freevars, annotate.__closure__, strict=True):\n        cell_dict[name] = cell\n        new_cell = None\n        if allow_evaluation:\n            try:\n                cell.cell_contents\n            except ValueError:\n                pass\n            else:\n                new_cell = cell\n        if new_cell is None:","sourceCodeStart":830,"sourceCodeEnd":866,"githubUrl":"https://github.com/python/cpython/blob/bc6749cc3b5ae4a5e88a6cc2d5b3bebbe354eae6/Lib/annotationlib.py#L830-L866","documentation":"The final else of the format dispatch in call_annotate_function raises ValueError('Invalid format: {format!r}') when the format argument matches none of the known Format members. This guards the public entry point against arbitrary integers or stale enum values passed as the format parameter.","triggerScenarios":"call_annotate_function(annotate, format=3) or any integer not in Format._value2member_map_; passing a string like 'value' instead of Format.VALUE; passing a Format member from a different Python version after enum values changed.","commonSituations":"Serializing/deserializing format values across processes or Python versions; typos passing format names as strings; old codebases after enum member reordering.","solutions":["Pass an actual Format enum member: Format.VALUE, Format.STRING, or Format.FORWARDREF.","Validate dynamic values with `isinstance(fmt, Format)` (or Format.try_value) before calling.","Do not hardcode integer format values; reference the enum so code survives value changes."],"exampleFix":"# before\nann = call_annotate_function(f.__annotate__, format='VALUE')  # ValueError: Invalid format: 'VALUE'\n\n# after\nfrom annotationlib import Format\nann = call_annotate_function(f.__annotate__, format=Format.VALUE)","handlingStrategy":"validation","validationCode":"from annotationlib import Format\n\ndef normalize_format(fmt):\n    return Format(fmt)  # raises ValueError for invalid values, use as pre-check","typeGuard":"from annotationlib import Format\nimport enum\n\ndef is_valid_format(fmt) -> bool:\n    return isinstance(fmt, Format) or (isinstance(fmt, int) and fmt in Format._value2member_map_)","tryCatchPattern":"try:\n    ann = call_annotate_function(annotate, format=fmt)\nexcept ValueError as e:\n    if 'Invalid format' in str(e):\n        raise ValueError(f'unsupported annotation format {fmt!r}; use Format.VALUE/STRING/FORWARDREF') from e\n    raise","preventionTips":["Type-hint format parameters as Format in your own wrappers","Never hardcode raw integers for format values","Coerce external input with Format(value) before calling"],"tags":["python","annotations","validation","format-enum"],"backgroundTag":null,"analyzedSha":"bc6749cc3b5ae4a5e88a6cc2d5b3bebbe354eae6","analyzedAt":"2026-08-14T22:01:13.976Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}