{"id":5130,"library":"beniget","title":"Beniget: Static Python Code Analysis","description":"Beniget is a static analyzer for Python code, providing compile-time analyses on Python Abstract Syntax Tree (AST). It offers over-approximation of global and local definitions and can compute def-use chains. It serves as a foundational building block for writing static analyzers or compilers for Python, relying on `gast` for cross-version AST abstraction and also supporting the standard library `ast` since version 0.5.0. It is actively maintained with periodic updates.","status":"active","version":"0.5.0","language":"en","source_language":"en","source_url":"https://github.com/serge-sans-paille/beniget/","tags":["static analysis","AST","code analysis","python","developer tool"],"install":[{"cmd":"pip install beniget","lang":"bash","label":"Install latest version"}],"dependencies":[{"reason":"Provides a consistent Python AST abstraction across various Python 3 versions, although beniget v0.5.0 now also supports the standard `ast` module. It is a direct dependency for the package.","package":"gast"}],"imports":[{"note":"One of the core analysis classes for building the ancestor tree.","symbol":"Ancestors","correct":"from beniget import Ancestors"},{"note":"One of the core analysis classes for computing definition-use chains.","symbol":"DefUseChains","correct":"from beniget import DefUseChains"},{"note":"One of the core analysis classes for computing use-definition chains.","symbol":"UseDefChains","correct":"from beniget import UseDefChains"},{"note":"While beniget 0.5.0 supports the standard `ast` module, `gast` (aliased as `ast`) is recommended for broader cross-version AST compatibility, especially with older Python versions, as `gast` is a core dependency.","wrong":"import beniget, ast","symbol":"ast","correct":"import beniget, gast as ast"}],"quickstart":{"code":"import beniget\nimport gast as ast # Use 'import ast' for Python >= 3.6 and beniget >= 0.5.0\n\n# Example: Detect unused imports\ncode = \"\"\"from math import cos, sin\nprint(cos(3))\"\"\"\nmodule = ast.parse(code)\n\nduc = beniget.DefUseChains()\nduc.visit(module)\n\n# Assuming the first body element is an ImportFrom statement\nimported_names = module.body[0].names\n\nfor name in imported_names:\n    # Find the corresponding definition-use chain for the imported name\n    # Note: `name.name` gives the imported symbol, e.g., 'cos' or 'sin'\n    ud_chain = duc.chains.get(name)\n    if ud_chain and not ud_chain.users():\n        print(f\"Unused import: {ud_chain.name()}\")\n","lang":"python","description":"This quickstart demonstrates how to use `beniget.DefUseChains` to identify unused imports in a Python code snippet. It parses the code into an AST (using `gast` for cross-version compatibility), computes def-use chains, and then checks if any imported names have no users."},"warnings":[{"fix":"Be aware of the limitations of static analysis when interpreting results from code that heavily relies on dynamic features.","message":"Beniget performs static analysis and cannot fully account for dynamic Python constructs like `eval()`, `exec()`, or direct manipulation of `globals()` or `locals()` dictionaries. Such code patterns may lead to incomplete or misleading analysis results.","severity":"gotcha","affected_versions":"All versions"},{"fix":"For `beniget >= 0.5.0`, you can use either `gast` or the standard `ast` module. Ensure your AST parsing logic aligns with the version of `beniget` and `ast` library you are using. If targeting older Python versions or cross-version compatibility, `gast` is still recommended.","message":"Prior to version 0.5.0, `beniget` exclusively relied on the `gast` library for Abstract Syntax Tree (AST) representation, which provided a unified interface across different Python 3 versions. As of `beniget` 0.5.0, it gained the ability to directly utilize Python's standard `ast` module. Code written for older `beniget` versions might implicitly expect `gast`'s AST structure, requiring adjustment for direct `ast` module usage post-0.5.0.","severity":"breaking","affected_versions":"< 0.5.0"},{"fix":"Always include `gast` in your project's dependencies and ensure it is installed alongside `beniget` (`pip install beniget gast`).","message":"The `gast` library is a mandatory runtime dependency for `beniget`, even though `beniget` v0.5.0 now supports the standard `ast` module. `gast` ensures consistent AST behavior across a wide range of Python 3 versions (>=3.6). Failure to install `gast` will lead to import errors if `beniget` (or your code) tries to use it.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-13T00:00:00.000Z","next_check":"2026-07-12T00:00:00.000Z"}