asyncify-codemod 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.
- asyncify_codemod-0.1.0/.cursor/rules/uv-pytest.mdc +7 -0
- asyncify_codemod-0.1.0/.github/workflows/publish.yml +31 -0
- asyncify_codemod-0.1.0/.gitignore +207 -0
- asyncify_codemod-0.1.0/LICENSE +21 -0
- asyncify_codemod-0.1.0/Makefile +15 -0
- asyncify_codemod-0.1.0/PKG-INFO +89 -0
- asyncify_codemod-0.1.0/README.md +56 -0
- asyncify_codemod-0.1.0/core_test.py +248 -0
- asyncify_codemod-0.1.0/pyproject.toml +23 -0
- asyncify_codemod-0.1.0/src/asyncify/__init__.py +3 -0
- asyncify_codemod-0.1.0/src/asyncify/cli.py +47 -0
- asyncify_codemod-0.1.0/src/asyncify/core.py +339 -0
- asyncify_codemod-0.1.0/uv.lock +351 -0
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
name: Publish to PyPI
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
tags:
|
|
6
|
+
- 'v*'
|
|
7
|
+
|
|
8
|
+
permissions:
|
|
9
|
+
contents: read
|
|
10
|
+
id-token: write
|
|
11
|
+
|
|
12
|
+
jobs:
|
|
13
|
+
build-and-publish:
|
|
14
|
+
runs-on: ubuntu-latest
|
|
15
|
+
steps:
|
|
16
|
+
- name: Check out
|
|
17
|
+
uses: actions/checkout@v4
|
|
18
|
+
|
|
19
|
+
- name: Set up Python
|
|
20
|
+
uses: actions/setup-python@v5
|
|
21
|
+
with:
|
|
22
|
+
python-version: '3.12'
|
|
23
|
+
|
|
24
|
+
- name: Install build tooling
|
|
25
|
+
run: python -m pip install --upgrade build
|
|
26
|
+
|
|
27
|
+
- name: Build distributions
|
|
28
|
+
run: python -m build
|
|
29
|
+
|
|
30
|
+
- name: Publish to PyPI
|
|
31
|
+
uses: pypa/gh-action-pypi-publish@release/v1
|
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
# Byte-compiled / optimized / DLL files
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.py[codz]
|
|
4
|
+
*$py.class
|
|
5
|
+
|
|
6
|
+
# C extensions
|
|
7
|
+
*.so
|
|
8
|
+
|
|
9
|
+
# Distribution / packaging
|
|
10
|
+
.Python
|
|
11
|
+
build/
|
|
12
|
+
develop-eggs/
|
|
13
|
+
dist/
|
|
14
|
+
downloads/
|
|
15
|
+
eggs/
|
|
16
|
+
.eggs/
|
|
17
|
+
lib/
|
|
18
|
+
lib64/
|
|
19
|
+
parts/
|
|
20
|
+
sdist/
|
|
21
|
+
var/
|
|
22
|
+
wheels/
|
|
23
|
+
share/python-wheels/
|
|
24
|
+
*.egg-info/
|
|
25
|
+
.installed.cfg
|
|
26
|
+
*.egg
|
|
27
|
+
MANIFEST
|
|
28
|
+
|
|
29
|
+
# PyInstaller
|
|
30
|
+
# Usually these files are written by a python script from a template
|
|
31
|
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
|
32
|
+
*.manifest
|
|
33
|
+
*.spec
|
|
34
|
+
|
|
35
|
+
# Installer logs
|
|
36
|
+
pip-log.txt
|
|
37
|
+
pip-delete-this-directory.txt
|
|
38
|
+
|
|
39
|
+
# Unit test / coverage reports
|
|
40
|
+
htmlcov/
|
|
41
|
+
.tox/
|
|
42
|
+
.nox/
|
|
43
|
+
.coverage
|
|
44
|
+
.coverage.*
|
|
45
|
+
.cache
|
|
46
|
+
nosetests.xml
|
|
47
|
+
coverage.xml
|
|
48
|
+
*.cover
|
|
49
|
+
*.py.cover
|
|
50
|
+
.hypothesis/
|
|
51
|
+
.pytest_cache/
|
|
52
|
+
cover/
|
|
53
|
+
|
|
54
|
+
# Translations
|
|
55
|
+
*.mo
|
|
56
|
+
*.pot
|
|
57
|
+
|
|
58
|
+
# Django stuff:
|
|
59
|
+
*.log
|
|
60
|
+
local_settings.py
|
|
61
|
+
db.sqlite3
|
|
62
|
+
db.sqlite3-journal
|
|
63
|
+
|
|
64
|
+
# Flask stuff:
|
|
65
|
+
instance/
|
|
66
|
+
.webassets-cache
|
|
67
|
+
|
|
68
|
+
# Scrapy stuff:
|
|
69
|
+
.scrapy
|
|
70
|
+
|
|
71
|
+
# Sphinx documentation
|
|
72
|
+
docs/_build/
|
|
73
|
+
|
|
74
|
+
# PyBuilder
|
|
75
|
+
.pybuilder/
|
|
76
|
+
target/
|
|
77
|
+
|
|
78
|
+
# Jupyter Notebook
|
|
79
|
+
.ipynb_checkpoints
|
|
80
|
+
|
|
81
|
+
# IPython
|
|
82
|
+
profile_default/
|
|
83
|
+
ipython_config.py
|
|
84
|
+
|
|
85
|
+
# pyenv
|
|
86
|
+
# For a library or package, you might want to ignore these files since the code is
|
|
87
|
+
# intended to run in multiple environments; otherwise, check them in:
|
|
88
|
+
# .python-version
|
|
89
|
+
|
|
90
|
+
# pipenv
|
|
91
|
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
|
92
|
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
|
93
|
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
|
94
|
+
# install all needed dependencies.
|
|
95
|
+
#Pipfile.lock
|
|
96
|
+
|
|
97
|
+
# UV
|
|
98
|
+
# Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control.
|
|
99
|
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
|
100
|
+
# commonly ignored for libraries.
|
|
101
|
+
#uv.lock
|
|
102
|
+
|
|
103
|
+
# poetry
|
|
104
|
+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
|
105
|
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
|
106
|
+
# commonly ignored for libraries.
|
|
107
|
+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
|
108
|
+
#poetry.lock
|
|
109
|
+
#poetry.toml
|
|
110
|
+
|
|
111
|
+
# pdm
|
|
112
|
+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
|
113
|
+
# pdm recommends including project-wide configuration in pdm.toml, but excluding .pdm-python.
|
|
114
|
+
# https://pdm-project.org/en/latest/usage/project/#working-with-version-control
|
|
115
|
+
#pdm.lock
|
|
116
|
+
#pdm.toml
|
|
117
|
+
.pdm-python
|
|
118
|
+
.pdm-build/
|
|
119
|
+
|
|
120
|
+
# pixi
|
|
121
|
+
# Similar to Pipfile.lock, it is generally recommended to include pixi.lock in version control.
|
|
122
|
+
#pixi.lock
|
|
123
|
+
# Pixi creates a virtual environment in the .pixi directory, just like venv module creates one
|
|
124
|
+
# in the .venv directory. It is recommended not to include this directory in version control.
|
|
125
|
+
.pixi
|
|
126
|
+
|
|
127
|
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
|
128
|
+
__pypackages__/
|
|
129
|
+
|
|
130
|
+
# Celery stuff
|
|
131
|
+
celerybeat-schedule
|
|
132
|
+
celerybeat.pid
|
|
133
|
+
|
|
134
|
+
# SageMath parsed files
|
|
135
|
+
*.sage.py
|
|
136
|
+
|
|
137
|
+
# Environments
|
|
138
|
+
.env
|
|
139
|
+
.envrc
|
|
140
|
+
.venv
|
|
141
|
+
env/
|
|
142
|
+
venv/
|
|
143
|
+
ENV/
|
|
144
|
+
env.bak/
|
|
145
|
+
venv.bak/
|
|
146
|
+
|
|
147
|
+
# Spyder project settings
|
|
148
|
+
.spyderproject
|
|
149
|
+
.spyproject
|
|
150
|
+
|
|
151
|
+
# Rope project settings
|
|
152
|
+
.ropeproject
|
|
153
|
+
|
|
154
|
+
# mkdocs documentation
|
|
155
|
+
/site
|
|
156
|
+
|
|
157
|
+
# mypy
|
|
158
|
+
.mypy_cache/
|
|
159
|
+
.dmypy.json
|
|
160
|
+
dmypy.json
|
|
161
|
+
|
|
162
|
+
# Pyre type checker
|
|
163
|
+
.pyre/
|
|
164
|
+
|
|
165
|
+
# pytype static type analyzer
|
|
166
|
+
.pytype/
|
|
167
|
+
|
|
168
|
+
# Cython debug symbols
|
|
169
|
+
cython_debug/
|
|
170
|
+
|
|
171
|
+
# PyCharm
|
|
172
|
+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
|
173
|
+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
|
174
|
+
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
|
175
|
+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
|
176
|
+
#.idea/
|
|
177
|
+
|
|
178
|
+
# Abstra
|
|
179
|
+
# Abstra is an AI-powered process automation framework.
|
|
180
|
+
# Ignore directories containing user credentials, local state, and settings.
|
|
181
|
+
# Learn more at https://abstra.io/docs
|
|
182
|
+
.abstra/
|
|
183
|
+
|
|
184
|
+
# Visual Studio Code
|
|
185
|
+
# Visual Studio Code specific template is maintained in a separate VisualStudioCode.gitignore
|
|
186
|
+
# that can be found at https://github.com/github/gitignore/blob/main/Global/VisualStudioCode.gitignore
|
|
187
|
+
# and can be added to the global gitignore or merged into this file. However, if you prefer,
|
|
188
|
+
# you could uncomment the following to ignore the entire vscode folder
|
|
189
|
+
# .vscode/
|
|
190
|
+
|
|
191
|
+
# Ruff stuff:
|
|
192
|
+
.ruff_cache/
|
|
193
|
+
|
|
194
|
+
# PyPI configuration file
|
|
195
|
+
.pypirc
|
|
196
|
+
|
|
197
|
+
# Cursor
|
|
198
|
+
# Cursor is an AI-powered code editor. `.cursorignore` specifies files/directories to
|
|
199
|
+
# exclude from AI features like autocomplete and code analysis. Recommended for sensitive data
|
|
200
|
+
# refer to https://docs.cursor.com/context/ignore-files
|
|
201
|
+
.cursorignore
|
|
202
|
+
.cursorindexingignore
|
|
203
|
+
|
|
204
|
+
# Marimo
|
|
205
|
+
marimo/_static/
|
|
206
|
+
marimo/_lsp/
|
|
207
|
+
__marimo__/
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 ryan
|
|
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.
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
UV ?= uv
|
|
2
|
+
|
|
3
|
+
.PHONY: build publish publish-test clean
|
|
4
|
+
|
|
5
|
+
build:
|
|
6
|
+
$(UV) build
|
|
7
|
+
|
|
8
|
+
publish: build
|
|
9
|
+
$(UV) tool run twine upload dist/*
|
|
10
|
+
|
|
11
|
+
publish-test: build
|
|
12
|
+
$(UV) tool run twine upload --repository testpypi dist/*
|
|
13
|
+
|
|
14
|
+
clean:
|
|
15
|
+
rm -rf dist build *.egg-info
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: asyncify-codemod
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Codemod to convert synchronous Python code to async.
|
|
5
|
+
License: MIT License
|
|
6
|
+
|
|
7
|
+
Copyright (c) 2026 ryan
|
|
8
|
+
|
|
9
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
10
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
11
|
+
in the Software without restriction, including without limitation the rights
|
|
12
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
13
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
14
|
+
furnished to do so, subject to the following conditions:
|
|
15
|
+
|
|
16
|
+
The above copyright notice and this permission notice shall be included in all
|
|
17
|
+
copies or substantial portions of the Software.
|
|
18
|
+
|
|
19
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
20
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
21
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
22
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
23
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
24
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
25
|
+
SOFTWARE.
|
|
26
|
+
License-File: LICENSE
|
|
27
|
+
Requires-Python: >=3.10
|
|
28
|
+
Requires-Dist: anyio
|
|
29
|
+
Requires-Dist: black
|
|
30
|
+
Requires-Dist: httpx
|
|
31
|
+
Requires-Dist: pytest>=9.0.2
|
|
32
|
+
Description-Content-Type: text/markdown
|
|
33
|
+
|
|
34
|
+
# asyncify-codemod
|
|
35
|
+
|
|
36
|
+
`asyncify-codemod` is a codemod that converts synchronous Python code into async where
|
|
37
|
+
possible. It focuses on a few common patterns (like `requests`, `time.sleep`,
|
|
38
|
+
and file I/O) and rewrites them to async equivalents.
|
|
39
|
+
|
|
40
|
+
## Install with pipx
|
|
41
|
+
|
|
42
|
+
```bash
|
|
43
|
+
pipx install .
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
## Install from PyPI
|
|
47
|
+
|
|
48
|
+
```bash
|
|
49
|
+
pipx install asyncify-codemod
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
Or with pip:
|
|
53
|
+
|
|
54
|
+
```bash
|
|
55
|
+
pip install asyncify-codemod
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
## Usage
|
|
59
|
+
|
|
60
|
+
```bash
|
|
61
|
+
asyncify-codemod path/to/file.py
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
To print to stdout instead of overwriting:
|
|
65
|
+
|
|
66
|
+
```bash
|
|
67
|
+
asyncify-codemod --stdout path/to/file.py
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
To skip Black formatting:
|
|
71
|
+
|
|
72
|
+
```bash
|
|
73
|
+
asyncify-codemod --no-format path/to/file.py
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
## Release
|
|
77
|
+
|
|
78
|
+
Build and publish to PyPI:
|
|
79
|
+
|
|
80
|
+
```bash
|
|
81
|
+
make build
|
|
82
|
+
make publish
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
Publish to TestPyPI:
|
|
86
|
+
|
|
87
|
+
```bash
|
|
88
|
+
make publish-test
|
|
89
|
+
```
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
# asyncify-codemod
|
|
2
|
+
|
|
3
|
+
`asyncify-codemod` is a codemod that converts synchronous Python code into async where
|
|
4
|
+
possible. It focuses on a few common patterns (like `requests`, `time.sleep`,
|
|
5
|
+
and file I/O) and rewrites them to async equivalents.
|
|
6
|
+
|
|
7
|
+
## Install with pipx
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
pipx install .
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Install from PyPI
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
pipx install asyncify-codemod
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
Or with pip:
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
pip install asyncify-codemod
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## Usage
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
asyncify-codemod path/to/file.py
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
To print to stdout instead of overwriting:
|
|
32
|
+
|
|
33
|
+
```bash
|
|
34
|
+
asyncify-codemod --stdout path/to/file.py
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
To skip Black formatting:
|
|
38
|
+
|
|
39
|
+
```bash
|
|
40
|
+
asyncify-codemod --no-format path/to/file.py
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## Release
|
|
44
|
+
|
|
45
|
+
Build and publish to PyPI:
|
|
46
|
+
|
|
47
|
+
```bash
|
|
48
|
+
make build
|
|
49
|
+
make publish
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
Publish to TestPyPI:
|
|
53
|
+
|
|
54
|
+
```bash
|
|
55
|
+
make publish-test
|
|
56
|
+
```
|
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
import unittest
|
|
2
|
+
import ast
|
|
3
|
+
from textwrap import dedent
|
|
4
|
+
|
|
5
|
+
from asyncify import AsyncTransformer, ImportCollector
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class TestAsyncify(unittest.TestCase):
|
|
9
|
+
def _run_transformer(self, source, module_sources=None):
|
|
10
|
+
source = dedent(source)
|
|
11
|
+
tree = ast.parse(source)
|
|
12
|
+
collector = ImportCollector(module_sources=module_sources)
|
|
13
|
+
collector.visit(tree)
|
|
14
|
+
transformer = AsyncTransformer(collector)
|
|
15
|
+
new_tree = transformer.visit(tree)
|
|
16
|
+
|
|
17
|
+
# Add imports
|
|
18
|
+
existing_imports = set()
|
|
19
|
+
for body_item in new_tree.body:
|
|
20
|
+
if isinstance(body_item, ast.Import):
|
|
21
|
+
for name in body_item.names:
|
|
22
|
+
existing_imports.add(name.name)
|
|
23
|
+
if isinstance(body_item, ast.ImportFrom):
|
|
24
|
+
if body_item.module:
|
|
25
|
+
existing_imports.add(body_item.module)
|
|
26
|
+
|
|
27
|
+
imports_to_add_nodes = []
|
|
28
|
+
for imp in sorted(list(transformer.imports_to_add)):
|
|
29
|
+
if imp not in existing_imports:
|
|
30
|
+
imports_to_add_nodes.append(ast.Import(names=[ast.alias(name=imp)]))
|
|
31
|
+
|
|
32
|
+
insert_pos = 0
|
|
33
|
+
for i, node in enumerate(new_tree.body):
|
|
34
|
+
if not isinstance(node, (ast.Import, ast.ImportFrom)):
|
|
35
|
+
insert_pos = i
|
|
36
|
+
break
|
|
37
|
+
|
|
38
|
+
new_tree.body = new_tree.body[:insert_pos] + imports_to_add_nodes + new_tree.body[insert_pos:]
|
|
39
|
+
|
|
40
|
+
ast.fix_missing_locations(new_tree)
|
|
41
|
+
return ast.unparse(new_tree)
|
|
42
|
+
|
|
43
|
+
def assertCodeEqual(self, a, b):
|
|
44
|
+
a_clean = "\n".join(line for line in dedent(a).splitlines() if line.strip())
|
|
45
|
+
b_clean = "\n".join(line for line in dedent(b).splitlines() if line.strip())
|
|
46
|
+
self.assertEqual(a_clean, b_clean)
|
|
47
|
+
|
|
48
|
+
def test_requests_to_httpx(self):
|
|
49
|
+
source = """
|
|
50
|
+
import requests
|
|
51
|
+
def fetch():
|
|
52
|
+
r = requests.get('https://example.com')
|
|
53
|
+
return r.text
|
|
54
|
+
"""
|
|
55
|
+
expected = """
|
|
56
|
+
import requests
|
|
57
|
+
import httpx
|
|
58
|
+
async def fetch():
|
|
59
|
+
r = await httpx.get('https://example.com')
|
|
60
|
+
return r.text
|
|
61
|
+
"""
|
|
62
|
+
self.assertCodeEqual(self._run_transformer(source), expected)
|
|
63
|
+
|
|
64
|
+
def test_open_to_anyio_open_file(self):
|
|
65
|
+
source = """
|
|
66
|
+
def read_file():
|
|
67
|
+
with open('file.txt', 'r') as f:
|
|
68
|
+
content = f.read()
|
|
69
|
+
return content
|
|
70
|
+
"""
|
|
71
|
+
expected = """
|
|
72
|
+
import anyio
|
|
73
|
+
async def read_file():
|
|
74
|
+
async with anyio.open_file('file.txt', 'r') as f:
|
|
75
|
+
content = await f.read()
|
|
76
|
+
return content
|
|
77
|
+
"""
|
|
78
|
+
self.assertCodeEqual(self._run_transformer(source), expected)
|
|
79
|
+
|
|
80
|
+
def test_time_sleep_to_anyio_sleep(self):
|
|
81
|
+
source = """
|
|
82
|
+
import time
|
|
83
|
+
def wait():
|
|
84
|
+
time.sleep(1)
|
|
85
|
+
"""
|
|
86
|
+
expected = """
|
|
87
|
+
import time
|
|
88
|
+
import anyio
|
|
89
|
+
async def wait():
|
|
90
|
+
await anyio.sleep(1)
|
|
91
|
+
"""
|
|
92
|
+
self.assertCodeEqual(self._run_transformer(source), expected)
|
|
93
|
+
|
|
94
|
+
def test_from_import_requests(self):
|
|
95
|
+
source = """
|
|
96
|
+
from requests import get
|
|
97
|
+
def fetch():
|
|
98
|
+
r = get('https://example.com')
|
|
99
|
+
return r.text
|
|
100
|
+
"""
|
|
101
|
+
expected = """
|
|
102
|
+
from requests import get
|
|
103
|
+
import httpx
|
|
104
|
+
async def fetch():
|
|
105
|
+
r = await httpx.get('https://example.com')
|
|
106
|
+
return r.text
|
|
107
|
+
"""
|
|
108
|
+
self.assertCodeEqual(self._run_transformer(source), expected)
|
|
109
|
+
|
|
110
|
+
def test_already_async_function(self):
|
|
111
|
+
source = """
|
|
112
|
+
import httpx
|
|
113
|
+
async def fetch():
|
|
114
|
+
r = await httpx.get('https://example.com')
|
|
115
|
+
return r.text
|
|
116
|
+
"""
|
|
117
|
+
self.assertCodeEqual(self._run_transformer(source), source)
|
|
118
|
+
|
|
119
|
+
def test_nested_function_def(self):
|
|
120
|
+
source = """
|
|
121
|
+
def outer():
|
|
122
|
+
def inner():
|
|
123
|
+
import time
|
|
124
|
+
time.sleep(1)
|
|
125
|
+
inner()
|
|
126
|
+
"""
|
|
127
|
+
expected = """
|
|
128
|
+
import anyio
|
|
129
|
+
async def outer():
|
|
130
|
+
async def inner():
|
|
131
|
+
import time
|
|
132
|
+
await anyio.sleep(1)
|
|
133
|
+
await inner()
|
|
134
|
+
"""
|
|
135
|
+
# The current implementation doesn't handle awaiting the inner call,
|
|
136
|
+
# but it should make the functions async.
|
|
137
|
+
# This is a known limitation to be improved.
|
|
138
|
+
transformed = self._run_transformer(source)
|
|
139
|
+
self.assertIn("async def outer():", transformed)
|
|
140
|
+
self.assertIn("async def inner():", transformed)
|
|
141
|
+
self.assertIn("await anyio.sleep(1)", transformed)
|
|
142
|
+
|
|
143
|
+
def test_no_io(self):
|
|
144
|
+
source = """
|
|
145
|
+
def my_func(a, b):
|
|
146
|
+
return a + b
|
|
147
|
+
"""
|
|
148
|
+
self.assertCodeEqual(self._run_transformer(source), source)
|
|
149
|
+
|
|
150
|
+
def test_import_addition(self):
|
|
151
|
+
source = """
|
|
152
|
+
import os
|
|
153
|
+
import requests
|
|
154
|
+
|
|
155
|
+
def main():
|
|
156
|
+
print(os.getcwd())
|
|
157
|
+
requests.get('https://example.com')
|
|
158
|
+
"""
|
|
159
|
+
|
|
160
|
+
expected = """
|
|
161
|
+
import os
|
|
162
|
+
import requests
|
|
163
|
+
import httpx
|
|
164
|
+
|
|
165
|
+
async def main():
|
|
166
|
+
print(os.getcwd())
|
|
167
|
+
await httpx.get('https://example.com')
|
|
168
|
+
"""
|
|
169
|
+
self.assertCodeEqual(self._run_transformer(source), expected)
|
|
170
|
+
|
|
171
|
+
def test_class_method(self):
|
|
172
|
+
source = """
|
|
173
|
+
import requests
|
|
174
|
+
class MyClass:
|
|
175
|
+
def fetch(self):
|
|
176
|
+
r = requests.get('https://example.com')
|
|
177
|
+
return r.text
|
|
178
|
+
"""
|
|
179
|
+
expected = """
|
|
180
|
+
import requests
|
|
181
|
+
import httpx
|
|
182
|
+
class MyClass:
|
|
183
|
+
async def fetch(self):
|
|
184
|
+
r = await httpx.get('https://example.com')
|
|
185
|
+
return r.text
|
|
186
|
+
"""
|
|
187
|
+
self.assertCodeEqual(self._run_transformer(source), expected)
|
|
188
|
+
|
|
189
|
+
def test_imported_httpx_client_instance(self):
|
|
190
|
+
a_source = """
|
|
191
|
+
import httpx
|
|
192
|
+
|
|
193
|
+
http_client = httpx.Client()
|
|
194
|
+
"""
|
|
195
|
+
b_source = """
|
|
196
|
+
from .a import http_client
|
|
197
|
+
|
|
198
|
+
def foo():
|
|
199
|
+
return http_client.post("https://example.com")
|
|
200
|
+
"""
|
|
201
|
+
expected_a = """
|
|
202
|
+
import httpx
|
|
203
|
+
|
|
204
|
+
http_client = httpx.AsyncClient()
|
|
205
|
+
"""
|
|
206
|
+
expected_b = """
|
|
207
|
+
from .a import http_client
|
|
208
|
+
async def foo():
|
|
209
|
+
return await http_client.post('https://example.com')
|
|
210
|
+
"""
|
|
211
|
+
self.assertCodeEqual(self._run_transformer(a_source), expected_a)
|
|
212
|
+
self.assertCodeEqual(
|
|
213
|
+
self._run_transformer(b_source, module_sources={"a": a_source}),
|
|
214
|
+
expected_b,
|
|
215
|
+
)
|
|
216
|
+
|
|
217
|
+
|
|
218
|
+
def test_imported_httpx_client_instance_2(self):
|
|
219
|
+
a_source = """
|
|
220
|
+
from httpx import Client
|
|
221
|
+
|
|
222
|
+
http_client = Client()
|
|
223
|
+
"""
|
|
224
|
+
b_source = """
|
|
225
|
+
from .a import http_client
|
|
226
|
+
|
|
227
|
+
def foo():
|
|
228
|
+
return http_client.post("https://example.com")
|
|
229
|
+
"""
|
|
230
|
+
expected_a = """
|
|
231
|
+
from httpx import AsyncClient as Client
|
|
232
|
+
|
|
233
|
+
http_client = Client()
|
|
234
|
+
"""
|
|
235
|
+
expected_b = """
|
|
236
|
+
from .a import http_client
|
|
237
|
+
async def foo():
|
|
238
|
+
return await http_client.post('https://example.com')
|
|
239
|
+
"""
|
|
240
|
+
self.assertCodeEqual(self._run_transformer(a_source), expected_a)
|
|
241
|
+
self.assertCodeEqual(
|
|
242
|
+
self._run_transformer(b_source, module_sources={"a": a_source}),
|
|
243
|
+
expected_b,
|
|
244
|
+
)
|
|
245
|
+
|
|
246
|
+
|
|
247
|
+
if __name__ == "__main__":
|
|
248
|
+
unittest.main()
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "asyncify-codemod"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "Codemod to convert synchronous Python code to async."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = { file = "LICENSE" }
|
|
11
|
+
requires-python = ">=3.10"
|
|
12
|
+
dependencies = [
|
|
13
|
+
"anyio",
|
|
14
|
+
"black",
|
|
15
|
+
"httpx",
|
|
16
|
+
"pytest>=9.0.2",
|
|
17
|
+
]
|
|
18
|
+
|
|
19
|
+
[project.scripts]
|
|
20
|
+
asyncify-codemod = "asyncify.cli:main"
|
|
21
|
+
|
|
22
|
+
[tool.hatch.build.targets.wheel]
|
|
23
|
+
packages = ["src/asyncify"]
|