Skip to main content

A attrs-style wrapper for python ast

Project description

asttrs

A attrs-style wrapper for python ast

Features

1. Developer-friendly version of ast, 1) easier to access docstring and 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.
#  :


# Then we can easily 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'
#

2. 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=src --cov-report=term-missing 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.1.0.tar.gz (42.6 kB view details)

Uploaded Source

Built Distribution

asttrs-1.1.0-py3-none-any.whl (46.2 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for asttrs-1.1.0.tar.gz
Algorithm Hash digest
SHA256 5499b7166020fccf606b4a7ab46c3b2298aa1d3e07b046e2c3b0bf94c51e16ed
MD5 59c911275346881e58b7f9a7419c8304
BLAKE2b-256 93930bc5e4a3d6477f1eeede50b35635361e96c46c454fc31ebd376a65c3a444

See more details on using hashes here.

File details

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

File metadata

  • Download URL: asttrs-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 46.2 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.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 fca2bd8f81da1f8a2969f83a4d376acb81cc3691631194784ab4712cb81d1d2a
MD5 cb953f4ddd85007252500ef32dae89b5
BLAKE2b-256 e8e42736097df077afdcd32759220117ac28f2b89daa46a850a0a72244b0ca17

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