Skip to main content

A attrs-style wrapper for python ast

Project description

asttrs

A attrs-style wrapper for python ast

Features

Developer-friendly version of ast

  1. easier to access docstring.
  2. easier to do codegen.
from ast import FunctionDef

help(FunctionDef)
# Help on class FunctionDef in module ast:

# class FunctionDef(stmt)
#  |  FunctionDef(identifier name, arguments args, stmt* body, expr* decorator_list, expr? returns, string? type_comment)
#  |  
#  |  (no docstring)
#  |  
#  |  Method resolution order:
#  |      FunctionDef
#  |      stmt
#  |      AST
#  |      builtins.object
#  |  
#  :

vs

from asttrs import FunctionDef

help(FunctionDef)
# Help on class FunctionDef in module asttrs._py3_11:

# class FunctionDef(stmt)
#  |  FunctionDef(*, name: 'identifier', args: 'arguments', body: List[ForwardRef('stmt')] = NOTHING, de
# corator_list: List[ForwardRef('expr')] = NOTHING, returns: 'expr' = None, type_comment: 'string' = Non
# e) -> None
#  |  
#  |  A function definition.
#  |  * ``name`` is a raw string of the function name.
#  |  * ``args`` is an :class:`arguments` node.
#  |  * ``body`` is the list of nodes inside the function.
#  |  * ``decorator_list`` is the list of decorators to be applied, stored outermost
#  |  first (i.e. the first in the list will be applied last).
#  |  * ``returns`` is the return annotation.
#  |  .. attribute:: type_comment
#  |  ``type_comment`` is an optional string with the type annotation as a comment.
#  :


# It's easier to know how to build a function
from asttrs import arguments, Return, Constant

func = FunctionDef(name="foo", args=arguments(), body=[Return(value=Constant(value="Hello World"))])

print(func.to_source())
# def foo():
#     return 'Hello World'
#

Provide Comment to codegen comments

from asttrs import Comment


comment = Comment(body="This is a comment,\nsecond line,\nthird line.")
print(comment.to_souce())
# This is a comment,
# second line,
# third line.

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

asttrs-1.3.0.tar.gz (42.8 kB view details)

Uploaded Source

Built Distribution

asttrs-1.3.0-py3-none-any.whl (46.3 kB view details)

Uploaded Python 3

File details

Details for the file asttrs-1.3.0.tar.gz.

File metadata

  • Download URL: asttrs-1.3.0.tar.gz
  • Upload date:
  • Size: 42.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: pdm/2.12.4 CPython/3.8.5

File hashes

Hashes for asttrs-1.3.0.tar.gz
Algorithm Hash digest
SHA256 39f3cd8bbecdeecff6c851d9cdb59e600a22611967cb2a0e1c5988d01a23233d
MD5 9f9bc4e09a5a6c2816e0bfb399692288
BLAKE2b-256 3f9b931770a722d8276bbbdbd109e5f6f9a589037cf471c8c1b43cc4e961bc4c

See more details on using hashes here.

File details

Details for the file asttrs-1.3.0-py3-none-any.whl.

File metadata

  • Download URL: asttrs-1.3.0-py3-none-any.whl
  • Upload date:
  • Size: 46.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: pdm/2.12.4 CPython/3.8.5

File hashes

Hashes for asttrs-1.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d671b985da21ac4981951a6e0474839d346d5ad58aca513c95f71e884e70b128
MD5 24e6c503676f607ea2602a44de63d5f7
BLAKE2b-256 dea5f081238c0da502699dda50d2469dffefad1f3e454a2c586912c9643614d1

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page