
asttrs
A attrs-style wrapper for python ast
Features
Developer-friendly version of ast
- easier to access docstring.
- easier to do codegen.
from ast import FunctionDef
help(FunctionDef)
vs
from asttrs import FunctionDef
help(FunctionDef)
from asttrs import arguments, Return, Constant
func = FunctionDef(name="foo", args=arguments(), body=[Return(value=Constant(value="Hello World"))])
print(func.to_source())
from asttrs import Comment
comment = Comment(body="This is a comment,\nsecond line,\nthird line.")
print(comment.to_souce())
Development
# switch python version and environment
$ (cd cpython; git checkout v3.11.7)
$ ln -vsfT .venv311 .venv
# run codegen based on Python.asdl
$ pdm run inv build | pdm run black - >! src/asttrs/_py3_11.py
# run testing
$ pdm run pytest --doctest-modules --cov=asttrs._py3_11 --cov-report=term-missing src/asttrs/_py3_11.py tests