Skip to content

fix(harness): EnvironmentContext.today_date returns naive datetime in timezone fallback path #26

@caoergou

Description

@caoergou

Problem

EnvironmentContext.today_date is documented as returning a timezone-aware datetime:

  • The EnvironmentContext type's docstring states: "Timezone-aware datetime on the computer"
  • The comment in EnvironmentResolver.resolve() reads: "Parse into a timezone-aware datetime."

However, the fallback path in environment.py (line 81) silently produces a naive datetime when strptime with %z fails:

# environment.py  line 80-81
except ValueError:
    now = datetime.strptime(raw_dt[:19], "%Y-%m-%dT%H:%M:%S")  # noqa: DTZ007

The # noqa: DTZ007 suppression is itself a signal that the linter flagged this as a timezone-unaware datetime. The existing test even asserts the broken contract explicitly:

# test_environment.py  line 84
assert env.today_date.tzinfo is None   # contradicts the documented "timezone-aware" guarantee

Impact

Any downstream code that treats today_date.tzinfo as reliably set — such as timezone-aware arithmetic or comparisons — will raise a TypeError on hosts where the shell's date output lacks a numeric UTC offset (some containers, minimal environments, or locale edge cases).

Proposed Fix

In the except ValueError fallback, attach UTC explicitly so the contract is always satisfied:

from datetime import UTC, datetime

# ...
except ValueError:
    now = datetime.strptime(raw_dt[:19], "%Y-%m-%dT%H:%M:%S").replace(tzinfo=UTC)

This removes the need for # noqa: DTZ007 and brings the test assertion in line with the documented contract:

assert env.today_date.tzinfo is not None

The change is minimal (1 import, 1 line, 1 test assertion) and does not affect the happy path.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions