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)
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-hooksso 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 usevirtualenvenvironment instead of conda, skip that. Runpdm installand a.venvwill be created if no Python binary path is found in.pdm-python. -
To run tests, run
pdm run python -m pytestand 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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pydantic_ast-0.2.1.tar.gz.
File metadata
- Download URL: pydantic_ast-0.2.1.tar.gz
- Upload date:
- Size: 7.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b288e6f3c3e3b74f791fb5616cfa283a9f7b1b6842f3107d804159b9ca176f25
|
|
| MD5 |
65ad5657d0e843199ba90a1122307b28
|
|
| BLAKE2b-256 |
62d02d338d5a1adc0a5411b94d079951c363fc7c0bf671d860322313d090e1ee
|
Provenance
The following attestation bundles were made for pydantic_ast-0.2.1.tar.gz:
Publisher:
CI.yml on lmmx/pydantic-ast
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pydantic_ast-0.2.1.tar.gz -
Subject digest:
b288e6f3c3e3b74f791fb5616cfa283a9f7b1b6842f3107d804159b9ca176f25 - Sigstore transparency entry: 1317917657
- Sigstore integration time:
-
Permalink:
lmmx/pydantic-ast@86ddd4008614619df5f5903194acdfcfc76f225a -
Branch / Tag:
refs/tags/0.2.1 - Owner: https://github.com/lmmx
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
CI.yml@86ddd4008614619df5f5903194acdfcfc76f225a -
Trigger Event:
push
-
Statement type:
File details
Details for the file pydantic_ast-0.2.1-py3-none-any.whl.
File metadata
- Download URL: pydantic_ast-0.2.1-py3-none-any.whl
- Upload date:
- Size: 13.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
eb05b3edc043d9ccc41d8d31ec30501fdd07ed0df4dad0c1aa82591ed2a9cfbb
|
|
| MD5 |
f021f8bb294fc2e730df2b81905f9a0b
|
|
| BLAKE2b-256 |
088a25f2b3692e5360a12048f9ce42110deaa563cf7ab9e9d9b457bed805176a
|
Provenance
The following attestation bundles were made for pydantic_ast-0.2.1-py3-none-any.whl:
Publisher:
CI.yml on lmmx/pydantic-ast
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pydantic_ast-0.2.1-py3-none-any.whl -
Subject digest:
eb05b3edc043d9ccc41d8d31ec30501fdd07ed0df4dad0c1aa82591ed2a9cfbb - Sigstore transparency entry: 1317917770
- Sigstore integration time:
-
Permalink:
lmmx/pydantic-ast@86ddd4008614619df5f5903194acdfcfc76f225a -
Branch / Tag:
refs/tags/0.2.1 - Owner: https://github.com/lmmx
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
CI.yml@86ddd4008614619df5f5903194acdfcfc76f225a -
Trigger Event:
push
-
Statement type: