Skip to content

fix: replace deprecated datetime.utcnow() with datetime.now(tz=timezone.utc)#5365

Open
NIK-TIGER-BILL wants to merge 1 commit intocrewAIInc:mainfrom
NIK-TIGER-BILL:fix/replace-deprecated-utcnow
Open

fix: replace deprecated datetime.utcnow() with datetime.now(tz=timezone.utc)#5365
NIK-TIGER-BILL wants to merge 1 commit intocrewAIInc:mainfrom
NIK-TIGER-BILL:fix/replace-deprecated-utcnow

Conversation

@NIK-TIGER-BILL
Copy link
Copy Markdown

@NIK-TIGER-BILL NIK-TIGER-BILL commented Apr 8, 2026

Summary

datetime.utcnow() was deprecated in Python 3.12 and emits a DeprecationWarning at runtime. This PR replaces all occurrences in the memory module with the timezone-aware equivalent datetime.now(tz=timezone.utc).

Changes

Source files (4 files, 9 call sites)

  • memory/storage/lancedb_storage.py — 4 calls replaced
  • memory/encoding_flow.py — 1 call replaced
  • memory/types.py — 2 default_factory + 1 call replaced
  • memory/unified_memory.py — 1 call replaced

Test files

  • tests/memory/test_unified_memory.py — 5 calls replaced (keeps tests consistent with source)

Why this matters

On Python 3.12+, every call to datetime.utcnow() emits:

DeprecationWarning: datetime.datetime.utcnow() is deprecated and scheduled for removal
in a future version. Use timezone-aware objects to represent datetimes in UTC:
datetime.datetime.now(datetime.timezone.utc)

The replacement is a drop-in equivalent — datetime.now(tz=timezone.utc) returns a timezone-aware UTC datetime. The Field(default_factory=...) entries in types.py use a lambda wrapper since datetime.now requires a keyword argument.

Testing

  • All replaced calls are semantically equivalent
  • Existing test suite updated to use timezone-aware datetimes

Note

Low Risk
Low risk mechanical change replacing datetime.utcnow() with datetime.now(tz=timezone.utc); main risk is subtle behavior differences when mixing naive vs aware datetimes in existing stored records.

Overview
Replaces deprecated datetime.utcnow() usages in the memory module with timezone-aware UTC timestamps via datetime.now(tz=timezone.utc).

This updates memory record defaults (created_at, last_accessed), scoring recency calculations, and LanceDB storage timestamp creation/touching/parsing to consistently operate in UTC, and adjusts unit tests to use timezone-aware datetimes.

Reviewed by Cursor Bugbot for commit 17c7269. Bugbot is set up for automated code reviews on this repo. Configure here.

…ne.utc)

datetime.utcnow() was deprecated in Python 3.12 and emits a
DeprecationWarning at runtime. Replace all occurrences across the
memory module (lancedb_storage, encoding_flow, types, unified_memory)
with the timezone-aware equivalent.

Changes:
- 4 source files: add timezone to imports, replace utcnow() calls
- 1 test file: update test helpers to use timezone-aware datetimes
- types.py: use lambda factory for Field defaults

Ref: https://docs.python.org/3/library/datetime.html#datetime.datetime.utcnow
Signed-off-by: NIK-TIGER-BILL <nik.tiger.bill@github.com>
Copy link
Copy Markdown

@cursor cursor bot left a comment

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 17c7269. Configure here.

"semantic" always; "recency" if decay > 0.5; "importance" if record.importance > 0.5.
"""
age_seconds = (datetime.utcnow() - record.created_at).total_seconds()
age_seconds = (datetime.now(tz=timezone.utc) - record.created_at).total_seconds()
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Naive/aware datetime subtraction causes TypeError on pre-existing data

High Severity

compute_composite_score now subtracts record.created_at from datetime.now(tz=timezone.utc) (timezone-aware), but _parse_dt in lancedb_storage.py can still return naive datetimes. When val is a datetime instance without tzinfo (line 271–272), it's returned as-is. When val is a string from pre-existing data stored via datetime.utcnow().isoformat() (no timezone suffix), fromisoformat also returns a naive datetime. Subtracting naive from aware raises TypeError at runtime, breaking recall for any data written before this change.

Additional Locations (1)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 17c7269. Configure here.

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.

1 participant