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)
# 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
Metadata
Release files for asttrs 1.3.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| asttrs-1.3.0.tar.gz | 42.8 kB | Without provenance | Details for asttrs-1.3.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| asttrs-1.3.0-py3-none-any.whl | Python 3 | none | any | Details for asttrs-1.3.0-py3-none-any.whl |
Total release size: 89.1 kB
Release files / asttrs-1.3.0.tar.gz
| Download URL | asttrs-1.3.0.tar.gz |
|---|---|
| Size | 42.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
39f3cd8bbecdeecff6c851d9cdb59e600a22611967cb2a0e1c5988d01a23233d
|
|
BLAKE2b-256 checksum How to use checksums |
3f9b931770a722d8276bbbdbd109e5f6f9a589037cf471c8c1b43cc4e961bc4c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
pdm/2.12.4 CPython/3.8.5
|
Release files / asttrs-1.3.0-py3-none-any.whl
| Download URL | asttrs-1.3.0-py3-none-any.whl |
|---|---|
| Size | 46.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
d671b985da21ac4981951a6e0474839d346d5ad58aca513c95f71e884e70b128
|
|
BLAKE2b-256 checksum How to use checksums |
dea5f081238c0da502699dda50d2469dffefad1f3e454a2c586912c9643614d1
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
pdm/2.12.4 CPython/3.8.5
|
Security for asttrs 1.3.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.