skillfed

asttokens

Annotate AST trees with source code positions

asttokens Permissive license Apache 2.0 Active 194 v3.0.2 released

Install

asttokens on PyPI

pip

pip install asttokens

uv

uv add asttokens

poetry

poetry add asttokens

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 — 32 days since the last release
Last repo commit
First released
Popularity one of the top 1,000 most-downloaded packages on PyPI (30-day window, as of 2026-08-13)
Known vulnerabilities none known (OSV.dev, checked 2026-08-13)

Evidence: asttokens-3.0.2-py3-none-any.whl

Keywords: code, ast, parse, tokenize, refactor

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

About asttokens

from the package's own PyPI description — quoted content, verbatim

ASTTokens

.. image:: https://img.shields.io/pypi/v/asttokens.svg :target: https://pypi.python.org/pypi/asttokens/ .. image:: https://img.shields.io/pypi/pyversions/asttokens.svg :target: https://pypi.python.org/pypi/asttokens/ .. image:: https://github.com/gristlabs/asttokens/actions/workflows/build-and-test.yml/badge.svg :target: https://github.com/gristlabs/asttokens/actions/workflows/build-and-test.yml .. image:: https://readthedocs.org/projects/asttokens/badge/?version=latest :target: https://asttokens.readthedocs.io/en/latest/index.html .. image:: https://coveralls.io/repos/github/gristlabs/asttokens/badge.svg :target: https://coveralls.io/github/gristlabs/asttokens

.. Start of user-guide

The asttokens module annotates Python abstract syntax trees (ASTs) with the positions of tokens and text in the source code that generated them.

It makes it possible for tools that work with logical AST nodes to find the particular text that resulted in those nodes, for example for automated refactoring or highlighting.

Installation

asttokens is available on PyPI: https://pypi.python.org/pypi/asttokens/::

pip install asttokens

The...

Read as markdown · JSON record · Source repository · Homepage · Docs

AI interpretation — verify before relying

AI-generated interpretation of the package facts above; every digit, version, license, or vulnerability id it cites is grounded in the facts already shown on this page

Annotates Python abstract syntax trees with the precise source code positions and text of tokens, enabling tools to map logical AST nodes back to their originating code for refactoring, analysis, and transformation.

Installs with no runtime dependencies and low friction; actively maintained with a recent release 32 days ago and active repository status.

Licensed under Apache 2.0 (permissive), allowing commercial and private use with minimal restrictions beyond attribution and liability disclaimers.

Usage

import asttokens, 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))

Requires Python 3.8 or later; supports CPython and PyPy.

Verdict: A stable, actively maintained library with zero known vulnerabilities, no external dependencies, and broad Python version support (3.8–3.14). Well-suited for code analysis and refactoring tools that need to bridge AST nodes and source text.

Needs verification

  • Whether the package works equally well with both ast and astroid-built trees in practice, or if one has limitations.
  • Performance characteristics when processing very large source files or deeply nested AST structures.
ast token position mappingpython ast source code annotationrefactoring ast to source textast node to code text lookuppython code tokenization and astast-based code transformationsource position tracking ast

Similar packages