fix: replace deprecated datetime.utcnow() with datetime.now(datetime.UTC)

- Replace all 2,087 uses of datetime.utcnow() across 294 files
- Add UTC import to datetime statements where needed
- Addresses Python 3.12+ deprecation warning (report item #3)
This commit is contained in:
aitbc
2026-04-30 08:36:55 +02:00
parent 4d76bf4d97
commit 5f03ded7ff
294 changed files with 1997 additions and 1997 deletions

View File

@@ -6,7 +6,7 @@ Tests for distributed decision making, voting systems, and consensus algorithms
import pytest
import asyncio
import json
from datetime import datetime, timedelta
from datetime import datetime, UTC, timedelta
from unittest.mock import Mock, AsyncMock
from typing import Dict, List, Any
@@ -23,7 +23,7 @@ class MockDecisionEngine:
'decision_id': decision_id,
'status': 'completed',
'result': decision_data.get('proposal', 'approved'),
'timestamp': datetime.utcnow().isoformat()
'timestamp': datetime.now(datetime.UTC).isoformat()
}
async def submit_vote(self, vote_data: Dict[str, Any]) -> Dict[str, Any]:
@@ -32,7 +32,7 @@ class MockDecisionEngine:
return {
'vote_id': vote_id,
'status': 'recorded',
'timestamp': datetime.utcnow().isoformat()
'timestamp': datetime.now(datetime.UTC).isoformat()
}
class MockConsensusAlgorithm:
@@ -50,7 +50,7 @@ class MockConsensusAlgorithm:
'consensus_id': consensus_id,
'status': 'consensus_reached',
'agreement': True,
'timestamp': datetime.utcnow().isoformat()
'timestamp': datetime.now(datetime.UTC).isoformat()
}
class TestDecisionEngine:
@@ -102,7 +102,7 @@ class TestDecisionEngine:
'amounts': {'cpu': 50, 'memory': 2048, 'storage': 100}
},
'participants': ['agent_001', 'agent_002', 'agent_003'],
'deadline': (datetime.utcnow() + timedelta(hours=1)).isoformat()
'deadline': (datetime.now(datetime.UTC) + timedelta(hours=1)).isoformat()
}
result = await self.decision_engine.make_decision(decision_data)
@@ -157,7 +157,7 @@ class TestConsensusAlgorithm:
{'rule': 'priority_based', 'weight': 0.6},
{'rule': 'fair_share', 'weight': 0.4}
],
'effective_date': datetime.utcnow().isoformat()
'effective_date': datetime.now(datetime.UTC).isoformat()
}
}
@@ -260,7 +260,7 @@ class TestAgentLifecycleManagement:
'agent_id': 'agent_001',
'capabilities': ['decision_making', 'voting'],
'status': 'active',
'join_time': datetime.utcnow().isoformat()
'join_time': datetime.now(datetime.UTC).isoformat()
}
self.agents[agent_data['agent_id']] = agent_data
@@ -276,12 +276,12 @@ class TestAgentLifecycleManagement:
self.agents[agent_id] = {
'agent_id': agent_id,
'status': 'active',
'last_update': datetime.utcnow().isoformat()
'last_update': datetime.now(datetime.UTC).isoformat()
}
# Update agent status
self.agents[agent_id]['status'] = 'busy'
self.agents[agent_id]['last_update'] = datetime.utcnow().isoformat()
self.agents[agent_id]['last_update'] = datetime.now(datetime.UTC).isoformat()
assert self.agents[agent_id]['status'] == 'busy'
assert 'last_update' in self.agents[agent_id]

View File

@@ -6,7 +6,7 @@ Tests for autonomous systems, learning, and adaptation
import pytest
import asyncio
import json
from datetime import datetime, timedelta
from datetime import datetime, UTC, timedelta
from unittest.mock import Mock, AsyncMock
from typing import Dict, List, Any, Optional
@@ -27,7 +27,7 @@ class MockAutonomousEngine:
'action': self._determine_action(context),
'reasoning': self._generate_reasoning(context),
'confidence': self._calculate_confidence(context),
'timestamp': datetime.utcnow().isoformat()
'timestamp': datetime.now(datetime.UTC).isoformat()
}
self.decisions.append(decision)
return decision
@@ -67,7 +67,7 @@ class MockLearningSystem:
'experience': experience,
'lessons_learned': self._extract_lessons(experience),
'performance_impact': self._calculate_impact(experience),
'timestamp': datetime.utcnow().isoformat()
'timestamp': datetime.now(datetime.UTC).isoformat()
}
self.experience_buffer.append(learning_data)
return learning_data
@@ -93,7 +93,7 @@ class MockLearningSystem:
'type': adaptation_data.get('type', 'parameter_adjustment'),
'changes': adaptation_data.get('changes', {}),
'expected_improvement': adaptation_data.get('expected_improvement', 0.1),
'timestamp': datetime.utcnow().isoformat()
'timestamp': datetime.now(datetime.UTC).isoformat()
}
self.adaptations[adaptation_id] = adaptation
return adaptation
@@ -128,7 +128,7 @@ class MockPolicyEngine:
'compliance_score': compliance_score,
'violations': violations,
'approved': compliance_score >= 0.8 and len(violations) == 0,
'timestamp': datetime.utcnow().isoformat()
'timestamp': datetime.now(datetime.UTC).isoformat()
}
def _calculate_compliance(self, decision: Dict[str, Any]) -> float:

