beniget
Extract semantic information about static Python code
What it is and what it does
Beniget is a static analysis library that extracts semantic information from Python abstract syntax trees. It provides three main analyses: Ancestors (maps each node to its enclosing nodes), DefUseChains (maps definitions to their uses), and UseDefChains (maps uses back to their definitions). It works across Python 3.6+ by relying on gast for cross-version AST abstraction, and as of version 0.5.0 also supports the standard library ast module.
The library is designed as a building block for writing static analyzers, compilers, and code transformation tools. Common use cases include detecting unused imports, finding decorated functions, gathering class attributes, computing variable capture in closures, and tracing instruction dependencies. It handles the semantic complexity of Python's scoping rules and name binding, though it cannot track dynamic lookups through eval() or globals().
Use it for:
- Detect unused imports and dead code by analyzing definition-use chains at module scope.
- Find all functions decorated with a specific decorator by traversing decorator uses and their parent nodes.
- Gather all attributes assigned to self in a class by walking method parameters and attribute accesses.
- Compute identifiers captured by inner functions and lambdas from outer scopes.
- Trace the set of statements required to compute a function by combining use-def chains with ancestor analysis.
Worth the install?
AI-flagged interpretation of the facts on this page — verify before relying
Beniget performs compile-time analysis on Python abstract syntax trees, extracting semantic information like definition-use chains, use-definition chains, and ancestor relationships to support static analysis and code transformation tools.
Yes. Beniget is a solid, actively maintained library for static Python code analysis with low install friction and no known vulnerabilities. It fills a specific niche—semantic AST analysis—and is well-suited for anyone building linters, compilers, or code transformation tools. The permissive BSD license poses no barrier. Install it if you need to reason about Python code structure and data flow.
Install
beniget on PyPI
pip
pip install benigetuv
uv add benigetpoetry
poetry add benigetInstalling beniget
Before you install
Low friction: pure Python wheel with a single runtime dependency (gast). Active maintenance with a recent release (258 days ago) and steady repository activity.
License in practice
BSD 3-Clause is permissive; you can use, modify, and distribute beniget with minimal restrictions, provided you include the license notice.
Quickstart
import beniget
import gast as ast
code = "from math import cos, sin; print(cos(3))"
module = ast.parse(code)
duc = beniget.DefUseChains()
duc.visit(module)
for name in module.body[0].names:
if not duc.chains[name].users():
print(f"Unused: {name.name}")
Requires Python 3.6 or later; gast is a runtime dependency.
Verify before relying
- Whether the standard library ast support (added in 0.5.0) is production-ready or still experimental.
- Performance characteristics on large codebases or deeply nested AST structures.
Package facts
| License | BSD 3-Clause (permissive) |
| Python support | supports the current Python release (>=3.6) |
| Install friction | low — pure-Python wheel |
| Runtime dependencies | 1 — gast |
| Maintenance | actively maintained — 258 days since the last release |
| Last repo commit | |
| First released | |
| Downloads | 802,315/month — #5,020 on PyPI (30-day window, as of 2026-08-14) |
| Known vulnerabilities | none known (OSV.dev, checked 2026-08-14) |
Evidence: beniget-0.5.0-py3-none-any.whl
Tags
More Software Development packages
Provides backported and experimental type hints…
permissive · top 100 on PyPI
numpyNumPy provides an N-dimensional array object…
permissive · top 100 on PyPI
fastapiFastAPI is a Python web framework for building…
permissive · top 100 on PyPI
annotated-docProvides a way to document function parameters,…
permissive · top 100 on PyPI
typerTyper builds command-line applications from…
permissive · top 1,000 on PyPI
distlibDistlib provides low-level packaging utilities…
permissive · top 1,000 on PyPI
gastProvides a unified Abstract Syntax Tree…
permissive · top 5,000 on PyPI
ast-decompilerConverts Python abstract syntax trees (ASTs)…
permissive · top 15,000 on PyPI
mr-propermr. Proper is a static code analyzer that…
permissive · top 15,000 on PyPI
trailmarkParses source code into a queryable graph of…
permissive · top 15,000 on PyPI
ast-commentsExtends Python's built-in `ast` module to…
permissive · top 15,000 on PyPI
astunparseConverts Python abstract syntax trees (ASTs)…
permissive · top 5,000 on PyPI
luaparserParses Lua source code into an abstract syntax…
permissive · top 15,000 on PyPI
asttokensAnnotates Python abstract syntax trees with the…
permissive · top 1,000 on PyPI
astprettyPretty-print Python abstract syntax trees (AST)…
permissive · top 15,000 on PyPI
pyan3Pyan3 performs static analysis on Python source…
copyleft · top 15,000 on PyPI