# Pydantic AST
Pydantic models covering Python AST types.
[![PyPI](https://img.shields.io/pypi/v/pydantic-ast?logo=python&logoColor=%23cccccc)](https://pypi.org/project/pydantic-ast)
[![pdm-managed](https://img.shields.io/badge/pdm-managed-blueviolet)](https://pdm.fming.dev)
[![pre-commit.ci status](https://results.pre-commit.ci/badge/github/lmmx/pydantic_ast/master.svg)](https://results.pre-commit.ci/latest/github/lmmx/pydantic_ast/master)
[![Supported Python versions](https://img.shields.io/pypi/pyversions/pydantic-ast.svg)](https://pypi.org/project/pydantic_ast)
<!-- [![build status](https://github.com/lmmx/pydantic_ast/actions/workflows/master.yml/badge.svg)](https://github.com/lmmx/pydantic_ast/actions/workflows/master.yml) -->
## Installation
```py
pip install pydantic-ast
```
## Usage
Use it as a drop-in replacement to `ast.parse` with a more readable representation.
```py
>>> 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
```sh
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.
```py
>>> 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.
```py
>>> 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](https://www.conventionalcommits.org/en/v1.0.0/).
This runs `black`, `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 use `virtualenv` environment instead of conda, skip that. Run `pdm 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.
Raw data
{
"_id": null,
"home_page": "",
"name": "pydantic-ast",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.10",
"maintainer_email": "",
"keywords": "pydantic serialization deserialization parsing",
"author": "",
"author_email": "Louis Maddox <louismmx@gmail.com>",
"download_url": "https://files.pythonhosted.org/packages/d0/86/51134dd2b12e98ffe3cf889fcf6634835878fdbe4a427d73634e77fb89b7/pydantic_ast-0.2.0.tar.gz",
"platform": null,
"description": "# Pydantic AST\n\nPydantic models covering Python AST types.\n\n[![PyPI](https://img.shields.io/pypi/v/pydantic-ast?logo=python&logoColor=%23cccccc)](https://pypi.org/project/pydantic-ast)\n[![pdm-managed](https://img.shields.io/badge/pdm-managed-blueviolet)](https://pdm.fming.dev)\n[![pre-commit.ci status](https://results.pre-commit.ci/badge/github/lmmx/pydantic_ast/master.svg)](https://results.pre-commit.ci/latest/github/lmmx/pydantic_ast/master)\n[![Supported Python versions](https://img.shields.io/pypi/pyversions/pydantic-ast.svg)](https://pypi.org/project/pydantic_ast)\n\n<!-- [![build status](https://github.com/lmmx/pydantic_ast/actions/workflows/master.yml/badge.svg)](https://github.com/lmmx/pydantic_ast/actions/workflows/master.yml) -->\n\n## Installation\n\n```py\npip install pydantic-ast\n```\n\n## Usage\n\nUse it as a drop-in replacement to `ast.parse` with a more readable representation.\n\n```py\n>>> import ast\n>>> import pydantic_ast\n>>> source = \"x = 1\"\n>>> ast.parse(source)\n<ast.Module object at 0x7fef82567610>\n>>> pydantic_ast.parse(source)\npydantic-ast: body=[Assign(targets=[Name(id='x', ctx=Store())], value=Constant(value=1), type_comment=None)] type_ignores=[]\n```\n\nUse it on the command line to quickly get an AST of a Python program or a section of one\n\n```sh\necho '\"Hello world\"' | pydantic-ast \n```\n\u21e3\n```\nbody=[Expr(value=Constant(value='Hello world'))] type_ignores=[]\n```\n\nUse it on ASTs you got from elsewhere to make them readable, or to inspect parts of them more easily.\nThe `AST_to_pydantic` class is a `ast.NodeTransformer` that converts nodes in the AST to\n`pydantic_ast` model types when the tree nodes are visited.\n\n```py\n>>> from pydantic_ast import AST_to_pydantic\n>>> source = \"123 + 345 == expected\"\n>>> my_mystery_ast = ast.parse(source)\n>>> ast_model = AST_to_pydantic().visit(my_mystery_ast)\n>>> ast_model\nbody=[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=[]\n>>> ast_model.body\n[Expr(value=Compare(left=BinOp(left=Constant(value=123), op=Add(), right=Constant(value=345)), ops=[Eq()], comparators=[Name(id='expected', ctx=Load())]))]\n```\n\nIt's simply much easier to drill down into a tree when you can see the fields in a repr.\n\n```py\n>>> ast_model.body[0].value\nCompare(left=BinOp(left=Constant(value=123), op=Add(), right=Constant(value=345)), ops=[Eq()],\ncomparators=[Name(id='expected', ctx=Load())])\n>>> ast_model.body[0].value.left\nBinOp(left=Constant(value=123), op=Add(), right=Constant(value=345))\n>>> ast_model.body[0].value.left.left\nConstant(value=123)\n>>> ast_model.body[0].value.left.left.value\n123\n```\n\n## Development\n\n- To set up pre-commit hooks (to keep the CI bot happy) run `pre-commit install-hooks` so all git\n commits trigger the pre-commit checks. I use [Conventional Commits](https://www.conventionalcommits.org/en/v1.0.0/).\n This runs `black`, `flake8`, `autopep8`, `pyupgrade`, etc.\n\n- To set up a dev env, I first create a new conda environment and use it in PDM with `which python > .pdm-python`.\n To use `virtualenv` environment instead of conda, skip that. Run `pdm install` and a `.venv` will be created if no\n Python binary path is found in `.pdm-python`.\n\n- To run tests, run `pdm run python -m pytest` and the PDM environment will be used to run the test suite.\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Pydantic models covering Python AST types",
"version": "0.2.0",
"project_urls": null,
"split_keywords": [
"pydantic",
"serialization",
"deserialization",
"parsing"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "f29b437d204376122fcc560264f976e17a788209be0d35bb715abae7ad2e882e",
"md5": "2adaf1497f753faa98e9813302a80539",
"sha256": "e035987c0c564d80d13ed284fcf7a84f2468fa59b20bec68ff90caa9e6cc77e8"
},
"downloads": -1,
"filename": "pydantic_ast-0.2.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "2adaf1497f753faa98e9813302a80539",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.10",
"size": 15158,
"upload_time": "2023-09-19T20:58:55",
"upload_time_iso_8601": "2023-09-19T20:58:55.793988Z",
"url": "https://files.pythonhosted.org/packages/f2/9b/437d204376122fcc560264f976e17a788209be0d35bb715abae7ad2e882e/pydantic_ast-0.2.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "d08651134dd2b12e98ffe3cf889fcf6634835878fdbe4a427d73634e77fb89b7",
"md5": "654732ec6c83ea91bc73560d31818a14",
"sha256": "5b113de7a11f81bf9bb0cdcb2f0ab230684c246d34471a10d1e8c734f2f1c5e7"
},
"downloads": -1,
"filename": "pydantic_ast-0.2.0.tar.gz",
"has_sig": false,
"md5_digest": "654732ec6c83ea91bc73560d31818a14",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.10",
"size": 10866,
"upload_time": "2023-09-19T20:58:57",
"upload_time_iso_8601": "2023-09-19T20:58:57.226422Z",
"url": "https://files.pythonhosted.org/packages/d0/86/51134dd2b12e98ffe3cf889fcf6634835878fdbe4a427d73634e77fb89b7/pydantic_ast-0.2.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-09-19 20:58:57",
"github": false,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"lcname": "pydantic-ast"
}