fix: replace deprecated datetime.utcnow() with datetime.now(tz=timezone.utc)#5365
fix: replace deprecated datetime.utcnow() with datetime.now(tz=timezone.utc)#5365NIK-TIGER-BILL wants to merge 1 commit intocrewAIInc:mainfrom
Conversation
…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>
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
❌ 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() |
There was a problem hiding this comment.
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)
Reviewed by Cursor Bugbot for commit 17c7269. Configure here.


Summary
datetime.utcnow()was deprecated in Python 3.12 and emits aDeprecationWarningat runtime. This PR replaces all occurrences in the memory module with the timezone-aware equivalentdatetime.now(tz=timezone.utc).Changes
Source files (4 files, 9 call sites)
memory/storage/lancedb_storage.py— 4 calls replacedmemory/encoding_flow.py— 1 call replacedmemory/types.py— 2default_factory+ 1 call replacedmemory/unified_memory.py— 1 call replacedTest 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:The replacement is a drop-in equivalent —
datetime.now(tz=timezone.utc)returns a timezone-aware UTC datetime. TheField(default_factory=...)entries intypes.pyuse a lambda wrapper sincedatetime.nowrequires a keyword argument.Testing
Note
Low Risk
Low risk mechanical change replacing
datetime.utcnow()withdatetime.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 viadatetime.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.