mirror of
https://github.com/crewAIInc/crewAI.git
synced 2026-01-14 10:38:29 +00:00
Fix Python 3.10 compatibility: Replace datetime.UTC with timezone.utc
- Created datetime_compat module to provide UTC constant using timezone.utc - Updated all direct UTC imports to use compatibility module - Added tests to verify UTC timezone compatibility - Fixes #2171 Co-Authored-By: Joe Moura <joao@crewai.com>
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
"""Test Agent creation and execution basic functionality."""
|
||||
|
||||
import os
|
||||
from datetime import UTC, datetime, timezone
|
||||
from datetime import datetime, timezone
|
||||
from unittest import mock
|
||||
from unittest.mock import patch
|
||||
|
||||
@@ -18,6 +18,7 @@ from crewai.tools import tool
|
||||
from crewai.tools.tool_calling import InstructorToolCalling
|
||||
from crewai.tools.tool_usage import ToolUsage
|
||||
from crewai.utilities import RPMController
|
||||
from crewai.utilities.datetime_compat import UTC
|
||||
from crewai.utilities.events import crewai_event_bus
|
||||
from crewai.utilities.events.tool_usage_events import ToolUsageFinishedEvent
|
||||
|
||||
@@ -916,9 +917,10 @@ def test_tool_result_as_answer_is_the_final_answer_for_the_agent():
|
||||
|
||||
@pytest.mark.vcr(filter_headers=["authorization"])
|
||||
def test_tool_usage_information_is_appended_to_agent():
|
||||
from datetime import UTC, datetime
|
||||
from datetime import datetime
|
||||
|
||||
from crewai.tools import BaseTool
|
||||
from crewai.utilities.datetime_compat import UTC
|
||||
|
||||
class MyCustomTool(BaseTool):
|
||||
name: str = "Decide Greetings"
|
||||
@@ -928,8 +930,9 @@ def test_tool_usage_information_is_appended_to_agent():
|
||||
return "Howdy!"
|
||||
|
||||
fixed_datetime = datetime(2025, 2, 10, 12, 0, 0, tzinfo=UTC)
|
||||
with patch("datetime.datetime") as mock_datetime:
|
||||
with patch("crewai.tools.tool_usage.datetime") as mock_datetime:
|
||||
mock_datetime.now.return_value = fixed_datetime
|
||||
mock_datetime.fromtimestamp = datetime.fromtimestamp
|
||||
mock_datetime.side_effect = lambda *args, **kw: datetime(*args, **kw)
|
||||
|
||||
agent1 = Agent(
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import os
|
||||
from datetime import UTC, datetime
|
||||
from datetime import datetime
|
||||
from unittest.mock import MagicMock, patch
|
||||
from uuid import UUID
|
||||
|
||||
@@ -21,6 +21,7 @@ from crewai.traces.unified_trace_controller import (
|
||||
trace_flow_step,
|
||||
trace_llm_call,
|
||||
)
|
||||
from crewai.utilities.datetime_compat import UTC
|
||||
|
||||
|
||||
class TestUnifiedTraceController:
|
||||
|
||||
14
tests/utilities/test_datetime_compat.py
Normal file
14
tests/utilities/test_datetime_compat.py
Normal file
@@ -0,0 +1,14 @@
|
||||
"""Test datetime compatibility module."""
|
||||
from datetime import timezone
|
||||
|
||||
from crewai.utilities.datetime_compat import UTC
|
||||
|
||||
|
||||
def test_utc_timezone_compatibility():
|
||||
"""Test that UTC timezone is compatible with both Python 3.10 and 3.11+"""
|
||||
assert UTC == timezone.utc
|
||||
assert UTC.tzname(None) == "UTC"
|
||||
# Verify it works with datetime.now()
|
||||
from datetime import datetime
|
||||
dt = datetime.now(UTC)
|
||||
assert dt.tzinfo == timezone.utc
|
||||
Reference in New Issue
Block a user