fix(py-mesh-datetime): replace deprecated utcnow with tz-aware UTC#2179
Merged
imran-siddique merged 1 commit intoMay 12, 2026
Merged
Conversation
`datetime.utcnow()` is deprecated in Python 3.12+ and returns a naive datetime, which silently mixes with tz-aware datetimes elsewhere in the codebase and causes `TypeError: can't subtract offset-naive and offset-aware datetimes` at runtime. Across `agent-governance-python/agent-mesh/`, this sweep replaces: - `datetime.utcnow()` -> `datetime.now(timezone.utc)` - `default_factory=datetime.utcnow` -> `default_factory=lambda: datetime.now(timezone.utc)` The latter form was the load-bearing case: pydantic `Field(default_factory=...)` sites stored naive timestamps in models like `HandshakeChallenge.timestamp`, which were then subtracted from `datetime.now(timezone.utc)` in `is_expired()` and friends. Imports updated to include `timezone` where missing. Scope covers `src/agentmesh/**`, `tests/**`, `examples/**` plus three doc snippets (`README.md`, `docs/zero-trust.md`, `docs/identity.md`, `examples/00-registration-hello-world/README.md`) that demonstrate the old pattern to users. Test suite: 11 failed before, 11 failed after (pre-existing Python 3.14 deprecation issues in `test_cedar.py`, `test_policy_provider.py`, `test_websocket_transport.py`). Pydantic deprecation warning count drops from 32707 to 34 -- the remaining warnings are pydantic's own internal utcnow() usage in `pydantic/main.py:250`, not in this codebase.
🤖 AI Agent: breaking-change-detector — API CompatibilityAPI Compatibility
|
🤖 AI Agent: test-generator — `src/agentmesh/governance/policy.py`
|
🤖 AI Agent: security-scanner — View detailsNo security issues found. |
🤖 AI Agent: docs-sync-checker — Docs SyncDocs Sync
|
🤖 AI Agent: code-reviewer — View detailsTL;DR: 0 blockers, 0 warnings. No issues found. Clean change. |
|
🟡 Contributor Check: MEDIUM
Automated check by AGT Contributor Check. |
PR Review Summary
Verdict: ✅ Ready for human review |
MohammadHaroonAbuomar
pushed a commit
to MohammadHaroonAbuomar/agt-acs
that referenced
this pull request
Jun 1, 2026
…icrosoft#2179) `datetime.utcnow()` is deprecated in Python 3.12+ and returns a naive datetime, which silently mixes with tz-aware datetimes elsewhere in the codebase and causes `TypeError: can't subtract offset-naive and offset-aware datetimes` at runtime. Across `agent-governance-python/agent-mesh/`, this sweep replaces: - `datetime.utcnow()` -> `datetime.now(timezone.utc)` - `default_factory=datetime.utcnow` -> `default_factory=lambda: datetime.now(timezone.utc)` The latter form was the load-bearing case: pydantic `Field(default_factory=...)` sites stored naive timestamps in models like `HandshakeChallenge.timestamp`, which were then subtracted from `datetime.now(timezone.utc)` in `is_expired()` and friends. Imports updated to include `timezone` where missing. Scope covers `src/agentmesh/**`, `tests/**`, `examples/**` plus three doc snippets (`README.md`, `docs/zero-trust.md`, `docs/identity.md`, `examples/00-registration-hello-world/README.md`) that demonstrate the old pattern to users. Test suite: 11 failed before, 11 failed after (pre-existing Python 3.14 deprecation issues in `test_cedar.py`, `test_policy_provider.py`, `test_websocket_transport.py`). Pydantic deprecation warning count drops from 32707 to 34 -- the remaining warnings are pydantic's own internal utcnow() usage in `pydantic/main.py:250`, not in this codebase.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
datetime.utcnow()is deprecated in Python 3.12+ and returns a naive datetime, which silently mixes with timezone-aware datetimes elsewhere in the codebase. The result isTypeError: can't subtract offset-naive and offset-aware datetimesat runtime in handshake/expiry/scoring paths.This sweep replaces every
utcnow()site acrossagent-governance-python/agent-mesh/withdatetime.now(timezone.utc), and the structurally-equivalent pydanticdefault_factory=datetime.utcnowwithdefault_factory=lambda: datetime.now(timezone.utc).Change
datetime.utcnow()datetime.now(timezone.utc)default_factory=datetime.utcnowdefault_factory=lambda: datetime.now(timezone.utc)from datetime import datetime[, ...](missingtimezone)timezoneThe
default_factorycase was load-bearing: models such asHandshakeChallenge.timestamp,AgentIdentity.created_at,Credential.issued_at, andRewardScore.calculated_atstored naive timestamps that were then subtracted from anow(timezone.utc)inis_expired()/is_valid()/ continuous-trust accumulators, producing the TypeError under registered-identity handshakes, signature tampering tests, capability inflation tests, expired-cert checks, and similar paths.Scope covers
src/agentmesh/**,tests/**, andexamples/**, plus four doc files (README.md,docs/zero-trust.md,docs/identity.md,examples/00-registration-hello-world/README.md) that showed the deprecated pattern in user-facing snippets.Tests
Identical failure set (pre-existing Python 3.14 deprecations in
test_cedar.py,test_policy_provider.py,test_websocket_transport.py-- unrelated to this change). Pydantic deprecation warning count drops to 34, all of which are pydantic's own internalutcnow()usage inpydantic/main.py:250, not in this codebase.Recipe (PowerShell):
Test plan
datetime.utcnowreferences inagent-governance-python/agent-mesh/Python sourcestimezoneimportedSurfaced during independent audit conducted by @finnoybu (Ken Tannenbaum, AEGIS Initiative); [LOW, Python Mesh].