Skip to content

Fix deprecated datetime.utcnow() calls (10 instances across 7 files)#129

Open
MichaelMVS wants to merge 1 commit intoGACWR:masterfrom
MichaelMVS:fix-deprecated-datetime-utcnow
Open

Fix deprecated datetime.utcnow() calls (10 instances across 7 files)#129
MichaelMVS wants to merge 1 commit intoGACWR:masterfrom
MichaelMVS:fix-deprecated-datetime-utcnow

Conversation

@MichaelMVS
Copy link
Copy Markdown

Replace all datetime.utcnow() with timezone-aware datetime.now(timezone.utc) to address Python 3.12+ deprecation warnings.

Changes

Fixed 10 instances of deprecated datetime.utcnow() across 7 files:

  • core/api_routers/auth.py: last_login_at timestamp
  • core/api_routers/workspaces.py: stopped_at timestamp
  • core/auth.py: JWT token expiration timestamps
  • core/operator/pipeline_handler.py: pipeline started_at/completed_at timestamps
  • core/repositories/anomaly_repository.py: anomaly timestamp and acknowledged_at
  • core/repositories/case_repository.py: case resolved_at timestamp
  • core/services/data_ingestion.py: Elasticsearch document @timestamp

Why this matters

datetime.utcnow() is deprecated in Python 3.12+ and will be removed in a future version. It returns a naive datetime without timezone info, which can cause issues when comparing timestamps across different systems and timezones. Using datetime.now(timezone.utc) provides a timezone-aware datetime that works correctly in all contexts.

Testing

The change is additive and backwards-compatible — all files already imported datetime, just added timezone import and replaced the function call.

Replace all datetime.utcnow() with timezone-aware datetime.now(timezone.utc)
to address Python 3.12+ deprecation warnings.

Files changed:
- core/api_routers/auth.py: last_login_at timestamp
- core/api_routers/workspaces.py: stopped_at timestamp
- core/auth.py: JWT token expiration
- core/operator/pipeline_handler.py: pipeline started_at/completed_at timestamps
- core/repositories/anomaly_repository.py: anomaly timestamp and acknowledged_at
- core/repositories/case_repository.py: case resolved_at timestamp
- core/services/data_ingestion.py: ES document @timestamp
Copilot AI review requested due to automatic review settings April 12, 2026 10:36
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Updates timestamp generation throughout the backend to eliminate Python 3.12+ deprecation warnings by replacing datetime.utcnow() with timezone-aware datetime.now(timezone.utc).

Changes:

  • Replaced multiple datetime.utcnow() usages with datetime.now(timezone.utc) for DB timestamps and status timestamps.
  • Updated imports in affected modules to include timezone.
  • Updated ISO8601 timestamp string generation for Elasticsearch docs and pipeline status fields.

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
core/api_routers/auth.py Sets last_login_at using a timezone-aware UTC datetime.
core/api_routers/workspaces.py Sets stopped_at using a timezone-aware UTC datetime.
core/auth.py Generates JWT expiration (exp) using a timezone-aware UTC datetime.
core/operator/pipeline_handler.py Uses timezone-aware UTC timestamps for pipeline started_at/completed_at status fields.
core/repositories/anomaly_repository.py Uses timezone-aware UTC datetimes for anomaly timestamp defaults and acknowledged_at.
core/repositories/case_repository.py Uses timezone-aware UTC datetime for resolved_at.
core/services/data_ingestion.py Sets Elasticsearch @timestamp using a timezone-aware UTC ISO8601 string.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread core/auth.py
Comment on lines 47 to 51
if expires_delta:
expire = datetime.utcnow() + expires_delta
expire = datetime.now(timezone.utc) + expires_delta
else:
expire = datetime.utcnow() + timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES)
expire = datetime.now(timezone.utc) + timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES)
to_encode.update({"exp": expire})
Copy link

Copilot AI Apr 12, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR description/title says all deprecated datetime.utcnow() calls were replaced, but there are still several occurrences in the repo (e.g., core/services/model_orchestrator.py:133,137,167,285,290,307,319,324; core/services/rule_engine.py:385,430; core/services/workspace_service.py:107). Either include these in this PR or adjust the wording to reflect that only a subset was updated.

Copilot uses AI. Check for mistakes.
@@ -103,7 +103,7 @@ def acknowledge(
return None
from datetime import datetime
Copy link

Copilot AI Apr 12, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The local from datetime import datetime inside acknowledge() is redundant now that datetime is already imported at module scope, and it also creates an unnecessary shadowing point right next to the new timezone-aware call. Consider removing the inner import and using the module-level datetime for clarity.

Suggested change
from datetime import datetime

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants