Skip to content

Commit

Permalink
Added documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
paulo-raca committed Jul 13, 2023
1 parent 9d1f105 commit d9dc536
Show file tree
Hide file tree
Showing 8 changed files with 164 additions and 30 deletions.
40 changes: 40 additions & 0 deletions docs/tutorial/async-cmd.md
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+
13 changes: 13 additions & 0 deletions docs_src/async_cmd/async001.py
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)
19 changes: 19 additions & 0 deletions docs_src/async_cmd/async002.py
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()
1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ nav:
- Typer Callback: tutorial/commands/callback.md
- One or Multiple Commands: tutorial/commands/one-or-multiple.md
- Using the Context: tutorial/commands/context.md
- Asynchronous functions: tutorial/async-cmd.md
- CLI Option autocompletion: tutorial/options-autocompletion.md
- CLI Parameter Types:
- CLI Parameter Types Intro: tutorial/parameter-types/index.md
Expand Down
30 changes: 0 additions & 30 deletions tests/test_asyncio.py

This file was deleted.

Empty file.
38 changes: 38 additions & 0 deletions tests/test_tutorial/test_async_cmd/test_async001.py
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
53 changes: 53 additions & 0 deletions tests/test_tutorial/test_async_cmd/test_async002.py
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

0 comments on commit d9dc536

Please sign in to comment.