Skip to content

Validate start/end times in the reporting client and CLI - #335

Open
cwasicki wants to merge 3 commits into
frequenz-floss:v0.x.xfrom
cwasicki:times
Open

cwasicki wants to merge 3 commits into
frequenz-floss:v0.x.xfrom
cwasicki:times

Conversation

@cwasicki

@cwasicki cwasicki commented Oct 2, 2026

Copy link
Copy Markdown
Contributor

Guards the reporting request time range against two easy mistakes: ambiguous naive datetimes and an inverted window.

  • Reject timezone-naive start_time/end_time in the client. A naive value is ambiguous, the wire encoding reads it as UTC while datetime.timestamp() reads it as local time, so the same input would denote different instants. The
    client now raises rather than silently pick one meaning.
  • Require a timezone offset on the CLI --start/--end arguments, so a naive value fails at argument parsing with a clear message instead of surfacing as a ValueError later.
  • Warn on the CLI when --end precedes --start. The inverted window is still a legal, empty range and the request runs, but at the CLI it is almost always a typo, and the warning explains the otherwise silent empty output.

@cwasicki
cwasicki requested a balanced review from Copilot October 2, 2026 17:45
@github-actions github-actions Bot added the part:tests Affects the unit, integration and performance (benchmarks) tests label Oct 2, 2026

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot review overview

🟡 Changes recommended

The awareness check misses valid naive cases, and the new constraint leaves public documentation and examples inaccurate.

Review effort: Balanced
Findings: 3 Low severity

Open (3)
What changed in this PR

Adds timezone validation and inverted-window warnings to reporting requests.

Changes:

  • Rejects timezone-naive client inputs.
  • Validates CLI timestamps and warns about inverted ranges.
  • Adds client validation tests.
File Description
src/​frequenz/​client/​reporting/​_client.py Validates request timestamps.
src/​frequenz/​client/​reporting/​cli/​__main__.py Validates CLI timestamps and warns on inverted ranges.
tests/​test_client_reporting.py Tests client-side validation.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines +66 to +67
def _reject_naive(**times: datetime | None) -> None:
"""Raise if any given datetime is timezone-naive.
include_bounds: bool = False,
) -> Receiver[ComponentsDataBatch]:
"""Return a Receiver for the microgrid component data stream."""
_reject_naive(start_time=start_time, end_time=end_time)
parsed = datetime.fromisoformat(value)
if parsed.tzinfo is None:
raise argparse.ArgumentTypeError(
f"datetime must include a timezone offset, e.g. {value}+00:00"
A naive datetime is ambiguous: the wire encoding assumes UTC while
datetime.timestamp() assumes local time, so the same value would denote
different instants. Reject it at the public request boundary rather than
silently pick one meaning.

Signed-off-by: cwasicki <[email protected]>
A naive datetime is ambiguous: the wire encoding assumes UTC while
datetime.timestamp() assumes local time. Reject it at the argparse boundary
so the CLI fails with a clear message instead of a ValueError.

Signed-off-by: cwasicki <[email protected]>
An inverted window is a legal but empty range, so the request still runs;
at the CLI it is almost always a typo, and the silent empty output gives
no hint why.

Signed-off-by: cwasicki <[email protected]>
@github-actions github-actions Bot added the part:docs Affects the documentation label Oct 2, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

part:docs Affects the documentation part:tests Affects the unit, integration and performance (benchmarks) tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants