Catch Python datetime bugs before they hit production.
| Code | Issue | Fix |
|---|---|---|
| DT001 | datetime.utcnow() — deprecated, returns naive datetime |
datetime.now(timezone.utc) |
| DT002 | datetime.now() — naive local time, breaks across timezones |
datetime.now(timezone.utc) |
| DT003 | datetime.utcfromtimestamp() — deprecated, naive |
datetime.fromtimestamp(ts, tz=timezone.utc) |
| DT004 | datetime.fromtimestamp() without tz= — naive local time |
Add tz=timezone.utc |
| DT005 | .replace(tzinfo=None) — strips timezone, causes TypeError on comparison |
Keep timezone info |
Naive/aware datetime mixing causes TypeError: can't compare offset-naive and offset-aware datetimes in production. It's one of Python's most common runtime errors and it's entirely preventable.
# .github/workflows/lint.yml
name: Datetime Lint
on: [pull_request]
jobs:
datetime-lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: qwenbona/datetime-lint-action@v1
with:
path: 'src/'
fail-on-warning: 'true'::warning file=app/services/cache.py,line=42,title=DT001::datetime.utcnow() is deprecated...
::warning file=app/utils/helpers.py,line=18,title=DT002::datetime.now() without a timezone...
2 datetime issue(s) found across 47 files.
python3 lint.py ./srcMIT