sanzgiri commited on
Commit
85de883
·
1 Parent(s): a584f85

Fix datetime deprecation warnings

Browse files

- Replace datetime.utcnow() with datetime.now(timezone.utc)
- Add timezone import to all affected files
- All 156 tests passing
- Warnings reduced from 78 to 9 (remaining are SQLite thread safety warnings)

data/feedback.jsonl CHANGED
@@ -1 +1,5 @@
1
  {"query_text": "test query", "semantic_plan": {"intent": "find_contracts", "filters": [], "projections": ["contract_identifier"], "aggregations": [], "group_by": null, "order_by": null, "limit": 10}, "feedback_type": "good", "feedback_text": "Works great!", "correct_result": null, "timestamp": "2025-11-07T21:26:46.679147"}
 
 
 
 
 
1
  {"query_text": "test query", "semantic_plan": {"intent": "find_contracts", "filters": [], "projections": ["contract_identifier"], "aggregations": [], "group_by": null, "order_by": null, "limit": 10}, "feedback_type": "good", "feedback_text": "Works great!", "correct_result": null, "timestamp": "2025-11-07T21:26:46.679147"}
2
+ {"query_text": "test query", "semantic_plan": {"intent": "find_contracts", "filters": [], "projections": ["contract_identifier"], "aggregations": [], "group_by": null, "order_by": null, "limit": 10}, "feedback_type": "good", "feedback_text": "Works great!", "correct_result": null, "timestamp": "2025-11-07T21:30:28.042424"}
3
+ {"query_text": "test query", "semantic_plan": {"intent": "find_contracts", "filters": [], "projections": ["contract_identifier"], "aggregations": [], "group_by": null, "order_by": null, "limit": 10}, "feedback_type": "good", "feedback_text": "Works great!", "correct_result": null, "timestamp": "2025-11-08T00:57:50.346582"}
4
+ {"query_text": "test query", "semantic_plan": {"intent": "find_contracts", "filters": [], "projections": ["contract_identifier"], "aggregations": [], "group_by": null, "order_by": null, "limit": 10}, "feedback_type": "good", "feedback_text": "Works great!", "correct_result": null, "timestamp": "2025-11-08T01:18:35.490463Z"}
5
+ {"query_text": "test query", "semantic_plan": {"intent": "find_contracts", "filters": [], "projections": ["contract_identifier"], "aggregations": [], "group_by": null, "order_by": null, "limit": 10}, "feedback_type": "good", "feedback_text": "Works great!", "correct_result": null, "timestamp": "2025-11-08T01:19:25.274999Z"}
schema_translator/feedback_loop.py CHANGED
@@ -6,7 +6,7 @@ and schema mapping over time.
6
  """
7
 
8
  from typing import List, Dict, Any, Optional, Tuple
9
- from datetime import datetime, timedelta
10
  from collections import defaultdict, Counter
11
  import json
12
  import logging
@@ -99,7 +99,7 @@ class FeedbackLoop:
99
  Returns:
100
  Summary statistics
101
  """
