chore: refactor type hints for Agent and Task in annotations and wrappers

This commit is contained in:
Greyson Lalonde
2025-10-13 15:35:39 -04:00
parent 6492852a0c
commit e029de2863
2 changed files with 14 additions and 23 deletions

View File

@@ -2,13 +2,14 @@
from collections.abc import Callable
from functools import wraps
from typing import Concatenate, ParamSpec, TypeVar
from typing import TYPE_CHECKING, Concatenate, ParamSpec, TypeVar
from crewai import Crew
from crewai.project.utils import memoize
if TYPE_CHECKING:
from crewai import Agent, Crew, Task
from crewai.project.wrappers import (
AfterKickoffMethod,
AgentInstance,
AgentMethod,
BeforeKickoffMethod,
CacheHandlerMethod,
@@ -17,7 +18,6 @@ from crewai.project.wrappers import (
LLMMethod,
OutputJsonClass,
OutputPydanticClass,
TaskInstance,
TaskMethod,
TaskResultT,
ToolMethod,
@@ -174,8 +174,8 @@ def crew(
Returns:
The configured Crew instance with callbacks attached.
"""
instantiated_tasks: list[TaskInstance] = []
instantiated_agents: list[AgentInstance] = []
instantiated_tasks: list[Task] = []
instantiated_agents: list[Agent] = []
agent_roles: set[str] = set()
# Use the preserved task and agent information

View File

@@ -2,7 +2,10 @@
from collections.abc import Callable
from functools import wraps
from typing import Any, Generic, ParamSpec, Protocol, Self, TypeVar
from typing import TYPE_CHECKING, Any, Generic, ParamSpec, Protocol, Self, TypeVar
if TYPE_CHECKING:
from crewai import Agent, Task
P = ParamSpec("P")
R = TypeVar("R")
@@ -18,27 +21,15 @@ class TaskResult(Protocol):
TaskResultT = TypeVar("TaskResultT", bound=TaskResult)
class AgentInstance(Protocol):
"""Protocol for agent instances."""
role: str
class TaskInstance(Protocol):
"""Protocol for task instances."""
agent: AgentInstance | None
class CrewInstance(Protocol):
"""Protocol for crew class instances with required attributes."""
_original_tasks: dict[str, Callable[[Self], TaskInstance]]
_original_agents: dict[str, Callable[[Self], AgentInstance]]
_original_tasks: dict[str, Callable[[Self], Task]]
_original_agents: dict[str, Callable[[Self], Agent]]
_before_kickoff: dict[str, Callable[..., Any]]
_after_kickoff: dict[str, Callable[..., Any]]
agents: list[AgentInstance]
tasks: list[TaskInstance]
agents: list[Agent]
tasks: list[Task]
class DecoratedMethod(Generic[P, R]):