astlab 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- astlab-0.1.0/LICENSE +21 -0
- astlab-0.1.0/PKG-INFO +82 -0
- astlab-0.1.0/README.md +56 -0
- astlab-0.1.0/pyproject.toml +135 -0
- astlab-0.1.0/src/astlab/__init__.py +6 -0
- astlab-0.1.0/src/astlab/_typing.py +22 -0
- astlab-0.1.0/src/astlab/abc.py +34 -0
- astlab-0.1.0/src/astlab/builder.py +1287 -0
- astlab-0.1.0/src/astlab/context.py +100 -0
- astlab-0.1.0/src/astlab/info.py +105 -0
- astlab-0.1.0/src/astlab/reader.py +48 -0
- astlab-0.1.0/src/astlab/resolver.py +75 -0
- astlab-0.1.0/src/astlab/types.py +203 -0
- astlab-0.1.0/src/astlab/writer.py +28 -0
astlab-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Danil Troshnev
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
astlab-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
Metadata-Version: 2.3
|
|
2
|
+
Name: astlab
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: provides an intuitive API for building and manipulating Abstract Syntax Trees (ASTs) to generate Python code.
|
|
5
|
+
License: MIT
|
|
6
|
+
Keywords: python,codegen
|
|
7
|
+
Author: zerlok
|
|
8
|
+
Author-email: danil.troshnev@gmail.com
|
|
9
|
+
Requires-Python: >=3.9,<4.0
|
|
10
|
+
Classifier: Development Status :: 3 - Alpha
|
|
11
|
+
Classifier: Intended Audience :: Developers
|
|
12
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
13
|
+
Classifier: Operating System :: OS Independent
|
|
14
|
+
Classifier: Programming Language :: Python :: 3
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
20
|
+
Classifier: Topic :: Software Development :: Code Generators
|
|
21
|
+
Classifier: Typing :: Typed
|
|
22
|
+
Project-URL: Homepage, https://github.com/zerlok/astlab
|
|
23
|
+
Project-URL: Issues, https://github.com/zerlok/astlab/issues
|
|
24
|
+
Description-Content-Type: text/markdown
|
|
25
|
+
|
|
26
|
+
# astlab
|
|
27
|
+
|
|
28
|
+
[](https://pypi.python.org/pypi/astlab)
|
|
29
|
+
[](https://pypi.python.org/pypi/astlab)
|
|
30
|
+
[](https://mypy.readthedocs.io/en/stable/getting_started.html#strict-mode-and-configuration)
|
|
31
|
+
[](https://codecov.io/gh/zerlok/astlab)
|
|
32
|
+
[](https://pypistats.org/packages/astlab)
|
|
33
|
+
[](https://github.com/zerlok/astlab/stargazers)
|
|
34
|
+
|
|
35
|
+
**astlab** is a Python library that provides an intuitive API for building and manipulating Abstract Syntax Trees (ASTs) to generate Python code. With **astlab**, you can easily create Python modules, classes, fields, and more using a simple and readable syntax, and convert the AST back into executable Python code.
|
|
36
|
+
|
|
37
|
+
## Features
|
|
38
|
+
|
|
39
|
+
- **Easy AST construction**: Build Python code using a fluent and intuitive API.
|
|
40
|
+
- **Code generation**: Convert your AST into valid Python code, forget about jinja templates.
|
|
41
|
+
- **Supports nested scopes & auto imports**: Create nested classes, methods, and fields effortlessly. Reference types from other modules easily.
|
|
42
|
+
- **Highly customizable**: Extend and modify the API to suit your needs.
|
|
43
|
+
|
|
44
|
+
## Installation
|
|
45
|
+
|
|
46
|
+
You can install **astlab** from PyPI using `pip`:
|
|
47
|
+
|
|
48
|
+
```bash
|
|
49
|
+
pip install astlab
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
## Usage
|
|
53
|
+
|
|
54
|
+
### Simple example
|
|
55
|
+
|
|
56
|
+
Here's a basic example of how to use **astlab** to create a Python module with a dataclass.
|
|
57
|
+
|
|
58
|
+
```python
|
|
59
|
+
import astlab
|
|
60
|
+
|
|
61
|
+
# Create a new Python module
|
|
62
|
+
with astlab.module("foo") as foo:
|
|
63
|
+
# Build a "Bar" dataclass
|
|
64
|
+
with foo.class_def("Bar").dataclass() as bar:
|
|
65
|
+
# Define a field "spam" of type int
|
|
66
|
+
bar.field_def("spam", int)
|
|
67
|
+
|
|
68
|
+
# Generate and print the Python code from the AST
|
|
69
|
+
print(foo.render())
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
### Output:
|
|
73
|
+
|
|
74
|
+
```python
|
|
75
|
+
import builtins
|
|
76
|
+
import dataclasses
|
|
77
|
+
|
|
78
|
+
@dataclasses.dataclass()
|
|
79
|
+
class Bar:
|
|
80
|
+
spam: builtins.int
|
|
81
|
+
```
|
|
82
|
+
|
astlab-0.1.0/README.md
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
# astlab
|
|
2
|
+
|
|
3
|
+
[](https://pypi.python.org/pypi/astlab)
|
|
4
|
+
[](https://pypi.python.org/pypi/astlab)
|
|
5
|
+
[](https://mypy.readthedocs.io/en/stable/getting_started.html#strict-mode-and-configuration)
|
|
6
|
+
[](https://codecov.io/gh/zerlok/astlab)
|
|
7
|
+
[](https://pypistats.org/packages/astlab)
|
|
8
|
+
[](https://github.com/zerlok/astlab/stargazers)
|
|
9
|
+
|
|
10
|
+
**astlab** is a Python library that provides an intuitive API for building and manipulating Abstract Syntax Trees (ASTs) to generate Python code. With **astlab**, you can easily create Python modules, classes, fields, and more using a simple and readable syntax, and convert the AST back into executable Python code.
|
|
11
|
+
|
|
12
|
+
## Features
|
|
13
|
+
|
|
14
|
+
- **Easy AST construction**: Build Python code using a fluent and intuitive API.
|
|
15
|
+
- **Code generation**: Convert your AST into valid Python code, forget about jinja templates.
|
|
16
|
+
- **Supports nested scopes & auto imports**: Create nested classes, methods, and fields effortlessly. Reference types from other modules easily.
|
|
17
|
+
- **Highly customizable**: Extend and modify the API to suit your needs.
|
|
18
|
+
|
|
19
|
+
## Installation
|
|
20
|
+
|
|
21
|
+
You can install **astlab** from PyPI using `pip`:
|
|
22
|
+
|
|
23
|
+
```bash
|
|
24
|
+
pip install astlab
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## Usage
|
|
28
|
+
|
|
29
|
+
### Simple example
|
|
30
|
+
|
|
31
|
+
Here's a basic example of how to use **astlab** to create a Python module with a dataclass.
|
|
32
|
+
|
|
33
|
+
```python
|
|
34
|
+
import astlab
|
|
35
|
+
|
|
36
|
+
# Create a new Python module
|
|
37
|
+
with astlab.module("foo") as foo:
|
|
38
|
+
# Build a "Bar" dataclass
|
|
39
|
+
with foo.class_def("Bar").dataclass() as bar:
|
|
40
|
+
# Define a field "spam" of type int
|
|
41
|
+
bar.field_def("spam", int)
|
|
42
|
+
|
|
43
|
+
# Generate and print the Python code from the AST
|
|
44
|
+
print(foo.render())
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
### Output:
|
|
48
|
+
|
|
49
|
+
```python
|
|
50
|
+
import builtins
|
|
51
|
+
import dataclasses
|
|
52
|
+
|
|
53
|
+
@dataclasses.dataclass()
|
|
54
|
+
class Bar:
|
|
55
|
+
spam: builtins.int
|
|
56
|
+
```
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
[tool.poetry]
|
|
2
|
+
name = "astlab"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "provides an intuitive API for building and manipulating Abstract Syntax Trees (ASTs) to generate Python code."
|
|
5
|
+
authors = ["zerlok <danil.troshnev@gmail.com>"]
|
|
6
|
+
readme = "README.md"
|
|
7
|
+
license = "MIT"
|
|
8
|
+
keywords = [
|
|
9
|
+
"python",
|
|
10
|
+
"codegen",
|
|
11
|
+
]
|
|
12
|
+
classifiers = [
|
|
13
|
+
"Development Status :: 3 - Alpha",
|
|
14
|
+
"Intended Audience :: Developers",
|
|
15
|
+
"Operating System :: OS Independent",
|
|
16
|
+
"Topic :: Software Development :: Code Generators",
|
|
17
|
+
"Typing :: Typed",
|
|
18
|
+
]
|
|
19
|
+
|
|
20
|
+
[tool.poetry.urls]
|
|
21
|
+
Homepage = "https://github.com/zerlok/astlab"
|
|
22
|
+
Issues = "https://github.com/zerlok/astlab/issues"
|
|
23
|
+
|
|
24
|
+
[tool.poetry.dependencies]
|
|
25
|
+
python = "^3.9"
|
|
26
|
+
|
|
27
|
+
[tool.poetry.group.dev.dependencies]
|
|
28
|
+
mypy = "^1.13.0"
|
|
29
|
+
pytest = "^8.3.3"
|
|
30
|
+
pytest-cov = "^6.0.0"
|
|
31
|
+
pytest-mypy-plugins = "^3.1.2"
|
|
32
|
+
ruff = ">=0.7.4,<0.9.0"
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
[build-system]
|
|
36
|
+
requires = ["poetry-core"]
|
|
37
|
+
build-backend = "poetry.core.masonry.api"
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
[tool.ruff]
|
|
41
|
+
target-version = "py39"
|
|
42
|
+
include = ["src/**/*.py", "tests/**/*.py"]
|
|
43
|
+
force-exclude = true
|
|
44
|
+
line-length = 120
|
|
45
|
+
output-format = "pylint"
|
|
46
|
+
|
|
47
|
+
[tool.ruff.lint]
|
|
48
|
+
select = ["ALL"]
|
|
49
|
+
ignore = [
|
|
50
|
+
"ANN", # because we use mypy
|
|
51
|
+
"D", # TODO: add docstrings to public code
|
|
52
|
+
"FA", # TODO: consider should we use __annotations__
|
|
53
|
+
"TD", # no task tracking
|
|
54
|
+
"FIX", # TODO: consider enable it against new code on pull requests
|
|
55
|
+
"COM812", # because ruff format suggests to skip it
|
|
56
|
+
"ISC001", # because ruff format suggests to skip it
|
|
57
|
+
"RET505", # clashes with mypy exhaustiveness check
|
|
58
|
+
"S101", # allow asserts for tests checks and mypy help
|
|
59
|
+
# TODO: stop ignore this rule
|
|
60
|
+
"UP007", # because of 3.9 support
|
|
61
|
+
# TODO: stop ignore this rule
|
|
62
|
+
"SIM117", # because ast builder 2.0 uses with statement to build contextual scope statemetns
|
|
63
|
+
]
|
|
64
|
+
|
|
65
|
+
[tool.ruff.lint.per-file-ignores]
|
|
66
|
+
"tests/**" = [
|
|
67
|
+
"PLR0913", # test functions can use a lots of arguments and fixtures
|
|
68
|
+
]
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
[tool.mypy]
|
|
72
|
+
files = ["src", "tests"]
|
|
73
|
+
strict = true
|
|
74
|
+
disallow_any_unimported = true
|
|
75
|
+
disallow_any_expr = true
|
|
76
|
+
disallow_any_decorated = true
|
|
77
|
+
disallow_any_explicit = true
|
|
78
|
+
disallow_any_generics = true
|
|
79
|
+
disallow_subclassing_any = true
|
|
80
|
+
disallow_untyped_calls = true
|
|
81
|
+
disallow_untyped_defs = true
|
|
82
|
+
disallow_incomplete_defs = true
|
|
83
|
+
disallow_untyped_decorators = true
|
|
84
|
+
warn_redundant_casts = true
|
|
85
|
+
warn_unused_ignores = true
|
|
86
|
+
warn_no_return = true
|
|
87
|
+
warn_return_any = true
|
|
88
|
+
warn_unreachable = true
|
|
89
|
+
strict_equality = true
|
|
90
|
+
strict_optional = true
|
|
91
|
+
enable_error_code = [
|
|
92
|
+
"redundant-self",
|
|
93
|
+
"redundant-expr",
|
|
94
|
+
"possibly-undefined",
|
|
95
|
+
"truthy-bool",
|
|
96
|
+
"truthy-iterable",
|
|
97
|
+
"ignore-without-code",
|
|
98
|
+
"unused-awaitable",
|
|
99
|
+
"explicit-override",
|
|
100
|
+
"mutable-override",
|
|
101
|
+
"unimported-reveal",
|
|
102
|
+
"narrowed-type-not-subtype",
|
|
103
|
+
]
|
|
104
|
+
|
|
105
|
+
# NOTE: allow return `typing.Any` in test fixtures (e.g. mock objects created with `create_autospec`)
|
|
106
|
+
[[tool.mypy.overrides]]
|
|
107
|
+
module = ["tests.*"]
|
|
108
|
+
disallow_any_expr = false
|
|
109
|
+
disallow_any_explicit = false
|
|
110
|
+
warn_return_any = false
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
[tool.pytest.ini_options]
|
|
114
|
+
pythonpath = [
|
|
115
|
+
"src",
|
|
116
|
+
]
|
|
117
|
+
addopts = [
|
|
118
|
+
"--cov=src",
|
|
119
|
+
"--cov-report=term-missing",
|
|
120
|
+
]
|
|
121
|
+
testpaths = [
|
|
122
|
+
"tests",
|
|
123
|
+
]
|
|
124
|
+
|
|
125
|
+
[tool.coverage.run]
|
|
126
|
+
branch = true
|
|
127
|
+
|
|
128
|
+
[tool.coverage.report]
|
|
129
|
+
exclude_lines = [
|
|
130
|
+
"pragma: no cover",
|
|
131
|
+
"@abc.abstractmethod",
|
|
132
|
+
"if __name__ == .__main__.:",
|
|
133
|
+
"if t.TYPE_CHECKING:",
|
|
134
|
+
]
|
|
135
|
+
show_missing = true
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
__all__ = [
|
|
2
|
+
"Self",
|
|
3
|
+
"TypeGuard",
|
|
4
|
+
"assert_never",
|
|
5
|
+
"override",
|
|
6
|
+
]
|
|
7
|
+
|
|
8
|
+
import typing as t
|
|
9
|
+
|
|
10
|
+
# NOTE: this allows to use methods with `Self` during runtime (when typing_extensions is not installed).
|
|
11
|
+
if t.TYPE_CHECKING:
|
|
12
|
+
from typing_extensions import Self, TypeGuard, assert_never, override
|
|
13
|
+
|
|
14
|
+
else:
|
|
15
|
+
Self = t.Any
|
|
16
|
+
TypeGuard = t.Optional
|
|
17
|
+
|
|
18
|
+
def assert_never(*args: object, **kwargs: object) -> t.NoReturn:
|
|
19
|
+
raise RuntimeError(args, kwargs) # pragma: no cover
|
|
20
|
+
|
|
21
|
+
def override(func: object) -> object:
|
|
22
|
+
return func
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
__all__ = [
|
|
4
|
+
"ExpressionASTBuilder",
|
|
5
|
+
"StatementASTBuilder",
|
|
6
|
+
"TypeInfoProvider",
|
|
7
|
+
]
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
import abc
|
|
11
|
+
import typing as t
|
|
12
|
+
|
|
13
|
+
if t.TYPE_CHECKING:
|
|
14
|
+
import ast
|
|
15
|
+
|
|
16
|
+
from astlab.info import TypeInfo
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class ExpressionASTBuilder(metaclass=abc.ABCMeta):
|
|
20
|
+
@abc.abstractmethod
|
|
21
|
+
def build(self) -> ast.expr:
|
|
22
|
+
raise NotImplementedError
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class StatementASTBuilder(metaclass=abc.ABCMeta):
|
|
26
|
+
@abc.abstractmethod
|
|
27
|
+
def build(self) -> t.Sequence[ast.stmt]:
|
|
28
|
+
raise NotImplementedError
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
class TypeInfoProvider(metaclass=abc.ABCMeta):
|
|
32
|
+
@abc.abstractmethod
|
|
33
|
+
def provide_type_info(self) -> TypeInfo:
|
|
34
|
+
raise NotImplementedError
|