From 3257d2757f4428a4abc0d069567ccb734aab5e85 Mon Sep 17 00:00:00 2001 From: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Date: Thu, 4 Sep 2025 02:17:59 +0000 Subject: [PATCH] fix: Complete deprecated typing imports replacement MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Replace typing.Type with type in all utility files - Replace typing.Dict with dict in remaining files - Replace typing.List with list in remaining files - Fix all undefined name errors from deprecated imports - Ensure compatibility with Python 3.10-3.13 type checking Co-Authored-By: João --- src/crewai/cli/run_crew.py | 2 +- src/crewai/events/event_bus.py | 8 +++---- .../tools/agent_tools/add_image_tool.py | 2 +- src/crewai/tools/base_tool.py | 8 +++---- src/crewai/utilities/config.py | 6 ++--- src/crewai/utilities/converter.py | 24 +++++++++---------- src/crewai/utilities/events/__init__.py | 6 ++--- .../utilities/pydantic_schema_parser.py | 14 +++++------ src/crewai/utilities/serialization.py | 6 ++--- 9 files changed, 37 insertions(+), 39 deletions(-) diff --git a/src/crewai/cli/run_crew.py b/src/crewai/cli/run_crew.py index 62241a4b5..582ff01d8 100644 --- a/src/crewai/cli/run_crew.py +++ b/src/crewai/cli/run_crew.py @@ -1,6 +1,6 @@ import subprocess from enum import Enum -from typing import List, Optional +from typing import Optional import click from packaging import version diff --git a/src/crewai/events/event_bus.py b/src/crewai/events/event_bus.py index 6daa63c55..fbb754def 100644 --- a/src/crewai/events/event_bus.py +++ b/src/crewai/events/event_bus.py @@ -2,7 +2,7 @@ from __future__ import annotations import threading from contextlib import contextmanager -from typing import Any, Callable, Type, TypeVar, cast +from typing import Any, Callable, TypeVar, cast from blinker import Signal @@ -32,10 +32,10 @@ class CrewAIEventsBus: def _initialize(self) -> None: """Initialize the event bus internal state""" self._signal = Signal("crewai_event_bus") - self._handlers: dict[Type[BaseEvent], list[Callable]] = {} + self._handlers: dict[type[BaseEvent], list[Callable]] = {} def on( - self, event_type: Type[EventT] + self, event_type: type[EventT] ) -> Callable[[Callable[[Any, EventT], None]], Callable[[Any, EventT], None]]: """ Decorator to register an event handler for a specific event type. @@ -82,7 +82,7 @@ class CrewAIEventsBus: self._signal.send(source, event=event) def register_handler( - self, event_type: Type[EventTypes], handler: Callable[[Any, EventTypes], None] + self, event_type: type[EventTypes], handler: Callable[[Any, EventTypes], None] ) -> None: """Register an event handler for a specific event type""" if event_type not in self._handlers: diff --git a/src/crewai/tools/agent_tools/add_image_tool.py b/src/crewai/tools/agent_tools/add_image_tool.py index 939dff2df..2109dd2ff 100644 --- a/src/crewai/tools/agent_tools/add_image_tool.py +++ b/src/crewai/tools/agent_tools/add_image_tool.py @@ -1,4 +1,4 @@ -from typing import Dict, Optional, Union +from typing import Optional, Union from pydantic import BaseModel, Field diff --git a/src/crewai/tools/base_tool.py b/src/crewai/tools/base_tool.py index ec9c6e7bd..8d3a12217 100644 --- a/src/crewai/tools/base_tool.py +++ b/src/crewai/tools/base_tool.py @@ -1,7 +1,7 @@ import asyncio from abc import ABC, abstractmethod from inspect import signature -from typing import Any, Callable, Type, get_args, get_origin, Optional +from typing import Any, Callable, get_args, get_origin, Optional from pydantic import ( BaseModel, @@ -34,7 +34,7 @@ class BaseTool(BaseModel, ABC): """Used to tell the model how/when/why to use the tool.""" env_vars: list[EnvVar] = [] """List of environment variables used by the tool.""" - args_schema: Type[PydanticBaseModel] = Field( + args_schema: type[PydanticBaseModel] = Field( default_factory=_ArgsSchemaPlaceholder, validate_default=True ) """The schema for the arguments that the tool accepts.""" @@ -52,8 +52,8 @@ class BaseTool(BaseModel, ABC): @field_validator("args_schema", mode="before") @classmethod def _default_args_schema( - cls, v: Type[PydanticBaseModel] - ) -> Type[PydanticBaseModel]: + cls, v: type[PydanticBaseModel] + ) -> type[PydanticBaseModel]: if not isinstance(v, cls._ArgsSchemaPlaceholder): return v diff --git a/src/crewai/utilities/config.py b/src/crewai/utilities/config.py index 2c8608de1..872ff6e0a 100644 --- a/src/crewai/utilities/config.py +++ b/src/crewai/utilities/config.py @@ -1,17 +1,17 @@ -from typing import Any, Type +from typing import Any from pydantic import BaseModel def process_config( - values: dict[str, Any], model_class: Type[BaseModel] + values: dict[str, Any], model_class: type[BaseModel] ) -> dict[str, Any]: """ Process the config dictionary and update the values accordingly. Args: values (dict[str, Any]): The dictionary of values to update. - model_class (Type[BaseModel]): The Pydantic model class to reference for field validation. + model_class (type[BaseModel]): The Pydantic model class to reference for field validation. Returns: dict[str, Any]: The updated values dictionary. diff --git a/src/crewai/utilities/converter.py b/src/crewai/utilities/converter.py index 1d08deef2..f1a6bc986 100644 --- a/src/crewai/utilities/converter.py +++ b/src/crewai/utilities/converter.py @@ -1,6 +1,6 @@ import json import re -from typing import Any, Optional, Type, Union, get_args, get_origin +from typing import Any, Optional, Union, get_args, get_origin from pydantic import BaseModel, ValidationError @@ -116,10 +116,10 @@ class Converter(OutputConverter): def convert_to_model( result: str, - output_pydantic: Optional[Type[BaseModel]], - output_json: Optional[Type[BaseModel]], + output_pydantic: Optional[type[BaseModel]], + output_json: Optional[type[BaseModel]], agent: Any, - converter_cls: Optional[Type[Converter]] = None, + converter_cls: Optional[type[Converter]] = None, ) -> Union[dict, BaseModel, str]: model = output_pydantic or output_json if model is None: @@ -146,7 +146,7 @@ def convert_to_model( def validate_model( - result: str, model: Type[BaseModel], is_json_output: bool + result: str, model: type[BaseModel], is_json_output: bool ) -> Union[dict, BaseModel]: exported_result = model.model_validate_json(result) if is_json_output: @@ -156,10 +156,10 @@ def validate_model( def handle_partial_json( result: str, - model: Type[BaseModel], + model: type[BaseModel], is_json_output: bool, agent: Any, - converter_cls: Optional[Type[Converter]] = None, + converter_cls: Optional[type[Converter]] = None, ) -> Union[dict, BaseModel, str]: match = re.search(r"({.*})", result, re.DOTALL) if match: @@ -185,10 +185,10 @@ def handle_partial_json( def convert_with_instructions( result: str, - model: Type[BaseModel], + model: type[BaseModel], is_json_output: bool, agent: Any, - converter_cls: Optional[Type[Converter]] = None, + converter_cls: Optional[type[Converter]] = None, ) -> Union[dict, BaseModel, str]: llm = agent.function_calling_llm or agent.llm instructions = get_conversion_instructions(model, llm) @@ -214,7 +214,7 @@ def convert_with_instructions( return exported_result -def get_conversion_instructions(model: Type[BaseModel], llm: Any) -> str: +def get_conversion_instructions(model: type[BaseModel], llm: Any) -> str: instructions = "Please convert the following text into valid JSON." if llm and not isinstance(llm, str) and llm.supports_function_calling(): model_schema = PydanticSchemaParser(model=model).get_schema() @@ -233,7 +233,7 @@ def get_conversion_instructions(model: Type[BaseModel], llm: Any) -> str: def create_converter( agent: Optional[Any] = None, - converter_cls: Optional[Type[Converter]] = None, + converter_cls: Optional[type[Converter]] = None, *args, **kwargs, ) -> Converter: @@ -253,7 +253,7 @@ def create_converter( return converter -def generate_model_description(model: Type[BaseModel]) -> str: +def generate_model_description(model: type[BaseModel]) -> str: """ Generate a string description of a Pydantic model's fields and their types. diff --git a/src/crewai/utilities/events/__init__.py b/src/crewai/utilities/events/__init__.py index 24184086a..2a0c4895c 100644 --- a/src/crewai/utilities/events/__init__.py +++ b/src/crewai/utilities/events/__init__.py @@ -3,7 +3,7 @@ import warnings from abc import ABC from collections.abc import Callable -from typing import Any, Type, TypeVar +from typing import Any, TypeVar from typing_extensions import deprecated import crewai.events as new_events @@ -32,7 +32,7 @@ class crewai_event_bus: # noqa: N801 @classmethod def on( - cls, event_type: Type[EventT] + cls, event_type: type[EventT] ) -> Callable[[Callable[[Any, EventT], None]], Callable[[Any, EventT], None]]: """Delegate to the actual event bus instance.""" return new_events.crewai_event_bus.on(event_type) @@ -44,7 +44,7 @@ class crewai_event_bus: # noqa: N801 @classmethod def register_handler( - cls, event_type: Type[EventTypes], handler: Callable[[Any, EventTypes], None] + cls, event_type: type[EventTypes], handler: Callable[[Any, EventTypes], None] ) -> None: """Delegate to the actual event bus instance.""" return new_events.crewai_event_bus.register_handler(event_type, handler) diff --git a/src/crewai/utilities/pydantic_schema_parser.py b/src/crewai/utilities/pydantic_schema_parser.py index 2b3457eaa..ee43c98f2 100644 --- a/src/crewai/utilities/pydantic_schema_parser.py +++ b/src/crewai/utilities/pydantic_schema_parser.py @@ -1,10 +1,10 @@ -from typing import Dict, List, Type, Union, get_args, get_origin +from typing import Union, get_args, get_origin from pydantic import BaseModel class PydanticSchemaParser(BaseModel): - model: Type[BaseModel] + model: type[BaseModel] def get_schema(self) -> str: """ @@ -14,7 +14,7 @@ class PydanticSchemaParser(BaseModel): """ return "{\n" + self._get_model_schema(self.model) + "\n}" - def _get_model_schema(self, model: Type[BaseModel], depth: int = 0) -> str: + def _get_model_schema(self, model: type[BaseModel], depth: int = 0) -> str: indent = " " * 4 * depth lines = [ f"{indent} {field_name}: {self._get_field_type(field, depth + 1)}" @@ -26,11 +26,11 @@ class PydanticSchemaParser(BaseModel): field_type = field.annotation origin = get_origin(field_type) - if origin in {list, List}: + if origin is list: list_item_type = get_args(field_type)[0] return self._format_list_type(list_item_type, depth) - if origin in {dict, Dict}: + if origin is dict: key_type, value_type = get_args(field_type) return f"dict[{key_type.__name__}, {value_type.__name__}]" @@ -77,10 +77,10 @@ class PydanticSchemaParser(BaseModel): def _get_field_type_for_annotation(self, annotation, depth: int) -> str: origin = get_origin(annotation) - if origin in {list, List}: + if origin is list: list_item_type = get_args(annotation)[0] return self._format_list_type(list_item_type, depth) - if origin in {dict, Dict}: + if origin is dict: key_type, value_type = get_args(annotation) return f"dict[{key_type.__name__}, {value_type.__name__}]" if origin is Union: diff --git a/src/crewai/utilities/serialization.py b/src/crewai/utilities/serialization.py index b45970968..21d57cfb5 100644 --- a/src/crewai/utilities/serialization.py +++ b/src/crewai/utilities/serialization.py @@ -5,10 +5,8 @@ from typing import Any, Union from pydantic import BaseModel -SerializablePrimitive = Union[str, int, float, bool, None] -Serializable = Union[ - SerializablePrimitive, list["Serializable"], dict[str, "Serializable"] -] +SerializablePrimitive = str | int | float | bool | None +Serializable = SerializablePrimitive | list["Serializable"] | dict[str, "Serializable"] def to_serializable(