-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
97c5e00
commit 5bdc3e8
Showing
6 changed files
with
76 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
from core_codemods.fix_task_instantiation import FixTaskInstantiation | ||
from integration_tests.base_test import ( | ||
BaseIntegrationTest, | ||
original_and_expected_from_code_path, | ||
) | ||
|
||
|
||
class TestFixTaskInstantiation(BaseIntegrationTest): | ||
codemod = FixTaskInstantiation | ||
code_path = "tests/samples/fix_task_instantiation.py" | ||
original_code, expected_new_code = original_and_expected_from_code_path( | ||
code_path, | ||
[ | ||
( | ||
7, | ||
""" task = asyncio.create_task(my_coroutine(), name="my task")\n""", | ||
), | ||
], | ||
) | ||
|
||
# fmt: off | ||
expected_diff =( | ||
"""--- \n""" | ||
"""+++ \n""" | ||
"""@@ -5,7 +5,7 @@\n""" | ||
""" print("Task completed")\n""" | ||
""" \n""" | ||
""" async def main():\n""" | ||
"""- task = asyncio.Task(my_coroutine(), name="my task")\n""" | ||
"""+ task = asyncio.create_task(my_coroutine(), name="my task")\n""" | ||
""" await task\n""" | ||
""" \n""" | ||
""" asyncio.run(main())\n""" | ||
) | ||
# fmt: on | ||
|
||
expected_line_change = "8" | ||
change_description = FixTaskInstantiation.change_description | ||
num_changed_files = 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
src/core_codemods/docs/pixee_python_fix-task-instantiation.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
The `asyncio` [documentation](https://docs.python.org/3/library/asyncio-task.html#asyncio.Task) explicitly discourages manual instantiation of a `Task` instance and instead recommends calling `create_task`. | ||
|
||
Our changes look like the following: | ||
```diff | ||
import asyncio | ||
|
||
- task = asyncio.Task(my_coroutine(), name="my task") | ||
+ task = asyncio.create_task(my_coroutine(), name="my task") | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import asyncio | ||
|
||
async def my_coroutine(): | ||
await asyncio.sleep(1) | ||
print("Task completed") | ||
|
||
async def main(): | ||
task = asyncio.Task(my_coroutine(), name="my task") | ||
await task | ||
|
||
asyncio.run(main()) |