Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#88: Add asyncio support #633

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions tests/test_annotated.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,19 @@ def cmd(force: Annotated[bool, typer.Option("--force")] = False):
result = runner.invoke(app, ["--force"])
assert result.exit_code == 0, result.output
assert "Forcing operation" in result.output


def test_runner_can_use_an_async_method():
app = typer.Typer()

@app.command()
async def cmd(val: Annotated[int, typer.Argument()] = 0):
print(f"hello {val}")

result = runner.invoke(app)
assert result.exit_code == 0, result.output
assert "hello 0" in result.output

result = runner.invoke(app, ["42"])
assert result.exit_code == 0, result.output
assert "hello 42" in result.output
9 changes: 1 addition & 8 deletions tests/test_completion/test_completion_install.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@
from unittest import mock

import shellingham
import typer
from typer.testing import CliRunner

from docs_src.commands.index import tutorial001 as mod

runner = CliRunner()
app = typer.Typer()
app.command()(mod.main)
app = mod.app


def test_completion_install_no_shell():
Expand Down Expand Up @@ -145,11 +143,6 @@ def test_completion_install_fish():
assert "Completion will take effect once you restart the terminal" in result.stdout


runner = CliRunner()
app = typer.Typer()
app.command()(mod.main)


def test_completion_install_powershell():
completion_path: Path = (
Path.home() / f".config/powershell/Microsoft.PowerShell_profile.ps1"
Expand Down
21 changes: 19 additions & 2 deletions typer/main.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import asyncio
import inspect
import os
import sys
import traceback
from datetime import datetime
from enum import Enum
from functools import update_wrapper
from functools import update_wrapper, wraps
from pathlib import Path
from traceback import FrameSummary, StackSummary
from types import TracebackType
Expand Down Expand Up @@ -46,6 +47,7 @@
except ImportError: # pragma: nocover
rich = None # type: ignore


_original_except_hook = sys.excepthook
_typer_developer_exception_attr_name = "__typer_developer_exception__"

Expand Down Expand Up @@ -235,12 +237,27 @@ def command(
cls = TyperCommand

def decorator(f: CommandFunctionType) -> CommandFunctionType:
def add_runner(f: CommandFunctionType) -> CommandFunctionType:
@wraps(f)
def run_wrapper(*args: Any, **kwargs: Any) -> Any:
if sys.version_info >= (3, 7):
return asyncio.run(f(*args, **kwargs))
else:
asyncio.get_event_loop().run_until_complete(f(*args, **kwargs))

return run_wrapper # type: ignore

if inspect.iscoroutinefunction(f):
callback = add_runner(f)
else:
callback = f

self.registered_commands.append(
CommandInfo(
name=name,
cls=cls,
context_settings=context_settings,
callback=f,
callback=callback,
help=help,
epilog=epilog,
short_help=short_help,
Expand Down