Skip to content

fix(sdk/python): replace deprecated datetime.utcnow() with timezone-a…#332

Merged
santoshkumarradha merged 2 commits intoAgent-Field:mainfrom
deensaleh:fix/replace-deprecated-datetime-utcnow
Apr 5, 2026
Merged

fix(sdk/python): replace deprecated datetime.utcnow() with timezone-a…#332
santoshkumarradha merged 2 commits intoAgent-Field:mainfrom
deensaleh:fix/replace-deprecated-datetime-utcnow

Conversation

@deensaleh
Copy link
Copy Markdown
Contributor

…ware alternative

datetime.utcnow() is deprecated since Python 3.12 and scheduled for removal. It produces naive datetime objects with no timezone info, which causes DeprecationWarning on every test run across 5 files.

Additionally, two call sites combined .isoformat() + 'Z' on a timezone-aware datetime, producing invalid ISO strings like:
'2026-04-05T01:30:20.584591+00:00Z' ← double timezone suffix

This caused test failures in test_vc_generator.py.

Changes:

  • agentfield/did_manager.py: add timezone import, utcnow → now(timezone.utc)
  • agentfield/agent.py: utcnow → now(timezone.utc), fix isoformat+Z
  • agentfield/client.py: datetime.datetime.utcnow() → datetime.datetime.now(datetime.timezone.utc)
  • agentfield/vc_generator.py: add timezone import, utcnow → now(timezone.utc)
  • tests/test_vc_generator.py: add timezone import, utcnow → now(timezone.utc), fix isoformat+Z

All 745 tests pass with 0 failures after this change. DeprecationWarnings reduced from 36 to 0.

Summary

Testing

  • ./scripts/test-all.sh
  • Additional verification (please describe):

Checklist

  • I updated documentation where applicable.
  • I added or updated tests (or none were needed).
  • I updated CHANGELOG.md (or this change does not warrant a changelog entry).

Screenshots (if UI-related)

Related issues

…ware alternative

datetime.utcnow() is deprecated since Python 3.12 and scheduled for
removal. It produces naive datetime objects with no timezone info,
which causes DeprecationWarning on every test run across 5 files.

Additionally, two call sites combined .isoformat() + 'Z' on a
timezone-aware datetime, producing invalid ISO strings like:
  '2026-04-05T01:30:20.584591+00:00Z'  ← double timezone suffix

This caused test failures in test_vc_generator.py.

Changes:
- agentfield/did_manager.py: add timezone import, utcnow → now(timezone.utc)
- agentfield/agent.py: utcnow → now(timezone.utc), fix isoformat+Z
- agentfield/client.py: datetime.datetime.utcnow() → datetime.datetime.now(datetime.timezone.utc)
- agentfield/vc_generator.py: add timezone import, utcnow → now(timezone.utc)
- tests/test_vc_generator.py: add timezone import, utcnow → now(timezone.utc), fix isoformat+Z

All 745 tests pass with 0 failures after this change.
DeprecationWarnings reduced from 36 to 0.
@CLAassistant
Copy link
Copy Markdown

CLAassistant commented Apr 5, 2026

CLA assistant check
All committers have signed the CLA.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 5, 2026

Performance

SDK Memory Δ Latency Δ Tests Status
Python 9.3 KB +4% 0.32 µs -9%

✓ No regressions detected

@santoshkumarradha
Copy link
Copy Markdown
Member

@deensaleh Thanks for the cleanup here, this is heading in the right direction. I noticed there are still a couple of client.py call sites building timestamps with datetime.datetime.now().isoformat() + "Z" for last_heartbeat and registered_at. Those are still naive local timestamps with a UTC suffix added afterward, so the timestamp handling is not fully consistent yet.

Could you update those remaining registration payload timestamps to use an actual UTC-aware datetime, and add a small test around that path in client.py? Right now the test coverage added in this PR is focused on vc_generator.py, so the client.py registration flow still is not covered.

Not blocking on this part, but it may also be worth using one small shared helper for outbound UTC timestamps so this does not drift across files again.

…tion payloads

Per code review feedback from @santoshkumarradha:

- Add _utc_now_iso() shared helper in client.py for consistent UTC
  timestamp formatting across the file
- Replace 4 remaining datetime.datetime.now().isoformat() + 'Z' calls
  in the registration payloads (last_heartbeat, registered_at) with
  the helper — these were producing naive local timestamps with a UTC
  suffix incorrectly appended
- Add TestUtcNowIso test class (5 tests) covering: string type, Z suffix,
  valid ISO parse, no double-offset, millisecond precision

All 745 tests pass.
@deensaleh deensaleh requested review from a team and AbirAbbas as code owners April 5, 2026 06:55
@deensaleh
Copy link
Copy Markdown
Contributor Author

deensaleh commented Apr 5, 2026 via email

Copy link
Copy Markdown
Member

@santoshkumarradha santoshkumarradha left a comment

Choose a reason for hiding this comment

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

Looks good now. Thanks for following up on the remaining client.py timestamp paths and adding coverage around the UTC helper.

@santoshkumarradha santoshkumarradha added this pull request to the merge queue Apr 5, 2026
Merged via the queue into Agent-Field:main with commit 265cf79 Apr 5, 2026
18 checks passed
@deensaleh
Copy link
Copy Markdown
Contributor Author

deensaleh commented Apr 5, 2026 via email

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.

3 participants