Skip to content

Commit 9071ce2

Browse files
committed
feat: async console extras
1 parent 53a84ff commit 9071ce2

2 files changed

Lines changed: 23 additions & 0 deletions

File tree

src/ape_console/_cli.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,10 @@ def _load_extras_file(self, extras_file: Path) -> dict:
158158
func_spec = inspect.getfullargspec(ape_init_extras)
159159
init_kwargs: dict[str, Any] = {k: self._get_from_ape(k) for k in func_spec.args}
160160
extras = ape_init_extras(**init_kwargs)
161+
if inspect.iscoroutine(extras):
162+
from ape.utils import run_until_complete
163+
164+
extras = run_until_complete(extras)
161165

162166
if isinstance(extras, dict):
163167
all_extras.update(extras)

tests/functional/test_console.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,25 @@ def test_extras_load_using_ape_namespace(self, scope):
8585
extras_content = """
8686
def ape_init_extras(project):
8787
return {"foo": type(project)}
88+
"""
89+
with create_tempdir() as temp:
90+
extras_file = temp / CONSOLE_EXTRAS_FILENAME
91+
extras_file.write_text(extras_content)
92+
extras.__dict__[f"_{scope}_path"] = extras_file
93+
extras.__dict__.pop(f"_{scope}_extras", None)
94+
assert extras["foo"] is LocalProject
95+
96+
@pytest.mark.parametrize("scope", ("local", "global"))
97+
def test_extras_async_ape_init_extras(self, scope):
98+
"""
99+
``ape_init_extras`` is allowed to be defined as an ``async`` function;
100+
it gets awaited before its result is merged into the namespace.
101+
"""
102+
extras = ApeConsoleNamespace()
103+
_ = getattr(extras, f"_{scope}_path")
104+
extras_content = """
105+
async def ape_init_extras(project):
106+
return {"foo": type(project)}
88107
"""
89108
with create_tempdir() as temp:
90109
extras_file = temp / CONSOLE_EXTRAS_FILENAME

0 commit comments

Comments
 (0)