Pydantic models covering Python AST types
Project description
Pydantic AST
Pydantic models covering Python AST types.
Installation
pip install pydantic-ast
Usage
Use it as a drop-in replacement to ast.parse
with a more readable representation.
>>> import ast
>>> import pydantic_ast
>>> source = "x = 1"
>>> ast.parse(source)
<ast.Module object at 0x7fef82567610>
>>> pydantic_ast.parse(source)
pydantic-ast: body=[Assign(targets=[Name(id='x', ctx=Store())], value=Constant(value=1), type_comment=None)] type_ignores=[]
Use it on the command line to quickly get an AST of a Python program or a section of one
echo '"Hello world"' | pydantic-ast
⇣
body=[Expr(value=Constant(value='Hello world'))] type_ignores=[]
Use it on ASTs you got from elsewhere to make them readable, or to inspect parts of them more easily.
The AST_to_pydantic
class is a ast.NodeTransformer
that converts nodes in the AST to
pydantic_ast
model types when the tree nodes are visited.
>>> from pydantic_ast import AST_to_pydantic
>>> source = "123 + 345 == expected"
>>> my_mystery_ast = ast.parse(source)
>>> ast_model = AST_to_pydantic().visit(my_mystery_ast)
>>> ast_model
body=[Expr(value=Compare(left=BinOp(left=Constant(value=123), op=Add(), right=Constant(value=345)), ops=[Eq()], comparators=[Name(id='expected', ctx=Load())]))] type_ignores=[]
>>> ast_model.body
[Expr(value=Compare(left=BinOp(left=Constant(value=123), op=Add(), right=Constant(value=345)), ops=[Eq()], comparators=[Name(id='expected', ctx=Load())]))]
It's simply much easier to drill down into a tree when you can see the fields in a repr.
>>> ast_model.body[0].value
Compare(left=BinOp(left=Constant(value=123), op=Add(), right=Constant(value=345)), ops=[Eq()],
comparators=[Name(id='expected', ctx=Load())])
>>> ast_model.body[0].value.left
BinOp(left=Constant(value=123), op=Add(), right=Constant(value=345))
>>> ast_model.body[0].value.left.left
Constant(value=123)
>>> ast_model.body[0].value.left.left.value
123
Development
-
To set up pre-commit hooks (to keep the CI bot happy) run
pre-commit install-hooks
so all git commits trigger the pre-commit checks. I use Conventional Commits. This runsblack
,flake8
,autopep8
,pyupgrade
, etc. -
To set up a dev env, I first create a new conda environment and use it in PDM with
which python > .pdm-python
. To usevirtualenv
environment instead of conda, skip that. Runpdm install
and a.venv
will be created if no Python binary path is found in.pdm-python
. -
To run tests, run
pdm run python -m pytest
and the PDM environment will be used to run the test suite.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file pydantic_ast-0.2.0.tar.gz
.
File metadata
- Download URL: pydantic_ast-0.2.0.tar.gz
- Upload date:
- Size: 10.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: pdm/2.9.2 CPython/3.10.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5b113de7a11f81bf9bb0cdcb2f0ab230684c246d34471a10d1e8c734f2f1c5e7 |
|
MD5 | 654732ec6c83ea91bc73560d31818a14 |
|
BLAKE2b-256 | d08651134dd2b12e98ffe3cf889fcf6634835878fdbe4a427d73634e77fb89b7 |
File details
Details for the file pydantic_ast-0.2.0-py3-none-any.whl
.
File metadata
- Download URL: pydantic_ast-0.2.0-py3-none-any.whl
- Upload date:
- Size: 15.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: pdm/2.9.2 CPython/3.10.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e035987c0c564d80d13ed284fcf7a84f2468fa59b20bec68ff90caa9e6cc77e8 |
|
MD5 | 2adaf1497f753faa98e9813302a80539 |
|
BLAKE2b-256 | f29b437d204376122fcc560264f976e17a788209be0d35bb715abae7ad2e882e |