Fix pyright cr_code access on Coroutine in BaseObject.create_task
`collections.abc.Coroutine` doesn't expose `cr_code`/`co_name`; only native coroutine objects do. Use `getattr` chains so pyright is happy and any non-native awaitable falls back to a generic task name instead of crashing.
This commit is contained in:
@@ -133,11 +133,12 @@ class BaseObject(ABC):
|
|||||||
Returns:
|
Returns:
|
||||||
The created asyncio task.
|
The created asyncio task.
|
||||||
"""
|
"""
|
||||||
if name:
|
if not name:
|
||||||
name = f"{self}::{name}"
|
# Native coroutines expose ``cr_code``; fall back to a generic
|
||||||
else:
|
# name for any other awaitable subtype.
|
||||||
name = f"{self}::{coroutine.cr_code.co_name}"
|
cr_code = getattr(coroutine, "cr_code", None)
|
||||||
return self.task_manager.create_task(coroutine, name)
|
name = getattr(cr_code, "co_name", "task")
|
||||||
|
return self.task_manager.create_task(coroutine, f"{self}::{name}")
|
||||||
|
|
||||||
async def cancel_task(self, task: asyncio.Task, timeout: float | None = 1.0):
|
async def cancel_task(self, task: asyncio.Task, timeout: float | None = 1.0):
|
||||||
"""Cancel a task managed by this object's task manager.
|
"""Cancel a task managed by this object's task manager.
|
||||||
|
|||||||
Reference in New Issue
Block a user