Skip to content

Commit

Permalink
tiangolo#88: Remove the loop factory to simplify the PR and make mypy…
Browse files Browse the repository at this point in the history
… happy
  • Loading branch information
borissmidt committed Jul 17, 2023
1 parent bb91319 commit 76cc37d
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 25 deletions.
17 changes: 0 additions & 17 deletions tests/test_annotated.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,20 +73,3 @@ async def cmd(val: Annotated[int, typer.Argument()] = 0):
result = runner.invoke(app, ["42"])
assert result.exit_code == 0, result.output
assert "hello 42" in result.output

if sys.version_info >= (3, 11):
def test_runner_can_use_a_custom_async_loop():
import asyncio
app = typer.Typer(loop_factory=asyncio.new_event_loop)
@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

10 changes: 2 additions & 8 deletions typer/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,8 @@
console_stderr = Console(stderr=True)

except ImportError: # pragma: nocover
rich = None # type: ignore
rich = None # type: ignore

from asyncio import AbstractEventLoop

_original_except_hook = sys.excepthook
_typer_developer_exception_attr_name = "__typer_developer_exception__"
Expand Down Expand Up @@ -142,7 +141,6 @@ def __init__(
pretty_exceptions_enable: bool = True,
pretty_exceptions_show_locals: bool = True,
pretty_exceptions_short: bool = True,
loop_factory: Optional[Callable[[], AbstractEventLoop]] = None,
):
self._add_completion = add_completion
self.rich_markup_mode: MarkupMode = rich_markup_mode
Expand Down Expand Up @@ -171,7 +169,6 @@ def __init__(
self.registered_groups: List[TyperInfo] = []
self.registered_commands: List[CommandInfo] = []
self.registered_callback: Optional[TyperInfo] = None
self.loop_factory = loop_factory

def callback(
self,
Expand Down Expand Up @@ -243,10 +240,7 @@ 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, 11) and self.loop_factory:
with asyncio.Runner(loop_factory=self.loop_factory) as runner: #type: ignore
return runner.run(f(*args, **kwargs))
elif sys.version_info >= (3, 7):
if sys.version_info >= (3, 7):
return asyncio.run(f(*args, **kwargs))
else:
asyncio.get_event_loop().run_until_complete(f(*args, **kwargs))
Expand Down

0 comments on commit 76cc37d

Please sign in to comment.