-
-
Notifications
You must be signed in to change notification settings - Fork 671
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
9d1f105
commit d9dc536
Showing
8 changed files
with
164 additions
and
30 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,40 @@ | ||
Typer allows you to use [async](https://docs.python.org/3/library/asyncio.html) functions. | ||
|
||
```Python | ||
{!../docs_src/async_cmd/async001.py!} | ||
``` | ||
|
||
<div class="termy"> | ||
|
||
```console | ||
$ python async.py | ||
|
||
Hello Async World | ||
``` | ||
|
||
</div> | ||
|
||
It also works with commands, and you can mix regular and async commands: | ||
|
||
```Python | ||
{!../docs_src/async_cmd/async002.py!} | ||
``` | ||
|
||
<div class="termy"> | ||
|
||
```console | ||
$ python async.py sync | ||
|
||
Hello Sync World | ||
|
||
$ python async.py async | ||
|
||
Hello Async World | ||
``` | ||
</div> | ||
|
||
!!! info | ||
Under the hood, Typer is running your async functions with [asyncio.run()](https://docs.python.org/3/library/asyncio-runner.html#asyncio.run) | ||
|
||
!!! warning | ||
Typer only supports async functions on Python 3.7+ |
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,13 @@ | ||
import asyncio | ||
import typer | ||
|
||
app = typer.Typer() | ||
|
||
|
||
async def main(): | ||
await asyncio.sleep(0) | ||
print("Hello Async World") | ||
|
||
|
||
if __name__ == "__main__": | ||
typer.run(main) |
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,19 @@ | ||
import asyncio | ||
import typer | ||
|
||
app = typer.Typer() | ||
|
||
|
||
@app.command("sync") | ||
def command_sync(): | ||
print("Hello Sync World") | ||
|
||
|
||
@app.command("async") | ||
async def command_async(): | ||
await asyncio.sleep(0) | ||
print("Hello Async World") | ||
|
||
|
||
if __name__ == "__main__": | ||
app() |
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 was deleted.
Oops, something went wrong.
Empty file.
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,38 @@ | ||
import subprocess | ||
import sys | ||
import pytest | ||
|
||
import typer | ||
from typer.testing import CliRunner | ||
|
||
from docs_src.async_cmd import async001 as mod | ||
|
||
runner = CliRunner() | ||
|
||
@pytest.mark.skipif(sys.version_info < (3, 7), reason="typer support for async functions requires python3.7 or higher") | ||
def test_cli(): | ||
app = typer.Typer() | ||
app.command()(mod.main) | ||
result = runner.invoke(app, []) | ||
assert result.output == "Hello Async World\n" | ||
|
||
|
||
@pytest.mark.skipif(sys.version_info < (3, 7), reason="typer support for async functions requires python3.7 or higher") | ||
def test_execute(): | ||
result = subprocess.run( | ||
[sys.executable, "-m", "coverage", "run", mod.__file__], | ||
stdout=subprocess.PIPE, | ||
stderr=subprocess.PIPE, | ||
encoding="utf-8", | ||
) | ||
assert result.stdout == "Hello Async World\n" | ||
|
||
|
||
def test_script(): | ||
result = subprocess.run( | ||
[sys.executable, "-m", "coverage", "run", mod.__file__, "--help"], | ||
stdout=subprocess.PIPE, | ||
stderr=subprocess.PIPE, | ||
encoding="utf-8", | ||
) | ||
assert "Usage" in result.stdout |
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,53 @@ | ||
import subprocess | ||
import sys | ||
import pytest | ||
|
||
import typer | ||
from typer.testing import CliRunner | ||
|
||
from docs_src.async_cmd import async002 as mod | ||
|
||
app = mod.app | ||
|
||
runner = CliRunner() | ||
|
||
def test_command_sync(): | ||
result = runner.invoke(app, ["sync"]) | ||
assert result.output == "Hello Sync World\n" | ||
|
||
|
||
@pytest.mark.skipif(sys.version_info < (3, 7), reason="typer support for async functions requires python3.7 or higher") | ||
def test_command_async(): | ||
result = runner.invoke(app, ["async"]) | ||
assert result.output == "Hello Async World\n" | ||
|
||
|
||
def test_execute_sync(): | ||
result = subprocess.run( | ||
[sys.executable, "-m", "coverage", "run", mod.__file__, "sync"], | ||
stdout=subprocess.PIPE, | ||
stderr=subprocess.PIPE, | ||
encoding="utf-8", | ||
) | ||
assert result.stdout == "Hello Sync World\n" | ||
|
||
|
||
@pytest.mark.skipif(sys.version_info < (3, 7), reason="typer support for async functions requires python3.7 or higher") | ||
def test_execute_async(): | ||
result = subprocess.run( | ||
[sys.executable, "-m", "coverage", "run", mod.__file__, "async"], | ||
stdout=subprocess.PIPE, | ||
stderr=subprocess.PIPE, | ||
encoding="utf-8", | ||
) | ||
assert result.stdout == "Hello Async World\n" | ||
|
||
|
||
def test_script(): | ||
result = subprocess.run( | ||
[sys.executable, "-m", "coverage", "run", mod.__file__, "--help"], | ||
stdout=subprocess.PIPE, | ||
stderr=subprocess.PIPE, | ||
encoding="utf-8", | ||
) | ||
assert "Usage" in result.stdout |