Address PR feedback: improve documentation, add deprecation warning, enhance test coverage

Co-Authored-By: Joe Moura <joao@crewai.com>
This commit is contained in:
Devin AI
2025-03-31 12:48:55 +00:00
parent d1fd44f477
commit f57fc521f8
2 changed files with 57 additions and 5 deletions

View File

@@ -1,12 +1,13 @@
import asyncio
import json
import re
import sys
import uuid
import warnings
from concurrent.futures import Future
from copy import copy as shallow_copy
from hashlib import md5
from typing import Any, Callable, Dict, List, Optional, Set, Tuple, Union, cast
from typing import Any, Callable, Dict, List, Optional, Set, Tuple, TypeAlias, Union, cast
from pydantic import (
UUID4,
@@ -1386,4 +1387,16 @@ class Crew(BaseModel):
raise RuntimeError(f"Failed to reset {name} memory") from e
Crewai = Crew
def _get_crewai():
warnings.warn(
"Crewai is deprecated, use Crew instead.",
DeprecationWarning,
stacklevel=2
)
return Crew
class _CrewaiDescriptor:
def __get__(self, obj, objtype=None):
return _get_crewai()
sys.modules[__name__].__dict__['Crewai'] = _get_crewai()

View File

@@ -1,17 +1,56 @@
import unittest
class TestCrewaiAlias(unittest.TestCase):
"""Test the Crewai alias for backward compatibility."""
"""Tests validating the Crewai alias and its backward compatibility.
These tests ensure that the Crewai alias works correctly for both
import scenarios and practical usage, providing backward compatibility
for existing code that uses the 'Crewai' name.
"""
def test_crewai_alias_import(self):
"""Test that Crewai can be imported from crewai.crew."""
try:
from crewai.crew import Crewai
from crewai.crew import Crew
from crewai.crew import Crew, Crewai
self.assertEqual(Crewai, Crew)
except ImportError:
self.fail("Failed to import Crewai from crewai.crew")
def test_crewai_instance_creation(self):
"""Ensure Crewai can be instantiated just like Crew."""
from crewai.crew import Crew, Crewai
from crewai.agent import Agent
test_agent = Agent(
role="Test Agent",
goal="Testing",
backstory="Created for testing"
)
crewai_instance = Crewai(agents=[test_agent], tasks=[])
crew_instance = Crew(agents=[test_agent], tasks=[])
self.assertIsInstance(crewai_instance, Crew)
self.assertEqual(type(crewai_instance), type(crew_instance))
def test_crewai_deprecation_warning(self):
"""Test that using Crewai emits a deprecation warning."""
import warnings
import importlib
import crewai.crew
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
importlib.reload(crewai.crew)
self.assertTrue(len(w) > 0, "No deprecation warning was captured")
self.assertTrue(any(issubclass(warning.category, DeprecationWarning) for warning in w),
"No DeprecationWarning was found")
self.assertTrue(any("Crewai is deprecated" in str(warning.message) for warning in w),
"Warning message doesn't contain expected text")
if __name__ == "__main__":
unittest.main()