From d9ca579419829eb6d613e0e2b7085ef940b08156 Mon Sep 17 00:00:00 2001 From: David Bieber Date: Thu, 19 Sep 2024 20:41:42 -0700 Subject: [PATCH] Move asyncio imports and update docs --- docs/guide.md | 19 +++++++++++++++++++ fire/core.py | 3 +-- fire/inspectutils.py | 3 +-- 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/docs/guide.md b/docs/guide.md index cdc3b2d0..444a76ff 100644 --- a/docs/guide.md +++ b/docs/guide.md @@ -589,6 +589,25 @@ default values that you don't want to specify. It is also important to remember to change the separator if you want to pass `-` as an argument. +##### Async Functions + +Fire supports calling async functions too. Here's a simple example. + +```python +import asyncio + +async def count_to_ten(): + for i in range(1, 11): + await asyncio.sleep(1) + print(i) + +if __name__ == '__main__': + fire.Fire(count_to_ten) +``` + +Whenever fire encounters a coroutine function, it runs it, blocking until it completes. + + ### Argument Parsing The types of the arguments are determined by their values, rather than by the diff --git a/fire/core.py b/fire/core.py index e4156760..6cd1907e 100644 --- a/fire/core.py +++ b/fire/core.py @@ -49,6 +49,7 @@ def main(argv): --trace: Get the Fire Trace for the command. """ +import asyncio import inspect import json import os @@ -68,8 +69,6 @@ def main(argv): from fire import value_types from fire.console import console_io -import asyncio # pylint: disable=import-error,g-import-not-at-top # pytype: disable=import-error - def Fire(component=None, command=None, name=None, serialize=None): """This function, Fire, is the main entrypoint for Python Fire. diff --git a/fire/inspectutils.py b/fire/inspectutils.py index 0d0b048d..a3ae7c27 100644 --- a/fire/inspectutils.py +++ b/fire/inspectutils.py @@ -14,14 +14,13 @@ """Inspection utility functions for Python Fire.""" +import asyncio import inspect import sys import types from fire import docstrings -import asyncio # pylint: disable=import-error,g-import-not-at-top # pytype: disable=import-error - class FullArgSpec(object): """The arguments of a function, as in Python 3's inspect.FullArgSpec."""