Skip to content

源代码大量使用 datetime.utcnow() —— Python 3.12 已 deprecated #7

@Zld1994

Description

@Zld1994

问题

源代码中至少 13 个文件、测试中至少 3 个文件使用了 datetime.utcnow(),它在 Python 3.12 已被标记 deprecated,未来版本会移除:

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.UTC).

影响文件 (16 个)

源代码:

  • agentManager/api.py
  • agentManager/engine/event_bus.py
  • agentManager/engine/event_bus/base.py
  • agentManager/engine/event_bus/redis_stream.py
  • agentManager/engine/scheduler.py
  • agentManager/engine/state_manager.py
  • agentManager/memory/memory_system.py
  • agentManager/memory/task_history.py
  • agentManager/recovery/recovery_context.py
  • agentManager/recovery/recovery_engine.py
  • agentManager/runtime/execution_context.py
  • agentManager/defect_repair/repair_pipeline.py
  • agentManager/defect_repair/repair_strategies.py

测试:

  • tests/unit/test_redis_stream_event_bus.py
  • tests/unit/memory/test_memory_system.py
  • tests/unit/memory/test_task_history.py

解决方案

全仓库替换:

# 旧
from datetime import datetime
datetime.utcnow()

# 新
from datetime import datetime, timezone
datetime.now(timezone.utc)

注意:新返回值是 timezone-aware 的 datetime,与原来的 naive datetime 不兼容做比较;如果代码里有 dt1 < dt2 这种比较,要保证两边都是 aware 或都是 naive。建议统一改为 aware。

可以用一条命令快速定位:

grep -rn 'datetime\.utcnow()' --include='*.py' .

影响

  • 严重程度:中(当前只是警告,但 Python 3.13/3.14 可能直接移除)
  • 范围:项目所有时间戳逻辑(事件、状态、内存、调度、checkpoint)

Metadata

Metadata

Assignees

No one assigned

    Labels

    good-first-issueGood first issue for new contributors

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions