asttokens
Annotate AST trees with source code positions
Decision gist · record as of 2026-08-14
Yes. asttokens is actively maintained, has zero known vulnerabilities, installs with no dependencies, and is widely used (top 1000 on PyPI). It solves a real problem for anyone building code analysis or transformation tools. The permissive Apache 2.0 license poses no restriction.AI-flagged interpretation of the facts on this page — verify before relying
Before you install
- Low friction; pure Python wheel with no runtime dependencies.
- Actively maintained with a release 33 days ago and recent commits.
- Supports Python 3.9 through 3.14 and PyPy.
License · maintenance · safety
Apache 2.0 (permissive) — Apache 2.0 permissive license allows commercial and private use with minimal restrictions, making it safe for most projects.
last release 2026-07-12 (33 days) · last repo commit 2026-07-12 · 194 stars
0 known vulnerabilities (OSV.dev, 2026-08-14) · 134,816,594 downloads/mo, #284 on PyPI
Alternatives
Verify before relying
import asttokens
import ast
source = "Robot('blue').walk(steps=10*n)"
atok = asttokens.ASTTokens(source, parse=True)
attr_node = next(n for n in ast.walk(atok.tree) if isinstance(n, ast.Attribute))
print(atok.get_text(attr_node))What it is and what it does
asttokens bridges the gap between Python's abstract syntax tree representation and the original source code. When you parse code into an AST, you get a logical structure but lose the exact positions and text that created each node. This package annotates AST nodes with token positions and text references, letting you recover the original code fragments corresponding to any node.
It works with both the standard ast module and astroid (used by linters and analysis tools). The typical workflow is to parse source code into an annotated tree, then use the package's methods to extract text for specific nodes, walk token sequences, or modify code by replacing text at known positions. This is essential for code generation tools, automated refactoring, syntax highlighting, and linters that need to report precise source locations.
Use it for
- Automated code refactoring: find an AST node and replace its source text with a transformed version.
- Linter and analysis tools: map diagnostic messages back to exact source positions and code snippets.
- Code generation and templating: extract and manipulate code fragments based on AST structure.
- Syntax highlighting and IDE features: correlate AST nodes with display positions in source.
- Documentation generation: extract and annotate code examples based on their AST structure.
Worth the install?
AI-flagged interpretation of the facts on this page. Verify before relying on it.
Yes.
asttokens is actively maintained, has zero known vulnerabilities, installs with no dependencies, and is widely used (top 1000 on PyPI). It solves a real problem for anyone building code analysis or transformation tools. The permissive Apache 2.0 license poses no restriction.
Install
asttokens on PyPI
Before you install
Low friction; pure Python wheel with no runtime dependencies. Actively maintained with a release 33 days ago and recent commits. Supports Python 3.9 through 3.14 and PyPy.
License in practice
Apache 2.0 permissive license allows commercial and private use with minimal restrictions, making it safe for most projects.
Quickstart
import asttokens
import ast
source = "Robot('blue').walk(steps=10*n)"
atok = asttokens.ASTTokens(source, parse=True)
attr_node = next(n for n in ast.walk(atok.tree) if isinstance(n, ast.Attribute))
print(atok.get_text(attr_node))
Package facts
| License | Apache 2.0 permissive |
| Python support | Supports the current Python release >=3.8 |
| Install friction | Low. Pure-Python wheel |
| Runtime dependencies | None |
| Maintenance | Actively maintained 33 days since the last release |
| Last repo commit | |
| First released | |
| Downloads | 134,816,594 / month, #284 on PyPI 30-day window, as of 2026-08-14 |
| Known vulnerabilities | None known OSV.dev, checked 2026-08-14 |
| Classifiers | Development Status :: 5 - Production/StableEnvironment :: ConsoleIntended Audience :: DevelopersOperating System :: OS IndependentProgramming Language :: Python :: 3.10Programming Language :: Python :: 3.11Programming Language :: Python :: 3.12Programming Language :: Python :: 3.13Programming Language :: Python :: 3.14Programming Language :: Python :: 3.9Programming Language :: Python :: Implementation :: CPythonProgramming Language :: Python :: Implementation :: PyPyTopic :: Software Development :: Code GeneratorsTopic :: Software Development :: CompilersTopic :: Software Development :: InterpretersTopic :: Software Development :: Libraries :: Python ModulesTopic :: Software Development :: Pre-processors |
Evidence: asttokens-3.0.2-py3-none-any.whl
Tags
Let your AI agent find packages like this
Example. Real query, live index.
You found this page by searching. An agent finds it by wishing: SkillFed indexes 14,416 PyPI packages by what they can do, searchable in plain language.
wish › “ast token position mapping”
- asttokensAnnotates Python abstract syntax trees with the positions of tokens…
- tokenize-rtWraps Python's stdlib tokenize module to enable proper roundtripping…
- json-source-mapMaps each value in a JSON document to its source location (line,…
Give your agent the search over MCP, or paste the wish link into any chat.
More Python Modules packages
Converts domain names between Unicode and ASCII-compatible encoding (Punycode) according to IDNA 2008 and Unicode Technical Standard 46, with security validation and broader script coverage than the standard library.
Install it if you work with internationalized domain names, need to validate domains, or use HTTP clients that depend on it transitively.
Setuptools is a Python build backend and package management tool that handles building, distributing, and installing Python packages, including support for C/C++ extension modules.
PyYAML parses and emits YAML 1.1 data format, enabling serialization and deserialization of configuration files and Python objects to and from human-readable YAML text.
Pydantic validates Python data structures against type hints, coercing and checking input at runtime to ensure it matches a declared schema.
Provides reusable metadata objects for use with PEP-593 `typing.Annotated` to express common constraints like bounds, collection sizes, and predicates on types.
Install it if you use or build libraries that need to express type constraints in a standardized, inspectable way—or if you want to annotate your own types with…
Provides runtime tools to inspect and introspect Python type annotations, enabling programmatic examination of type hints at execution time.
See also ast-decompiler · baron · astor · ast-comments · gast · astroid · trailmark · executing · redbaron · google-pasta