102
- cutoff_date = datetime.utcnow() - timedelta(days=days)
103
  recent_feedback = [
104
  f for f in self.feedback_cache
105
  if f.timestamp >= cutoff_date
@@ -352,7 +352,7 @@ class FeedbackLoop:
352
  feedback_to_export = self.feedback_cache
353
 
354
  if days:
355
- cutoff_date = datetime.utcnow() - timedelta(days=days)
356
  feedback_to_export = [
357
  f for f in feedback_to_export
358
  if f.timestamp >= cutoff_date
@@ -377,7 +377,7 @@ class FeedbackLoop:
377
  Returns:
378
  Number of entries removed
379
  """
380
- cutoff_date = datetime.utcnow() - timedelta(days=days)
381
 
382
  old_count = len(self.feedback_cache)
383
  self.feedback_cache = [
@@ -423,7 +423,7 @@ class FeedbackLoop:
423
 
424
  type_counts = Counter(f.feedback_type for f in self.feedback_cache)
425
 
426
- now = datetime.utcnow()
427
  ages = [(now - f.timestamp).days for f in self.feedback_cache]
428
 
429
  return {
 
6
  """
7
 
8
  from typing import List, Dict, Any, Optional, Tuple
9
+ from datetime import datetime, timedelta, timezone
10
  from collections import defaultdict, Counter
11
  import json
12
  import logging
 
99
  Returns:
100
  Summary statistics
101
  """
102
+ cutoff_date = datetime.now(timezone.utc) - timedelta(days=days)
103
  recent_feedback = [
104
  f for f in self.feedback_cache
105
  if f.timestamp >= cutoff_date
 
352
  feedback_to_export = self.feedback_cache
353
 
354
  if days:
355
+ cutoff_date = datetime.now(timezone.utc) - timedelta(days=days)
356
  feedback_to_export = [
357
  f for f in feedback_to_export
358
  if f.timestamp >= cutoff_date
 
377
  Returns:
378
  Number of entries removed
379
  """
380
+ cutoff_date = datetime.now(timezone.utc) - timedelta(days=days)
381
 
382
  old_count = len(self.feedback_cache)
383
  self.feedback_cache = [
 
423
 
424
  type_counts = Counter(f.feedback_type for f in self.feedback_cache)
425
 
426
+ now = datetime.now(timezone.utc)
427
  ages = [(now - f.timestamp).days for f in self.feedback_cache]
428
 
429
  return {
schema_translator/models.py CHANGED
@@ -1,6 +1,6 @@
1
  """Data models for Schema Translator using Pydantic."""
2
 
3
- from datetime import datetime
4
  from enum import Enum
5
  from typing import Any, Dict, List, Optional
6
 
@@ -238,7 +238,7 @@ class QueryFeedback(BaseModel):
238
  feedback_type: str = Field(..., description="Type of feedback (incorrect, missing, good)")
239
  feedback_text: Optional[str] = Field(None, description="User's feedback comment")
240
  correct_result: Optional[Any] = Field(None, description="What the correct result should be")
241
- timestamp: datetime = Field(default_factory=datetime.utcnow, description="When feedback was given")
242
 
243
 
244
  class SchemaChange(BaseModel):
@@ -249,5 +249,5 @@ class SchemaChange(BaseModel):
249
  column_name: Optional[str] = Field(None, description="Affected column")
250
  old_value: Optional[Any] = Field(None, description="Previous value")
251
  new_value: Optional[Any] = Field(None, description="New value")
252
- detected_at: datetime = Field(default_factory=datetime.utcnow, description="When change was detected")
253
  requires_remapping: bool = Field(default=False, description="Whether concept mappings need update")
 
1
  """Data models for Schema Translator using Pydantic."""
2
 
3
+ from datetime import datetime, timezone
4
  from enum import Enum
5
  from typing import Any, Dict, List, Optional
6
 
 
238
  feedback_type: str = Field(..., description="Type of feedback (incorrect, missing, good)")
239
  feedback_text: Optional[str] = Field(None, description="User's feedback comment")
240
  correct_result: Optional[Any] = Field(None, description="What the correct result should be")
241
+ timestamp: datetime = Field(default_factory=lambda: datetime.now(timezone.utc), description="When feedback was given")
242
 
243
 
244
  class SchemaChange(BaseModel):
 
249
  column_name: Optional[str] = Field(None, description="Affected column")
250
  old_value: Optional[Any] = Field(None, description="Previous value")
251
  new_value: Optional[Any] = Field(None, description="New value")
252
+ detected_at: datetime = Field(default_factory=lambda: datetime.now(timezone.utc), description="When change was detected")
253
  requires_remapping: bool = Field(default=False, description="Whether concept mappings need update")
schema_translator/orchestrator.py CHANGED
@@ -2,7 +2,7 @@
2
 
3
  import logging
4
  import time
5
- from datetime import datetime
6
  from typing import Dict, List, Optional, Any
7
 
8
  from schema_translator.agents import QueryUnderstandingAgent, SchemaAnalyzerAgent
@@ -52,7 +52,7 @@ class QueryHistory:
52
  error: Error message if query failed
53
  """
54
  self.queries.append({
55
- "timestamp": datetime.utcnow(),
56
  "query_text": query_text,
57
  "semantic_plan": semantic_plan,
58
  "result": result,
@@ -601,5 +601,5 @@ class ChatOrchestrator:
601
  "query_statistics": query_stats,
602
  "feedback_insights": feedback_insights,
603
  "drift_summary": drift_summary,
604
- "timestamp": datetime.utcnow().isoformat()
605
  }
 
2
 
3
  import logging
4
  import time
5
+ from datetime import datetime, timezone
6
  from typing import Dict, List, Optional, Any
7
 
8
  from schema_translator.agents import QueryUnderstandingAgent, SchemaAnalyzerAgent
 
52
  error: Error message if query failed
53
  """
54
  self.queries.append({
55
+ "timestamp": datetime.now(timezone.utc),
56
  "query_text": query_text,
57
  "semantic_plan": semantic_plan,
58
  "result": result,
 
601
  "query_statistics": query_stats,
602
  "feedback_insights": feedback_insights,
603
  "drift_summary": drift_summary,
604
+ "timestamp": datetime.now(timezone.utc).isoformat()
605
  }
schema_translator/schema_drift_detector.py CHANGED
@@ -6,7 +6,7 @@ affect query execution and mappings.
6
  """
7
 
8
  from typing import List, Dict, Any, Optional, Set, Tuple
9
- from datetime import datetime
10
  from pathlib import Path
11
  import sqlite3
12
  import json
@@ -87,7 +87,7 @@ class SchemaDrift:
87
  self.severity = severity
88
  self.description = description
89
  self.details = details
90
- self.detected_at = datetime.utcnow()
91
 
92
  def to_dict(self) -> Dict[str, Any]:
93
  """Convert to dictionary."""
@@ -171,7 +171,7 @@ class SchemaDriftDetector:
171
 
172
  snapshot = SchemaSnapshot(
173
  customer_id=customer_id,
174
- timestamp=datetime.utcnow(),
175
  tables=tables,
176
  row_counts=row_counts
177
  )
 
6
  """
7
 
8
  from typing import List, Dict, Any, Optional, Set, Tuple
9
+ from datetime import datetime, timezone
10
  from pathlib import Path
11
  import sqlite3
12
  import json
 
87
  self.severity = severity
88
  self.description = description
89
  self.details = details
90
+ self.detected_at = datetime.now(timezone.utc)
91
 
92
  def to_dict(self) -> Dict[str, Any]:
93
  """Convert to dictionary."""
 
171
 
172
  snapshot = SchemaSnapshot(
173
  customer_id=customer_id,
174
+ timestamp=datetime.now(timezone.utc),
175
  tables=tables,
176
  row_counts=row_counts
177
  )
tests/test_learning.py CHANGED
@@ -6,7 +6,7 @@ Tests for FeedbackLoop and SchemaDriftDetector components.
6
 
7
  import pytest
8
  from pathlib import Path
9
- from datetime import datetime, timedelta
10
  from unittest.mock import Mock, patch
11
 
12
  from schema_translator.feedback_loop import FeedbackLoop
@@ -178,7 +178,7 @@ class TestFeedbackLoop:
178
  loop.submit_feedback("query1", plan, "good")
179
 
180
  # Manually set old timestamp
181
- loop.feedback_cache[0].timestamp = datetime.utcnow() - timedelta(days=100)
182
 
183
  removed = loop.clear_old_feedback(days=90)
184
 
@@ -193,7 +193,7 @@ class TestSchemaSnapshot:
193
  """Test creating a schema snapshot."""
194
  snapshot = SchemaSnapshot(
195
  customer_id="test_customer",
196
- timestamp=datetime.utcnow(),
197
  tables={"contracts": ["id", "value", "status"]},
198
  row_counts={"contracts": 10}
199
  )
@@ -206,7 +206,7 @@ class TestSchemaSnapshot:
206
  """Test snapshot to/from dict."""
207
  snapshot1 = SchemaSnapshot(
208
  customer_id="test",
209
- timestamp=datetime.utcnow(),
210
  tables={"t1": ["c1", "c2"]},
211
  row_counts={"t1": 5}
212
  )
@@ -264,7 +264,7 @@ class TestSchemaDriftDetector:
264
  # Create old snapshot
265
  old_snapshot = SchemaSnapshot(
266
  customer_id="test",
267
- timestamp=datetime.utcnow(),
268
  tables={"table1": ["col1"]},
269
  row_counts={"table1": 10}
270
  )
@@ -272,7 +272,7 @@ class TestSchemaDriftDetector:
272
  # Create new snapshot with added table
273
  new_snapshot = SchemaSnapshot(
274
  customer_id="test",
275
- timestamp=datetime.utcnow(),
276
  tables={
277
  "table1": ["col1"],
278
  "table2": ["col2", "col3"]
@@ -290,14 +290,14 @@ class TestSchemaDriftDetector:
290
  """Test detecting removed table."""
291
  old_snapshot = SchemaSnapshot(
292
  customer_id="test",
293
- timestamp=datetime.utcnow(),
294
  tables={"table1": ["col1"], "table2": ["col2"]},
295
  row_counts={"table1": 10, "table2": 5}
296
  )
297
 
298
  new_snapshot = SchemaSnapshot(
299
  customer_id="test",
300
- timestamp=datetime.utcnow(),
301
  tables={"table1": ["col1"]},
302
  row_counts={"table1": 10}
303
  )
@@ -312,14 +312,14 @@ class TestSchemaDriftDetector:
312
  """Test detecting added columns."""
313
  old_snapshot = SchemaSnapshot(
314
  customer_id="test",
315
- timestamp=datetime.utcnow(),
316
  tables={"table1": ["col1"]},
317
  row_counts={"table1": 10}
318
  )
319
 
320
  new_snapshot = SchemaSnapshot(
321
  customer_id="test",
322
- timestamp=datetime.utcnow(),
323
  tables={"table1": ["col1", "col2", "col3"]},
324
  row_counts={"table1": 10}
325
  )
@@ -334,14 +334,14 @@ class TestSchemaDriftDetector:
334
  """Test detecting removed columns."""
335
  old_snapshot = SchemaSnapshot(
336
  customer_id="test",
337
- timestamp=datetime.utcnow(),
338
  tables={"table1": ["col1", "col2", "col3"]},
339
  row_counts={"table1": 10}
340
  )
341
 
342
  new_snapshot = SchemaSnapshot(
343
  customer_id="test",
344
- timestamp=datetime.utcnow(),
345
  tables={"table1": ["col1"]},
346
  row_counts={"table1": 10}
347
  )
@@ -356,14 +356,14 @@ class TestSchemaDriftDetector:
356
  """Test detecting significant row count changes."""
357
  old_snapshot = SchemaSnapshot(
358
  customer_id="test",
359
- timestamp=datetime.utcnow(),
360
  tables={"table1": ["col1"]},
361
  row_counts={"table1": 100}
362
  )
363
 
364
  new_snapshot = SchemaSnapshot(
365
  customer_id="test",
366
- timestamp=datetime.utcnow(),
367
  tables={"table1": ["col1"]},
368
  row_counts={"table1": 10} # 90% decrease
369
  )
@@ -378,7 +378,7 @@ class TestSchemaDriftDetector:
378
  """Test when no drift exists."""
379
  snapshot = SchemaSnapshot(
380
  customer_id="test",
381
- timestamp=datetime.utcnow(),
382
  tables={"table1": ["col1"]},
383
  row_counts={"table1": 10}
384
  )
@@ -391,7 +391,7 @@ class TestSchemaDriftDetector:
391
  """Test snapshot persistence."""
392
  snapshot = SchemaSnapshot(
393
  customer_id="test",
394
- timestamp=datetime.utcnow(),
395
  tables={"table1": ["col1"]},
396
  row_counts={"table1": 10}
397
  )
 
6
 
7
  import pytest
8
  from pathlib import Path
9
+ from datetime import datetime, timedelta, timezone
10
  from unittest.mock import Mock, patch
11
 
12
  from schema_translator.feedback_loop import FeedbackLoop
 
178
  loop.submit_feedback("query1", plan, "good")
179
 
180
  # Manually set old timestamp
181
+ loop.feedback_cache[0].timestamp = datetime.now(timezone.utc) - timedelta(days=100)
182
 
183
  removed = loop.clear_old_feedback(days=90)
184
 
 
193
  """Test creating a schema snapshot."""
194
  snapshot = SchemaSnapshot(
195
  customer_id="test_customer",
196
+ timestamp=datetime.now(timezone.utc),
197
  tables={"contracts": ["id", "value", "status"]},
198
  row_counts={"contracts": 10}
199
  )
 
206
  """Test snapshot to/from dict."""
207
  snapshot1 = SchemaSnapshot(
208
  customer_id="test",
209
+ timestamp=datetime.now(timezone.utc),
210
  tables={"t1": ["c1", "c2"]},
211
  row_counts={"t1": 5}
212
  )
 
264
  # Create old snapshot
265
  old_snapshot = SchemaSnapshot(
266
  customer_id="test",
267
+ timestamp=datetime.now(timezone.utc),
268
  tables={"table1": ["col1"]},
269
  row_counts={"table1": 10}
270
  )
 
272
  # Create new snapshot with added table
273
  new_snapshot = SchemaSnapshot(
274
  customer_id="test",
275
+ timestamp=datetime.now(timezone.utc),
276
  tables={
277
  "table1": ["col1"],
278
  "table2": ["col2", "col3"]
 
290
  """Test detecting removed table."""
291
  old_snapshot = SchemaSnapshot(
292
  customer_id="test",
293
+ timestamp=datetime.now(timezone.utc),
294
  tables={"table1": ["col1"], "table2": ["col2"]},
295
  row_counts={"table1": 10, "table2": 5}
296
  )
297
 
298
  new_snapshot = SchemaSnapshot(
299
  customer_id="test",
300
+ timestamp=datetime.now(timezone.utc),
301
  tables={"table1": ["col1"]},
302
  row_counts={"table1": 10}
303
  )
 
312
  """Test detecting added columns."""
313
  old_snapshot = SchemaSnapshot(
314
  customer_id="test",
315
+ timestamp=datetime.now(timezone.utc),
316
  tables={"table1": ["col1"]},
317
  row_counts={"table1": 10}
318
  )
319
 
320
  new_snapshot = SchemaSnapshot(
321
  customer_id="test",
322
+ timestamp=datetime.now(timezone.utc),
323
  tables={"table1": ["col1", "col2", "col3"]},
324
  row_counts={"table1": 10}
325
  )
 
334
  """Test detecting removed columns."""
335
  old_snapshot = SchemaSnapshot(
336
  customer_id="test",
337
+ timestamp=datetime.now(timezone.utc),
338
  tables={"table1": ["col1", "col2", "col3"]},
339
  row_counts={"table1": 10}
340
  )
341
 
342
  new_snapshot = SchemaSnapshot(
343
  customer_id="test",
344
+ timestamp=datetime.now(timezone.utc),
345
  tables={"table1": ["col1"]},
346
  row_counts={"table1": 10}
347
  )
 
356
  """Test detecting significant row count changes."""
357
  old_snapshot = SchemaSnapshot(
358
  customer_id="test",
359
+ timestamp=datetime.now(timezone.utc),
360
  tables={"table1": ["col1"]},
361
  row_counts={"table1": 100}
362
  )
363
 
364
  new_snapshot = SchemaSnapshot(
365
  customer_id="test",
366
+ timestamp=datetime.now(timezone.utc),
367
  tables={"table1": ["col1"]},
368
  row_counts={"table1": 10} # 90% decrease
369
  )
 
378
  """Test when no drift exists."""
379
  snapshot = SchemaSnapshot(
380
  customer_id="test",
381
+ timestamp=datetime.now(timezone.utc),
382
  tables={"table1": ["col1"]},
383
  row_counts={"table1": 10}
384
  )
 
391
  """Test snapshot persistence."""
392
  snapshot = SchemaSnapshot(
393
  customer_id="test",
394
+ timestamp=datetime.now(timezone.utc),
395
  tables={"table1": ["col1"]},
396
  row_counts={"table1": 10}
397
  )