Skip to content

Commit eddc95d

Browse files
committed
Improved task name handling
1 parent 318e6ce commit eddc95d

File tree

2 files changed

+20
-18
lines changed

2 files changed

+20
-18
lines changed

click_async_plugins/util.py

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,38 +22,42 @@ class CliContext:
2222
pass_clictx = click.make_pass_decorator(CliContext)
2323

2424

25+
@dataclass
26+
class TaskWithName:
27+
task: PluginTask | None
28+
name: str
29+
30+
2531
async def sleep_forever(sleep: float = 1, *, forever: bool = True) -> Never | None:
2632
while await asyncio.sleep(sleep, forever):
2733
pass
2834
return None
2935

3036

3137
def create_plugin_task[T](
32-
task: PluginTask | None,
38+
task: TaskWithName,
3339
*,
34-
name: str | None = None,
3540
create_task_fn: Callable[..., asyncio.Task[T]] = asyncio.create_task,
3641
) -> asyncio.Task[T]:
3742
async def task_wrapper() -> None:
3843
try:
39-
if asyncio.iscoroutine(task):
40-
logger.debug(f"Scheduling task for '{name}'")
41-
await task
44+
if asyncio.iscoroutine(task.task):
45+
logger.debug(f"Scheduling task for '{task.name}'")
46+
await task.task
4247

4348
else:
44-
logger.debug(f"Waiting until programme termination for '{name}'")
49+
logger.debug(f"Waiting until programme termination for '{task.name}'")
4550
await sleep_forever(3600)
4651

4752
except asyncio.CancelledError:
48-
logger.debug(f"Task for '{name}' cancelled")
53+
logger.debug(f"Task for '{task.name}' cancelled")
4954

50-
if task is not None:
51-
name = name or task.__name__
52-
task_wrapper = update_wrapper(task_wrapper, task) # type: ignore[arg-type]
55+
if task.task is not None:
56+
task_wrapper = update_wrapper(task_wrapper, task.task) # type: ignore[arg-type]
5357
else:
5458
task_wrapper = update_wrapper(task_wrapper, sleep_forever)
5559

56-
return create_task_fn(task_wrapper(), name=name)
60+
return create_task_fn(task_wrapper(), name=task.name)
5761

5862

5963
def _get_name(plugin_factory: PluginFactory) -> str:
@@ -72,21 +76,19 @@ async def setup_plugins(
7276
*args: Any,
7377
stack: AsyncExitStack,
7478
**kwargs: Any,
75-
) -> list[PluginTask | None]:
76-
tasks: list[PluginTask | None] = []
79+
) -> list[TaskWithName]:
80+
tasks: list[TaskWithName] = []
7781
for plugin_factory in plugin_factories:
7882
plugin_fn = plugin_factory(*args, **kwargs)
7983
name = _get_name(plugin_factory)
8084
logger.debug(f"Setting up task for '{name}'")
8185
task = await stack.enter_async_context(plugin_fn)
82-
if task is not None and name is not None:
83-
task.__name__ = name
84-
tasks.append(task)
86+
tasks.append(TaskWithName(task=task, name=name))
8587

8688
return tasks
8789

8890

89-
async def run_tasks(tasks: list[PluginTask | None]) -> None:
91+
async def run_tasks(tasks: list[TaskWithName]) -> None:
9092
try:
9193
async with asyncio.TaskGroup() as tg:
9294
plugin_task = partial(create_plugin_task, create_task_fn=tg.create_task)

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ write_to = "_version.py"
77

88
[project]
99
name = "click-async-plugins"
10-
version = "0.4.0"
10+
version = "0.5.0"
1111
authors = [
1212
{ name = "martin f. krafft", email = "[email protected]" },
1313
]

0 commit comments

Comments
 (0)