skillfed

beniget

Extract semantic information about static Python code

beniget v0.5.0 802.3K downloads/30d#5,020 on PyPI85
Permissive license BSD 3-Clause Active released

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 beniget

uv

uv add beniget

poetry

poetry add beniget

Installing 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

Development Status :: 4 - BetaEnvironment :: ConsoleIntended Audience :: DevelopersLicense :: OSI Approved :: BSD LicenseNatural Language :: EnglishProgramming Language :: Python :: 3

Tags

python ast analysisdefinition use chainsstatic code analysispython compiler buildingsemantic ast extractioncode flow analysispython ast traversal
ast-analysisstatic-analysiscompiler-tools

More Software Development packages