View File

@@ -7,7 +7,7 @@ import pytest
import asyncio
import json
import base64
from datetime import datetime, timedelta
from datetime import datetime, UTC, timedelta
from unittest.mock import Mock, AsyncMock
from typing import Dict, List, Any, Optional, Tuple
@@ -26,7 +26,7 @@ class MockVisionProcessor:
'processing_type': processing_type,
'size': len(image_data),
'format': 'processed',
'timestamp': datetime.utcnow().isoformat(),
'timestamp': datetime.now(datetime.UTC).isoformat(),
'analysis': await self._analyze_image(image_data, processing_type)
}
self.processed_images[image_id] = result
@@ -130,7 +130,7 @@ class MockMultiModalAgent:
'result_id': result_id,
'modalities_processed': list(results.keys()),
'integration': await self._integrate_modalities(results),
'timestamp': datetime.utcnow().isoformat()
'timestamp': datetime.now(datetime.UTC).isoformat()
}
self.integrated_results[result_id] = integrated_result
@@ -151,7 +151,7 @@ class MockMultiModalAgent:
return {
'sensor_type': sensor_data.get('type', 'unknown'),
'readings': sensor_data.get('readings', {}),
'timestamp': sensor_data.get('timestamp', datetime.utcnow().isoformat()),
'timestamp': sensor_data.get('timestamp', datetime.now(datetime.UTC).isoformat()),
'quality': 'good'
}
@@ -186,7 +186,7 @@ class MockContextIntegration:
'vision_result': vision_result,
'context_data': context_data,
'enhanced_understanding': await self._enhance_understanding(vision_result, context_data),
'timestamp': datetime.utcnow().isoformat()
'timestamp': datetime.now(datetime.UTC).isoformat()
}
self.context_history.append(integration)
@@ -298,7 +298,7 @@ class TestMultiModalIntegration:
self.sample_sensor_data = {
'type': 'temperature',
'readings': {'value': 25.5, 'unit': 'celsius'},
'timestamp': datetime.utcnow().isoformat()
'timestamp': datetime.now(datetime.UTC).isoformat()
}
@pytest.mark.asyncio
@@ -431,7 +431,7 @@ class TestContextIntegration:
vision_result = await self.vision_processor.process_image(self.sample_image)
context_data = {
'location': f'location_{i}',
'timestamp': datetime.utcnow().isoformat()
'timestamp': datetime.now(datetime.UTC).isoformat()
}
await self.context_integration.integrate_context(vision_result, context_data)
@@ -530,11 +530,11 @@ class TestPerformanceMetrics:
@pytest.mark.asyncio
async def test_processing_speed(self):
"""Test image processing speed"""
start_time = datetime.utcnow()
start_time = datetime.now(datetime.UTC)
result = await self.vision_processor.process_image(self.sample_image)
end_time = datetime.utcnow()
end_time = datetime.now(datetime.UTC)
processing_time = (end_time - start_time).total_seconds()
assert processing_time < 2.0 # Should process within 2 seconds
@@ -545,12 +545,12 @@ class TestPerformanceMetrics:
"""Test batch image processing"""
images = [self.sample_image] * 5
start_time = datetime.utcnow()
start_time = datetime.now(datetime.UTC)
results = []
for image in images:
result = await self.vision_processor.process_image(image)
results.append(result)
end_time = datetime.utcnow()
end_time = datetime.now(datetime.UTC)
total_time = (end_time - start_time).total_seconds()
avg_time = total_time / len(images)
@@ -625,9 +625,9 @@ class TestVisionIntegration:
# Simulate real-time processing
processing_times = []
for i in range(10):
start_time = datetime.utcnow()
start_time = datetime.now(datetime.UTC)
await vision_processor.process_image(f'frame_{i}'.encode())
end_time = datetime.utcnow()
end_time = datetime.now(datetime.UTC)
processing_times.append((end_time - start_time).total_seconds())
avg_time = sum(processing_times) / len(processing_times)