fix: add UTC timezone suffix to datetime serializers#12995
fix: add UTC timezone suffix to datetime serializers#12995luke-mino-altherr wants to merge 1 commit intoComfy-Org:masterfrom
Conversation
|
Part of the Unify Timestamps effort. Related PRs:
|
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (3)
🚧 Files skipped from review as they are similar to previous changes (2)
📝 WalkthroughWalkthroughThe PR adds explicit timezone handling: API schema serializers and the database model assign UTC (timezone.utc) to datetime values that lack tzinfo before calling isoformat(). The helper docstring was updated to clarify usage for DB columns declared with timezone=False; its implementation remains unchanged (it still returns a UTC time with tzinfo stripped). No public/exported function or class signatures were changed. 🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. 📝 Coding Plan
Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
app/assets/api/schemas_out.py (1)
21-27: LGTM with optional DRY suggestion.The serialization logic is correct. Consider extracting the repeated pattern into a module-level helper to reduce duplication across the three serializer methods.
,
♻️ Optional: Extract shared helper
def _serialize_dt(v: datetime | None) -> str | None: if v is None: return None if v.tzinfo is None: v = v.replace(tzinfo=timezone.utc) return v.isoformat()Then each serializer becomes:
`@field_serializer`("created_at", "updated_at", "last_access_time") def _serialize_datetime(self, v: datetime | None, _info): - if v is None: - return None - if v.tzinfo is None: - v = v.replace(tzinfo=timezone.utc) - return v.isoformat() + return _serialize_dt(v)🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@app/assets/api/schemas_out.py` around lines 21 - 27, The datetime serializer _serialize_datetime repeats logic that can be reused; extract a module-level helper (e.g., _serialize_dt) that accepts datetime | None and returns str | None, performing the None check, ensuring tzinfo (replace with timezone.utc when missing) and calling isoformat, then update _serialize_datetime (and any other serializers for "created_at", "updated_at", "last_access_time") to delegate to that helper to remove duplication while keeping behavior identical.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@app/assets/api/schemas_out.py`:
- Around line 21-27: The datetime serializer _serialize_datetime repeats logic
that can be reused; extract a module-level helper (e.g., _serialize_dt) that
accepts datetime | None and returns str | None, performing the None check,
ensuring tzinfo (replace with timezone.utc when missing) and calling isoformat,
then update _serialize_datetime (and any other serializers for "created_at",
"updated_at", "last_access_time") to delegate to that helper to remove
duplication while keeping behavior identical.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: 34f3dfd3-8eba-49b7-9f1d-319a485d4f8c
📒 Files selected for processing (3)
app/assets/api/schemas_out.pyapp/assets/helpers.pyapp/database/models.py
Co-authored-by: Amp <amp@ampcode.com> Amp-Thread-ID: https://ampcode.com/threads/T-019cf73e-7f44-7003-8886-58eaaf1edbf4
1eb1bac to
7da9f22
Compare
Datetime fields in asset API responses are serialized via
.isoformat()without timezone info, producing strings like"2026-03-16T10:30:00.000000". Per the ECMAScript spec,new Date()parses these as local time, causing offset errors when the browser timezone ≠ UTC.Changes:
get_utc_now()now returns timezone-awaredatetime(also the Python 3.12+ recommended replacement for deprecateddatetime.utcnow())field_serializermethods inschemas_out.pyattach+00:00to legacy naive datetimesto_dict()inmodels.pyapplies the same patternOutput changes from
"2026-03-16T10:30:00.000000"to"2026-03-16T10:30:00+00:00"which is valid RFC 3339 and parsed correctly by JavaScript.