{"record":{"id":"04c59be28c01f626","repo":"python/cpython","slug":"module-ast-has-no-attribute-attr-r","errorCode":null,"errorMessage":"module 'ast' has no attribute {attr!r}","messagePattern":"module 'ast' has no attribute (.+?)","errorType":"exception","errorClass":"AttributeError","httpStatus":null,"severity":"error","filePath":"Lib/ast.py","lineNumber":733,"sourceCode":"    print(dump(tree, include_attributes=args.include_attributes,\n               color=can_colorize(file=sys.stdout),\n               indent=args.indent, show_empty=args.show_empty))\n\n_deprecated = {\n        'slice': globals().pop(\"slice\"),\n        'Index': globals().pop(\"Index\"),\n        'ExtSlice': globals().pop(\"ExtSlice\"),\n        'Suite': globals().pop(\"Suite\"),\n        'AugLoad': globals().pop(\"AugLoad\"),\n        'AugStore': globals().pop(\"AugStore\"),\n        'Param': globals().pop(\"Param\")\n}\n\ndef __getattr__(attr):\n    try:\n        val = _deprecated[attr]\n    except KeyError:\n        raise AttributeError(f\"module 'ast' has no attribute {attr!r}\") from None\n    warnings._deprecated(f\"ast.{attr}\", remove=(3, 21))\n    return val\n\nif __name__ == '__main__':\n    main()\n","sourceCodeStart":715,"sourceCodeEnd":739,"githubUrl":"https://github.com/python/cpython/blob/bc6749cc3b5ae4a5e88a6cc2d5b3bebbe354eae6/Lib/ast.py#L715-L739","documentation":"ast's module-level __getattr__ serves the deprecated node classes removed from the main namespace (Index, ExtSlice, Suite, AugLoad, AugStore, Param, and peers kept in _deprecated). A lookup that misses _deprecated entirely raises AttributeError; names present in the mapping emit a DeprecationWarning (removal targeted for 3.21) and return the shim class. This pattern replaced the old hard ImportError after these classes were made subclasses of their replacements.","triggerScenarios":"ast.Index / ast.ExtSlice / ast.Suite / ast.AugLoad / ast.AugStore / ast.Param accessed directly (deprecation path); any other misspelled or nonexistent ast name such as ast.AstParser raising the AttributeError; getattr(ast, name) probes in version-adaptive code.","commonSituations":"Old AST-rewriting tools (codemods, macros, optimizers) written before 3.9 when Index/SExtSlice were real node classes; code that constructs ast.Index(value) while targeting multiple Python versions; hasattr-based feature detection against ast.","solutions":["Replace deprecated classes: use the wrapped node directly (e.g. x instead of ast.Index(x) for subscripts; ast.Tuple for ExtSlice; plain Load/Store for AugLoad/AugStore; drop ast.Param in favor of plain arg).","Pin or upgrade the tooling — many codemod libraries (e.g. libcst, astroid) already shipped 3.9+ fixes.","For dynamic lookups use getattr(ast, name, None) and check sys.version_info before touching removed names."],"exampleFix":"# before\nslc = ast.Index(value=ast.Name(id='i'))  # deprecation + removal in 3.21\n\n# after\nslc = ast.Name(id='i', ctx=ast.Load())  # bare expression as index (3.9+)","handlingStrategy":"type-guard","validationCode":"DEPRECATED_AST = {'Index', 'ExtSlice', 'Suite', 'AugLoad', 'AugStore', 'Param'}\n\ndef ast_attr(name, default=None):\n    if name in DEPRECATED_AST:\n        return default  # do not touch the deprecation shims\n    return getattr(ast, name, default)","typeGuard":"def ast_has(name) -> bool:\n    \"\"\"True only for non-deprecated attributes.\"\"\"\n    import warnings\n    DEPRECATED = {'Index', 'ExtSlice', 'Suite', 'AugLoad', 'AugStore', 'Param'}\n    return name not in DEPRECATED and hasattr(ast, name)","tryCatchPattern":"try:\n    cls = getattr(ast, name)\nexcept AttributeError:\n    raise RuntimeError(\n        f'ast.{name} does not exist on Python {sys.version.split()[0]}; '\n        f'use 3.9+ node classes') from None","preventionTips":["Target modern node classes: bare expressions instead of Index, Tuple instead of ExtSlice, plain Load/Store contexts.","Use getattr(ast, name, None) plus sys.version_info checks in cross-version codemods.","Run tests with -W error::DeprecationWarning to catch ast deprecations before removal."],"tags":["ast","deprecation","attributeerror","compatibility"],"backgroundTag":null,"analyzedSha":"bc6749cc3b5ae4a5e88a6cc2d5b3bebbe354eae6","analyzedAt":"2026-08-14T22:01:13.976Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}