{"id":463,"library":"astroid","title":"Astroid: Python Abstract Syntax Tree with Inference","description":"Astroid is a Python library that provides an extended abstract syntax tree (AST) with inference capabilities, primarily used by Pylint for static code analysis. It reconstructs Python's built-in `_ast` module's tree, adding richer nodes with methods and attributes for static inference and local scope analysis. It can also build partial ASTs from live Python objects. As of version 4.1.2, it supports Python 3.10 and newer.","status":"active","version":"4.1.2","language":"python","source_language":"en","source_url":"https://github.com/pylint-dev/astroid","tags":["ast","abstract syntax tree","static analysis","linter","pylint"],"install":[{"cmd":"pip install astroid","lang":"bash","label":"Install latest version"}],"dependencies":[{"reason":"Required for type hint compatibility in Python versions prior to 3.11, when used with astroid's Python >=3.10.0 requirement.","package":"typing-extensions>=4","optional":true}],"imports":[{"note":"Entry point for parsing Python code into an astroid AST.","symbol":"parse","correct":"from astroid import parse"},{"note":"While 'AstroidManager' exists, 'MANAGER' is the common singleton instance for managing ASTs and inference.","wrong":"from astroid.manager import AstroidManager","symbol":"MANAGER","correct":"from astroid import MANAGER"},{"note":"Direct import of node classes from the top-level `astroid` package is deprecated since v4.1.0 and will be removed in v5. Import from `astroid.nodes` instead.","wrong":"from astroid import FunctionDef","symbol":"FunctionDef","correct":"from astroid.nodes import FunctionDef"},{"note":"Used to build ASTs, particularly when custom options or manager instances are needed.","symbol":"AstroidBuilder","correct":"from astroid.builder import AstroidBuilder"}],"quickstart":{"code":"from astroid import parse\nfrom astroid.nodes import Module, FunctionDef, Name\n\ncode = \"\"\"\ndef greet(name):\n    return f\"Hello, {name}!\"\n\ndef farewell(name):\n    print(f\"Goodbye, {name}.\")\n\"\"\"\n\n# Parse the code into an Astroid module\nmodule_node: Module = parse(code)\n\nprint(f\"Module name: {module_node.name}\")\n\n# Iterate over nodes to find functions\nfor node in module_node.body:\n    if isinstance(node, FunctionDef):\n        print(f\"  Found function: {node.name}\")\n        # Access arguments\n        for arg in node.args.args:\n            print(f\"    Argument: {arg.name}\")\n        # Example: Find 'print' calls (simple traversal)\n        for child_node in node.body:\n            if isinstance(child_node, Name) and child_node.name == 'print':\n                print(f\"      Function '{node.name}' uses 'print'.\")\n","lang":"python","description":"This quickstart demonstrates parsing a Python code string into an Astroid AST. It then iterates through the top-level nodes of the module to identify function definitions and their arguments. It also shows a basic traversal within a function to find specific name usages (e.g., 'print')."},"warnings":[{"fix":"Upgrade your Python environment to 3.8 or newer. Current Astroid versions (4.x) require Python >=3.10.0.","message":"Astroid v3.0.0 and later removed support for Python 3.7.","severity":"breaking","affected_versions":">=3.0.0"},{"fix":"Ensure you pass a manager instance, typically `astroid.MANAGER`, when initializing `AstroidBuilder`. E.g., `builder = AstroidBuilder(manager=astroid.MANAGER)`.","message":"In Astroid v4.0.0, the `manager` argument to `AstroidBuilder()` became required to alleviate circular import issues.","severity":"breaking","affected_versions":">=4.0.0"},{"fix":"Import node classes from `astroid.nodes` instead (e.g., `from astroid.nodes import FunctionDef`).","message":"Importing node classes directly from the top-level `astroid` package (e.g., `from astroid import FunctionDef`) is deprecated.","severity":"deprecated","affected_versions":">=4.1.0 (to be removed in v5)"},{"fix":"Increase Python's recursion limit if encountering such errors: `import sys; sys.setrecursionlimit(YOUR_HIGHER_LIMIT)`.","message":"Deeply nested code structures or complex transformations can lead to `RecursionError` during AST processing.","severity":"gotcha","affected_versions":"All versions, but logged as UserWarning with fix instructions since >=4.1.2"},{"fix":"Upgrade your Python environment to 3.9 or newer. Current Astroid versions (4.x) require Python >=3.10.0.","message":"Astroid v3.3.0 and later removed support for Python 3.8.","severity":"breaking","affected_versions":">=3.3.0"},{"fix":"Avoid using these internal functions directly, as they are no longer part of the public API. Adapt code to use public inference methods.","message":"Internal functions `infer_numpy_member`, `name_looks_like_numpy_member`, and `attribute_looks_like_numpy_member` were removed from `astroid.brain.brain_numpy_utils`.","severity":"breaking","affected_versions":">=4.0.0"}],"env_vars":null,"last_verified":"2026-05-12T13:58:27.245Z","next_check":"2026-06-26T00:00:00.000Z","problems":[{"fix":"Install the 'astroid' package using pip: 'pip install astroid'.","cause":"The 'astroid' package is not installed in the Python environment.","error":"ModuleNotFoundError: No module named 'astroid'"},{"fix":"Ensure 'astroid' is installed and accessible: 'pip install astroid'.","cause":"The 'astroid' package is not installed or not accessible in the Python environment.","error":"ImportError: No module named 'astroid'"},{"fix":"Update the syntax to be compatible with Python 3: 'except ValueError as ex:'.","cause":"Using Python 2 syntax in a Python 3 environment, such as 'except ValueError, ex:'.","error":"SyntaxError: invalid syntax"},{"fix":"Ensure all necessary '__init__.py' files are present and consider using absolute imports.","cause":"Pylint cannot resolve the import due to relative import issues or missing '__init__.py' files.","error":"E0401: Unable to import 'mytool' (import-error)"},{"fix":"Check the traceback for details and consider reporting the issue to the Astroid maintainers.","cause":"An unexpected error occurred while building the Astroid representation, often due to internal issues.","error":"F0002: %s: %s"}],"ecosystem":"pypi","meta_description":null,"install_score":100,"install_tag":"verified","quickstart_score":80,"quickstart_tag":"verified","pypi_latest":null,"install_checks":{"last_tested":"2026-05-12","tag":"verified","tag_description":"installs cleanly on critical runtimes, fast import, recently tested","results":[{"runtime":"python:3.10-alpine","python_version":"3.10","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.14,"mem_mb":5.6,"disk_size":"20.4M"},{"runtime":"python:3.10-slim","python_version":"3.10","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.1,"mem_mb":5.6,"disk_size":"21M"},{"runtime":"python:3.11-alpine","python_version":"3.11","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.21,"mem_mb":6.1,"disk_size":"22.3M"},{"runtime":"python:3.11-slim","python_version":"3.11","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.17,"mem_mb":6.1,"disk_size":"23M"},{"runtime":"python:3.12-alpine","python_version":"3.12","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.19,"mem_mb":6,"disk_size":"14.1M"},{"runtime":"python:3.12-slim","python_version":"3.12","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.2,"mem_mb":6,"disk_size":"15M"},{"runtime":"python:3.13-alpine","python_version":"3.13","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.18,"mem_mb":6.2,"disk_size":"13.8M"},{"runtime":"python:3.13-slim","python_version":"3.13","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.17,"mem_mb":6.2,"disk_size":"14M"},{"runtime":"python:3.9-alpine","python_version":"3.9","os_libc":"alpine (musl)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.13,"mem_mb":5.8,"disk_size":"19.8M"},{"runtime":"python:3.9-slim","python_version":"3.9","os_libc":"slim (glibc)","variant":"default","exit_code":0,"wheel_type":null,"failure_reason":null,"install_time_s":null,"import_time_s":0.11,"mem_mb":5.8,"disk_size":"20M"}]},"quickstart_checks":{"last_tested":"2026-04-23","tag":"verified","tag_description":"quickstart runs on critical runtimes, recently tested","results":[{"runtime":"python:3.10-alpine","exit_code":0},{"runtime":"python:3.10-slim","exit_code":0},{"runtime":"python:3.11-alpine","exit_code":0},{"runtime":"python:3.11-slim","exit_code":0},{"runtime":"python:3.12-alpine","exit_code":0},{"runtime":"python:3.12-slim","exit_code":0},{"runtime":"python:3.13-alpine","exit_code":0},{"runtime":"python:3.13-slim","exit_code":0},{"runtime":"python:3.9-alpine","exit_code":0},{"runtime":"python:3.9-slim","exit_code":0}]}}